import { getSupplemental } from '../../../cldr' ; import { parsePluralRule , CompactOrCondition } from '../../../parsing/parser.plural' ; const categories = { zero : 0 , one : 1 , two : 2 , few : 3 , many : 4 , other : 5 , } ; type StringMap = { [ x ] : string ; } ; let sequence = 0 ; const expressions = { } ; const exprId = ( o ) : number => { const e = JSON . stringify ( o ) ; let id = expressions [ e ] ; if ( id === undefined ) { id = sequence ++ ; expressions [ e ] = id ; } return id ; } ; const parseRule = ( lang , category , raw ) : CompactOrCondition => { const result = parsePluralRule ( raw ) ; if ( result . isNothing ( ) ) { throw new Error ( `Failure to parse plural rule for ${ lang } key ${ category }: ${ raw }` ) ; } const rule = result . get ( ) . _1 ; return rule . compact ( ) ; } ; const parseRules = ( pluralSet ) : StringMap => { const map = { } ; Object . keys ( pluralSet ) . forEach ( ( lang ) => { const obj = pluralSet [ lang ] ; map [ lang ] = Object . keys ( obj ) . filter ( ( k ) => k . split ( '-' ) [ 2 ] !== 'other' ) . map ( ( k ) => { const code = categories [ k . split ( '-' ) [ 2 ] ] ; const json = parseRule ( lang , k , obj [ k ] ) ; return [ code , json . map ( ( c ) => c . map ( exprId ) ) ] ; } ) ; } ) ; return map ; } ; const SPACER = /[\s,]+/ ; const IGNORE = new Set ( [ '@integer' , '@decimal' , '…' ] ) ; const parseSamples = ( pluralSet ) : StringMap => { const map = { } ; Object . keys ( pluralSet ) . forEach ( ( lang ) => { const obj = pluralSet [ lang ] ; map [ lang ] = Object . keys ( obj ) . map ( ( k ) => { const code = k . split ( '-' ) [ 2 ] ; const rule = parsePluralRule ( obj [ k ] ) . get ( ) . _1 ; const samples = rule . samples . split ( SPACER ) . filter ( ( s ) => ! IGNORE . has ( s ) ) ; return [ code , samples ] ; } ) ; } ) ; return map ; } ; export const getPlurals = ( ) : any => { const tree = getSupplemental ( ) ; const { Cardinals , Ordinals } = tree ; const cardinals = parseRules ( Cardinals ) ; const ordinals = parseRules ( Ordinals ) ; const exprs = Object . keys ( expressions ) . sort ( ( a , b ) => ( expressions [ a ] < expressions [ b ] ? - 1 : 1 ) ) ; const samples = { cardinals : parseSamples ( Cardinals ) , ordinals : parseSamples ( Ordinals ) , } ; return { cardinals , ordinals , expressions : exprs . map ( ( e ) => JSON . parse ( e ) ) , samples , } ; } ;