$$include '../../meta/macros.ptl'

import [mix linreg clamp fallback] from "@iosevka/util"
import [DependentSelector] from "@iosevka/glyph/relation"

glyph-module

glyph-block Letter-Cyrillic-Yat : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Adjustment : ExtendAboveBaseAnchors
	glyph-block-import Letter-Shared : CreateDependentComposite
	glyph-block-import Letter-Shared-Metrics : BowlXDepth
	glyph-block-import Letter-Shared-Shapes : LetterBarOverlay
	glyph-block-import Letter-Cyrillic-Yeri : YeriConfig YeriBarPos
	glyph-block-import Letter-Cyrillic-Iotified-A : Iotified

	define [xBarLeft df] : Math.max (df.rightSB - (RightSB - SB)) : if SLAB
		Just ([mix df.leftSB df.rightSB 0.35] - [HSwToV : 0.5 * df.mvs])
		Just  [mix df.leftSB df.rightSB 0.2]

	define [YatShape] : with-params [df Yeri top [pBar YeriBarPos] [fLowerCase false] [fIotifiedBase false] [sw df.mvs] [xCrossbarLeftOverride nothing] [yCrossbarOverride nothing]] : glyph-proc
		local xYeriLeft : xBarLeft df

		include : Yeri top
			left   -- xYeriLeft
			right  -- (df.rightSB - O * 2)
			pBar   -- pBar
			stroke -- sw

		local cTop : if SLAB (top - HalfStroke) top

		local _xCrossbarLeft : mix 0 df.leftSB : if SLAB 0.25 0.375
		local xCrossbarLeft : fallback xCrossbarLeftOverride _xCrossbarLeft
		local xCrossbarRight : mix _xCrossbarLeft (xYeriLeft + [HSwToV : 0.5 * sw]) 2
		local yCrossbar : fallback yCrossbarOverride : [mix (top * pBar + sw / 2) cTop : if SLAB 0.625 0.5] + 0.25 * OverlayStroke
		include : HBar.t xCrossbarLeft xCrossbarRight yCrossbar OverlayStroke

		if SLAB : begin
			local swSerif : Math.min (VJutStroke * sw / Stroke) (0.625 * (xYeriLeft - _xCrossbarLeft))
			local vJut : Math.min VJut (OverlayStroke + 0.5 * (yCrossbar - OverlayStroke - (top * pBar)))
			include : tagged 'serifDL' : VSerif.dl xCrossbarLeft  yCrossbar vJut swSerif
			include : tagged 'serifDR' : VSerif.dr xCrossbarRight yCrossbar vJut swSerif

			if fLowerCase : begin
				eject-contour 'serifYeriLT'
				include : tagged 'serifLT' : if (fIotifiedBase && para.isItalic) [glyph-proc] [HSerif.lt xYeriLeft top SideJut sw]

	define [IotifiedYatShape] : with-params [df Yeri top [pBar YeriBarPos] [fLowerCase false] [sw df.mvs]] : glyph-proc
		local gap : 0.25 * (df.width - 2 * df.leftSB - [if SLAB 2.5 4.5] * sw)
		define divSub : (df.width - gap - sw) / Width
		define dfSub : DivFrame divSub 2

		local shift : Width * (df.adws - divSub)
		include : with-transform [ApparentTranslate shift 0]
			YatShape dfSub Yeri top
				pBar          -- pBar
				fLowerCase    -- fLowerCase
				fIotifiedBase -- true
				sw            -- sw
				xCrossbarLeftOverride -- (df.leftSB - shift)
		eject-contour 'serifDL'

		include : Iotified.[if fLowerCase 'ascender' 'full'] df top 0 (fCapital -- (!fLowerCase))

	foreach { suffix { Uc Lc } } [pairs-of YeriConfig] : do
		create-glyph "cyrl/Yat.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleT
			include : df.markSet.capital
			include : YatShape df Uc CAP
				pBar -- 0.5

		create-glyph "cyrl/yat.upright.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleT
			include : df.markSet.b
			include : YatShape df Lc Ascender
				pBar -- (YeriBarPos * XH / Ascender)
				fLowerCase -- true

		create-glyph "cyrl/yatTall.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleT
			include : df.markSet.b
			local asc : Ascender + 0.5 * AccentStackOffset
			include : ExtendAboveBaseAnchors asc
			include : YatShape df Lc asc
				pBar -- (YeriBarPos * XH / asc)
				fLowerCase -- true
				yCrossbarOverride -- (Ascender - [if SLAB (0.25 * df.mvs) 0])

		create-glyph "cyrl/YatIotified.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleM 3
			include : df.markSet.capital
			include : IotifiedYatShape df Uc CAP
				pBar -- 0.5

		create-glyph "cyrl/yatIotified.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleM 3
			include : df.markSet.b
			include : IotifiedYatShape df Lc Ascender
				pBar -- 0.375
				fLowerCase -- true

		# Italic Yat
		create-glyph "cyrl/yat.italic/yeri.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleMM 3
			include : df.markSet.e

			local xMidBarLeft : df.middle - [HSwToV : 0.5 * df.mvs]

			include : Lc XH xMidBarLeft df.rightSB
				stroke -- df.mvs
				yStart -- (XH - SmallArchDepthB)
			eject-contour 'serifYeriLT'
			eject-contour 'serifYeriLB'

			DependentSelector.set currentGlyph : if (suffix === "corner") 'full' 'reduced'

		create-glyph "cyrl/tje.italic/yeri.\(suffix)" : glyph-proc
			local df : include : DivFrame [mix 1 para.advanceScaleMM 2] 4
			include : df.markSet.e

			local dfSub : df.slice 4 3 0
			local xMidBarLeft : dfSub.rightSB - [HSwToV df.mvs]

			include : Lc XH xMidBarLeft df.rightSB
				stroke -- df.mvs
				yStart -- (XH - SmallArchDepthB)
			eject-contour 'serifYeriLT'
			eject-contour 'serifYeriLB'

			DependentSelector.set currentGlyph : if (suffix === "corner") 'full' 'reduced'

	select-variant 'cyrl/Yat' 0x462 (follow -- 'cyrl/Yeri')
	select-variant 'cyrl/yat.upright' (follow -- 'cyrl/yeri')
	select-variant 'cyrl/yatTall' 0x1C87 (follow -- 'cyrl/yeri')

	select-variant 'cyrl/YatIotified' 0xA652 (follow -- 'cyrl/Yeri')
	select-variant 'cyrl/yatIotified' 0xA653 (follow -- 'cyrl/yeri')

	select-variant 'cyrl/yat.italic/yeri' (follow -- 'cyrl/yeri')
	CreateDependentComposite 'cyrl/yat.italic' null 'cyrl/yat.italic/yeri' : object
		full    'cyrl/yat.italic/base/corner'
		reduced 'cyrl/yat.italic/base/cursive'

	select-variant 'cyrl/tje.italic/yeri' (follow -- 'cyrl/tje/rightHalf')
	CreateDependentComposite 'cyrl/tje.italic' null 'cyrl/tje.italic/yeri' : object
		full    'cyrl/tje.italic/base/corner'
		reduced 'cyrl/tje.italic/base/cursive'

	alias 'latn/yatSakha.italic' null 'cyrl/yat.italic'
