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

import [mix linreg clamp fallback SuffixCfg] from "@iosevka/util"
import [DesignParameters] from "../../meta/aesthetics.mjs"
import [MathSansSerif] from "@iosevka/glyph/relation"

glyph-module

glyph-block Letter-Greek-Psi : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives

	define [PsiShape df y1 y2 y3 y4 ada adb doBotSerif doTopSerif doSideSerifL doSideSerifR] : glyph-proc
		include : UShape df y3 y2 df.mvs ada adb
		include : VBar.m df.middle y2 y4 df.mvs
		include : VBar.m df.middle y1 (y2 + HalfStroke)

		if doSideSerifL : include : tagged 'serifLT' : HSerif.lt df.leftSB  y3 SideJut
		if doSideSerifR : include : tagged 'serifRT' : HSerif.rt df.rightSB y3 SideJut
		if doTopSerif   : include : tagged 'serifMT' : HSerif.mt df.middle CAP MidJutCenter
		if doBotSerif   : include : tagged 'serifMB' : HSerif.mb df.middle 0   MidJutCenter

	create-glyph 'grek/Psi' 0x3A8 : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		include : df.markSet.capital
		include : PsiShape df 0 (CAP * 0.3) CAP CAP df.archDepthA df.archDepthB SLAB (SLAB && (df.width > para.refJut * 7)) SLAB SLAB

	alias 'cyrl/Psi' 0x470 'grek/Psi'

	create-glyph 'smcpPsi' 0x1D2A : glyph-proc
		local df : include : DivFrame para.advanceScaleT 3
		include : df.markSet.e
		include : PsiShape df 0 (XH * 0.3) XH XH df.archDepthA df.archDepthB SLAB false SLAB SLAB

	define GrekLowerPsiConfig : SuffixCfg.weave
		object # top
			""                  Ascender
			flatTop             XH
		object # slab
			serifless           false
			serifed             true

	foreach { suffix { top slab } } [Object.entries GrekLowerPsiConfig] : do
		create-glyph "grek/psi.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleM 3
			include : df.markSet.[if (top > XH) 'bp' 'p']
			include : PsiShape df Descender 0 XH top df.smallArchDepthA df.smallArchDepthB false false slab false

	select-variant 'grek/psi' 0x3C8
	link-reduced-variant 'grek/psi/sansSerif' 'grek/psi' MathSansSerif
	select-variant 'cyrl/psi' 0x471 (shapeFrom -- 'grek/psi')
