{"version":3,"sources":["../src/translationGeneration.ts","../src/compile-to-string.ts"],"sourcesContent":["import { getPluralKey } from './compile-to-string'\n\nfunction resolveSelect(\n  value: string | number | undefined | null,\n  options: Record<string, string | (() => string)>\n): string {\n  const v = value == null ? 'other' : String(value)\n  const handler = options[v] ?? options['other']\n\n  if (handler == null) return ''\n  return typeof handler === 'function' ? handler() : handler\n}\n\nfunction resolvePlural(\n  value: number,\n  options: Record<string, string | (() => string)>\n): string {\n  const key = getPluralKey(value)\n  const handler = options[key] ?? options['other']\n  if (handler == null) return ''\n  return typeof handler === 'function' ? handler() : handler\n}\n\nfunction round(value: number): string {\n  return String(Math.round(value))\n}\n\nexport const TranslationGen = {\n  resolveSelect,\n  resolvePlural,\n  round,\n}\n","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;;;ACEO,SAAS,aAAa,KAAc;AACzC,SAAO,QAAQ,IAAI,OACf,QAAQ,IAAI,OACV,QAAQ,IAAI,OACV,MAAM,KAAK,MAAM,IAAI,QACnB,OAAO,IAAI,SAAS;AAChC;;;ADNA,SAAS,cACP,OACA,SACQ;AACR,QAAM,IAAI,SAAS,OAAO,UAAU,OAAO,KAAK;AAChD,QAAM,UAAU,QAAQ,CAAC,KAAK,QAAQ,OAAO;AAE7C,MAAI,WAAW,KAAM,QAAO;AAC5B,SAAO,OAAO,YAAY,aAAa,QAAQ,IAAI;AACrD;AAEA,SAAS,cACP,OACA,SACQ;AACR,QAAM,MAAM,aAAa,KAAK;AAC9B,QAAM,UAAU,QAAQ,GAAG,KAAK,QAAQ,OAAO;AAC/C,MAAI,WAAW,KAAM,QAAO;AAC5B,SAAO,OAAO,YAAY,aAAa,QAAQ,IAAI;AACrD;AAEA,SAAS,MAAM,OAAuB;AACpC,SAAO,OAAO,KAAK,MAAM,KAAK,CAAC;AACjC;AAEO,IAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AACF;","names":[]}