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

export : define [buildGsubThousands gsub para] : begin
	local rec : gsub.beginBlock

	define Thousand : gsub.addCommonFeature : gsub.createFeature 'THND'
	define {chain-rule reverse-rule} : gsub.ChainRuleBuilder
	define numberGlyphIDs {
		'zero.lnum' 'one.lnum' 'two.lnum' 'three.lnum' 'four.lnum'
		'five.lnum' 'six.lnum' 'seven.lnum' 'eight.lnum' 'nine.lnum'
	}

	define [nd s] : numberGlyphIDs.map : lambda [x] "\(x).nd\(s)"

	define a : Thousand.addLookup : gsub.createLookup : object
		.type 'gsub_chaining'
		.rules : list
			chain-rule ({'period'} ~> null) (numberGlyphIDs ~> [nd 2]) (numberGlyphIDs ~> null) (numberGlyphIDs ~> null)
			chain-rule ([nd 2] ~> null) (numberGlyphIDs ~> [nd 1])
			chain-rule ([nd 1] ~> null) (numberGlyphIDs ~> [nd 6])
			chain-rule ([nd 6] ~> null) (numberGlyphIDs ~> [nd 5])
			chain-rule ([nd 5] ~> null) (numberGlyphIDs ~> [nd 4])
			chain-rule ([nd 4] ~> null) (numberGlyphIDs ~> [nd 3])
			chain-rule ([nd 3] ~> null) (numberGlyphIDs ~> [nd 2])

	define b : Thousand.addLookup : gsub.createLookup : object
		.type 'gsub_chaining'
		.rules : list
			chain-rule (numberGlyphIDs ~> [nd 0]) (numberGlyphIDs ~> null) (numberGlyphIDs ~> null) (numberGlyphIDs ~> null)
			chain-rule ([nd 0] ~> null) (numberGlyphIDs ~> [nd 0])

	define c : Thousand.addLookup : gsub.createLookup : object
		.type 'gsub_reverse'
		.rules : list
			reverse-rule ([nd 0] ~> [nd 1]) ([nd 0] ~>  null)
			reverse-rule ([nd 0] ~> [nd 2]) ([nd 1] ~>  null)
			reverse-rule ([nd 0] ~> [nd 3]) ([nd 2] ~>  null)
			reverse-rule ([nd 0] ~> [nd 4]) ([nd 3] ~>  null)
			reverse-rule ([nd 0] ~> [nd 5]) ([nd 4] ~>  null)
			reverse-rule ([nd 0] ~> [nd 6]) ([nd 5] ~>  null)
			reverse-rule ([nd 0] ~> [nd 1]) ([nd 6] ~>  null)

	gsub.setDependency a b
	gsub.setDependency b c

	gsub.endBlock rec
