{"version":3,"sources":["../src/compile-to-string.ts"],"sourcesContent":["import type { ICUASTNode } from '@/src/parse'\n\nexport function getPluralKey(num: number)  {\n  return num === 0 ? '=0' :\n      num === 1 ? '=1' :\n        num === 2 ? '=2' :\n          num > 2 && num < 5 ? 'few' :\n            num >= 5 ? 'many' : 'other'\n}\n\ntype CompilerContext = {\n  hashtagReplacer?: number,\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ICUCompilerValues = Record<string, any>\n\n/**\n * Compiles a ICUASTNode tree to a string\n * @param node Tree to compile\n * @param values The values that replace the variable in the tree\n * @param context The context values of previous compile iterations\n */\nexport function compileToString(node: ICUASTNode, values: ICUCompilerValues, context: CompilerContext = {}): string {\n  switch (node.type) {\n    case 'Node': {\n      return node.parts.map(p => compileToString(p, values, context)).join('')\n    }\n    case 'Text':\n      return node.value\n    case 'SimpleReplace': {\n      const name = node.variableName\n      if (values && values[name] !== undefined) return String(values[name])\n      console.warn(`ICU Compile: missing value for ${name}`)\n      return `{${name}}`\n    }\n    case 'OptionReplace': {\n      const name = node.variableName\n      const operation = node.operatorName\n      const val = values ? values[name] : undefined\n      switch (operation) {\n        case 'plural': {\n          const num = Number(val)\n          if (isNaN(num)) {\n            console.warn(`ICU Compile: plural expected numeric value for ${name}, got ${val}`)\n            return `{${name}}`\n          }\n          const pluralKey = getPluralKey(num)\n\n          const chosen = node.options[pluralKey] ?? node.options['other']\n          if (!chosen) {\n            console.warn(`ICU Compile: plural for ${name} could not find key ${pluralKey} and no other`)\n            return `{${name}}`\n          }\n          return compileToString(chosen, values, { ...context, hashtagReplacer: num })\n        }\n        case 'select': {\n          if (val === undefined) {\n            console.warn(`ICU Compile: missing value for select ${name}`)\n            const other = node.options['other']\n            return other ? compileToString(other, values, context) : `{${name}}`\n          }\n          const chosen = node.options[String(val)] ?? node.options['other']\n          if (!chosen) {\n            console.warn(`ICU Compile: select ${name} chose undefined option \"${val}\" and no \"other\" provided`)\n            return `{${name}}`\n          }\n          return compileToString(chosen, values, context)\n        }\n        default: {\n          return `{${name}, ${operation}}`\n        }\n      }\n    }\n    case 'NumberField': {\n      if (context.hashtagReplacer !== undefined) {\n        return `${context.hashtagReplacer}`\n      } else {\n        return '{#}'\n      }\n    }\n  }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,SAAS,aAAa,KAAc;AACzC,SAAO,QAAQ,IAAI,OACf,QAAQ,IAAI,OACV,QAAQ,IAAI,OACV,MAAM,KAAK,MAAM,IAAI,QACnB,OAAO,IAAI,SAAS;AAChC;AAeO,SAAS,gBAAgB,MAAkB,QAA2B,UAA2B,CAAC,GAAW;AAClH,UAAQ,KAAK,MAAM;AAAA,IACjB,KAAK,QAAQ;AACX,aAAO,KAAK,MAAM,IAAI,OAAK,gBAAgB,GAAG,QAAQ,OAAO,CAAC,EAAE,KAAK,EAAE;AAAA,IACzE;AAAA,IACA,KAAK;AACH,aAAO,KAAK;AAAA,IACd,KAAK,iBAAiB;AACpB,YAAM,OAAO,KAAK;AAClB,UAAI,UAAU,OAAO,IAAI,MAAM,OAAW,QAAO,OAAO,OAAO,IAAI,CAAC;AACpE,cAAQ,KAAK,kCAAkC,IAAI,EAAE;AACrD,aAAO,IAAI,IAAI;AAAA,IACjB;AAAA,IACA,KAAK,iBAAiB;AACpB,YAAM,OAAO,KAAK;AAClB,YAAM,YAAY,KAAK;AACvB,YAAM,MAAM,SAAS,OAAO,IAAI,IAAI;AACpC,cAAQ,WAAW;AAAA,QACjB,KAAK,UAAU;AACb,gBAAM,MAAM,OAAO,GAAG;AACtB,cAAI,MAAM,GAAG,GAAG;AACd,oBAAQ,KAAK,kDAAkD,IAAI,SAAS,GAAG,EAAE;AACjF,mBAAO,IAAI,IAAI;AAAA,UACjB;AACA,gBAAM,YAAY,aAAa,GAAG;AAElC,gBAAM,SAAS,KAAK,QAAQ,SAAS,KAAK,KAAK,QAAQ,OAAO;AAC9D,cAAI,CAAC,QAA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