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

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

glyph-module

glyph-block Letter-Greek-Lower-Xi : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives

	define SHAPE-ROUNDED  0
	define SHAPE-FLAT-TOP 1

	define [LowerXiShape shape] : glyph-proc
		local adaTop : ArchDepthAOf (ArchDepth * 0.45) Width
		local adbTop : ArchDepthBOf (ArchDepth * 0.45) Width
		local adaBot : ArchDepthAOf (ArchDepth * 0.55) Width
		local adbBot : ArchDepthBOf (ArchDepth * 0.55) Width

		local fineTop : Stroke * CThinB
		local fine : Stroke * CThin
		local xBarStart : mix SB RightSB : match shape
			[Just SHAPE-ROUNDED]  0.625
			[Just SHAPE-FLAT-TOP] 0.75
		local xBarEnd : mix SB RightSB : match shape
			[Just SHAPE-ROUNDED]  0.75
			[Just SHAPE-FLAT-TOP] 0.85
		local yBar : mix 0 Ascender 0.55

		local xUpperArcLeft : match shape
			[Just SHAPE-ROUNDED]  : SB + 0
			[Just SHAPE-FLAT-TOP] : [mix SB RightSB 0.075] + OX
		local xLowerArcLeft : match shape
			[Just SHAPE-ROUNDED]  : SB + OX * 2
			[Just SHAPE-FLAT-TOP] : [mix SB RightSB 0.025] + OX * 2

		include : match shape
			[Just SHAPE-FLAT-TOP] : HBar.t SB RightSB Ascender
			__                    : glyph-proc

		include : dispiro
			match shape
				[Just SHAPE-ROUNDED] : list
					g4 (RightSB + O) (Ascender - Hook) [widths.lhs]
					hookstart Ascender
				[Just SHAPE-FLAT-TOP] : list
					flat xBarEnd (Ascender - Stroke + fineTop) [widths.lhs fineTop]
					curl xBarStart (Ascender - Stroke + fineTop) [widths.lhs fineTop]
					archv 16
			g4 xUpperArcLeft [YSmoothMidL Ascender (yBar - Stroke) adaTop adbTop] [widths.lhs]
			arcvh 16
			flat xBarStart (yBar - fine) [widths.lhs.heading fine Rightward]
			curl xBarEnd (yBar - fine) [widths.lhs.heading fine Rightward]
		include : dispiro
			flat xBarEnd (yBar - Stroke + fine) [widths.lhs.heading fine Leftward]
			curl xBarStart (yBar - Stroke + fine) [widths.lhs.heading fine Leftward]
			archv 16
			g4 xLowerArcLeft (0.9 * [YSmoothMidL (yBar - Stroke) Stroke adaBot adbBot]) [widths.lhs]
			arcvh 16
			g4 [mix (xLowerArcLeft + [HSwToV Stroke]) (RightSB - [HSwToV Stroke]) 0.6] 0 [heading Rightward]
			archv 16
			g4.down.mid (RightSB - [HSwToV Stroke]) [mix Descender Stroke 0.5] [heading Downward]
			arcvh 16
			flat (RightSB - (Stroke - [mix Descender Stroke 0.5]) * 1.1) (Descender + Stroke)
			curl [Math.min (RightSB - (Stroke - [mix Descender Stroke 0.5]) * 1.1 - 1) [mix SB RightSB 0.5]] (Descender + Stroke)

	create-glyph 'grek/xi.flatTop' : glyph-proc
		include : MarkSet.bp
		include : LowerXiShape SHAPE-FLAT-TOP

	create-glyph 'grek/xi.rounded' : glyph-proc
		include : MarkSet.bp
		include : LowerXiShape SHAPE-ROUNDED

	select-variant 'grek/xi' 0x3BE
