{"version":3,"file":"formatChartValue.cjs","names":["_resolveChartLocale","require","numberFormatOptionsByPreset","compact","notation","decimal","percent","style","formatChartValue","format","languageCode","value","Intl","NumberFormat","resolveChartLocale","exports"],"sources":["../../../../src/charts/utils/formatChartValue.ts"],"sourcesContent":["/*!\n * Copyright (c) 2026-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n */\n\nimport { resolveChartLocale } from \"./resolveChartLocale.js\";\n\n/**\n * The `Intl.NumberFormat` options for each preset name. The preset union type {@link ChartValueFormat} takes its members from the keys of this object.\n * This keeps the two lists in agreement. If a name exists in the union but not here, it resolves to `undefined` options. The value then formats as a plain decimal. The code gives no error.\n */\nconst numberFormatOptionsByPreset = {\n  compact: { notation: \"compact\" },\n  decimal: {},\n  percent: { style: \"percent\" },\n} as const satisfies Record<string, Intl.NumberFormatOptions>;\n\n/**\n * The named presets accepted by {@link ChartValueFormat}.\n *\n * - `\"decimal\"` renders the value in full, for example `1,234.5`.\n * - `\"compact\"` shortens large values, for example `1.2K`.\n * - `\"percent\"` multiplies the value by 100 and adds a percent sign. For example, `0.4567` renders as `46%`. Supply the value as a fraction, not as a percentage that is already scaled to 100. If the value is already scaled, the preset scales it twice. For example, `42` renders as `4,200%`.\n */\nexport type ChartValueFormatPreset = keyof typeof numberFormatOptionsByPreset;\n\n/**\n * A chart renders a value in this currency.\n */\nexport type ChartValueCurrencyFormat = {\n  /**\n   * An ISO 4217 currency code such as `\"USD\"` or `\"JPY\"`.\n   */\n  currency: string;\n  style: \"currency\";\n};\n\n/**\n * A chart value format is one of three kinds: a named preset, an explicit currency, or a function. A function takes the raw number and returns a string. A function gives the caller full control over the presentation.\n *\n * The presets are a closed set. They do not use the general type `Intl.NumberFormatOptions`. That type allows combinations that fail when the code builds them. For example, `{ style: \"currency\" }` without a `currency` field passes the type check. However, it raises a `TypeError` at construction. A caller that needs options outside the preset set can pass a function instead.\n */\nexport type ChartValueFormat =\n  | ChartValueFormatPreset\n  | ChartValueCurrencyFormat\n  | ((value: number) => string);\n\n/**\n * Props for {@link formatChartValue}.\n */\nexport type FormatChartValueProps = {\n  /**\n   * How to format the value.\n   * @default \"decimal\"\n   */\n  format?: ChartValueFormat;\n  /**\n   * A language code from the Odyssey translation context, such as `\"en\"` or `\"pt_BR\"`. The function converts the language code to a BCP 47 tag before the value reaches `Intl`.\n   */\n  languageCode: string;\n  /** The number to format. */\n  value: number;\n};\n\n/**\n * Formats a single chart value into a locale aware string.\n *\n * When `format` is a function, the code calls it with `value`. The code returns the result directly and ignores `languageCode`. A caller that supplies a function controls the full presentation of the value. Otherwise, the function formats the value with an `Intl.NumberFormat` instance. It builds the instance from the resolved locale and the requested preset or currency.\n */\nexport const formatChartValue = ({\n  format = \"decimal\",\n  languageCode,\n  value,\n}: FormatChartValueProps): string => {\n  if (typeof format === \"function\") {\n    return format(value);\n  }\n\n  return new Intl.NumberFormat(\n    resolveChartLocale(languageCode),\n    typeof format === \"string\" ? numberFormatOptionsByPreset[format] : format,\n  ).format(value);\n};\n"],"mappings":";;;;;;AAYA,IAAAA,mBAAA,GAAAC,OAAA;AAZA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAQA,MAAMC,2BAA2B,GAAG;EAClCC,OAAO,EAAE;IAAEC,QAAQ,EAAE;EAAU,CAAC;EAChCC,OAAO,EAAE,CAAC,CAAC;EACXC,OAAO,EAAE;IAAEC,KAAK,EAAE;EAAU;AAC9B,CAA6D;AAsDtD,MAAMC,gBAAgB,GAAGA,CAAC;EAC/BC,MAAM,GAAG,SAAS;EAClBC,YAAY;EACZC;AACqB,CAAC,KAAa;EACnC,IAAI,OAAOF,MAAM,KAAK,UAAU,EAAE;IAChC,OAAOA,MAAM,CAACE,KAAK,CAAC;EACtB;EAEA,OAAO,IAAIC,IAAI,CAACC,YAAY,CAC1B,IAAAC,sCAAkB,EAACJ,YAAY,CAAC,EAChC,OAAOD,MAAM,KAAK,QAAQ,GAAGP,2BAA2B,CAACO,MAAM,CAAC,GAAGA,MACrE,CAAC,CAACA,MAAM,CAACE,KAAK,CAAC;AACjB,CAAC;AAACI,OAAA,CAAAP,gBAAA,GAAAA,gBAAA","ignoreList":[]}