{"version":3,"file":"intl.cjs","names":["internationalization"],"sources":["../../../src/utils/intl.ts"],"sourcesContent":["/**\n * Cached Intl helper – drop‑in replacement for the global `Intl` object.\n * ‑‑‑\n * • Uses a `Proxy` to lazily wrap every *constructor* hanging off `Intl` (NumberFormat, DateTimeFormat, …).\n * • Each wrapped constructor keeps an in‑memory cache keyed by `[locales, options]` so that identical requests\n * reuse the same heavy instance instead of reparsing CLDR data every time.\n * • A polyfill warning for `Intl.DisplayNames` is emitted only once and only in dev.\n * • The public API is fully type‑safe and mirrors the native `Intl` surface exactly –\n * you can treat `CachedIntl` just like the built‑in `Intl`.\n *\n * Usage @example:\n * ---------------\n * ```ts\n * import { CachedIntl } from \"./cached-intl\";\n *\n * const nf = CachedIntl.NumberFormat(\"en-US\", { style: \"currency\", currency: \"USD\" });\n * console.log(nf.format(1234));\n *\n * const dn = CachedIntl.DisplayNames([\"fr\"], { type: \"language\" });\n * console.log(dn.of(\"en\")); * → \"anglais\"\n *\n * You can also spin up an isolated instance with its own caches (handy in test suites):\n * const TestIntl = createCachedIntl();\n * ```\n */\n\nimport { internationalization } from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\n\nconst MAX_CACHE_SIZE = 50;\nconst cache = new Map<any, Map<string, any>>();\n\ntype IntlConstructors = {\n  [K in keyof typeof Intl as (typeof Intl)[K] extends new (\n    ...args: any\n  ) => any\n    ? K\n    : never]: (typeof Intl)[K];\n};\n\ntype ReplaceLocaleWithLocalesValues<T> = T extends new (\n  locales: any,\n  options?: infer Options\n) => infer Instance\n  ? {\n      new (locales?: LocalesValues, options?: Options): Instance;\n      new (options?: Options & { locale?: LocalesValues }): Instance;\n      (locales?: LocalesValues, options?: Options): Instance;\n      (options?: Options & { locale?: LocalesValues }): Instance;\n    }\n  : T extends new (\n        locales: any\n      ) => infer Instance\n    ? {\n        new (locales?: LocalesValues): Instance;\n        new (options?: { locale?: LocalesValues }): Instance;\n        (locales?: LocalesValues): Instance;\n        (options?: { locale?: LocalesValues }): Instance;\n      }\n    : T;\n\nexport type WrappedIntl = {\n  [K in keyof typeof Intl]: K extends keyof IntlConstructors\n    ? ReplaceLocaleWithLocalesValues<(typeof Intl)[K]>\n    : (typeof Intl)[K];\n};\n\n/**\n * Generic caching instantiator for Intl constructors.\n */\nexport const getCachedIntl = <T extends new (...args: any[]) => any>(\n  Ctor: T,\n  locale?: LocalesValues | string,\n  options?: any\n): InstanceType<T> => {\n  const resLoc = locale ?? internationalization?.defaultLocale;\n\n  const optKey = options ? JSON.stringify(options) : '';\n  const key = `${resLoc}|${optKey}`;\n\n  let ctorCache = cache.get(Ctor);\n\n  if (!ctorCache) {\n    ctorCache = new Map();\n    cache.set(Ctor, ctorCache);\n  }\n\n  let instance = ctorCache.get(key);\n\n  if (!instance) {\n    if (ctorCache.size > MAX_CACHE_SIZE) ctorCache.clear();\n    instance = new Ctor(resLoc, options);\n    ctorCache.set(key, instance);\n  }\n  return instance;\n};\n\n/**\n * Optional: Keep bindIntl if your library exports it publicly.\n * It now uses the much smaller getCachedIntl under the hood.\n */\nexport const bindIntl = (boundLocale: LocalesValues): WrappedIntl => {\n  const bindWrap = (Ctor: any) =>\n    // function is used as a constructor, do not change in arrow function\n    function intlConstructor(locales?: any, options?: any) {\n      const isOptsFirst =\n        locales !== null &&\n        typeof locales === 'object' &&\n        !Array.isArray(locales);\n      const resOpts = isOptsFirst ? locales : options;\n      const resLoc = isOptsFirst\n        ? (resOpts as any).locale || boundLocale\n        : locales || boundLocale;\n\n      return getCachedIntl(Ctor, resLoc, resOpts);\n    };\n\n  return {\n    ...Intl,\n    Collator: bindWrap(Intl.Collator),\n    DateTimeFormat: bindWrap(Intl.DateTimeFormat),\n    DisplayNames: bindWrap(Intl.DisplayNames),\n    ListFormat: bindWrap(Intl.ListFormat),\n    NumberFormat: bindWrap(Intl.NumberFormat),\n    PluralRules: bindWrap(Intl.PluralRules),\n    RelativeTimeFormat: bindWrap(Intl.RelativeTimeFormat),\n    Locale: bindWrap(Intl.Locale),\n    Segmenter: bindWrap((Intl as any).Segmenter),\n  } as unknown as WrappedIntl;\n};\n\n// Add this to the bottom of utils/intl.ts ONLY if required for public API compatibility.\nexport const CachedIntl = {\n  // function is used as a constructor, do not change in arrow function\n  Collator: function Collator(locales?: any, options?: any) {\n    return getCachedIntl(Intl.Collator, locales, options);\n  },\n  DateTimeFormat: function DateTimeFormat(locales?: any, options?: any) {\n    return getCachedIntl(Intl.DateTimeFormat, locales, options);\n  },\n  DisplayNames: function DisplayNames(locales?: any, options?: any) {\n    return getCachedIntl(Intl.DisplayNames, locales, options);\n  },\n  ListFormat: function ListFormat(locales?: any, options?: any) {\n    return getCachedIntl(Intl.ListFormat as any, locales, options);\n  },\n  NumberFormat: function NumberFormat(locales?: any, options?: any) {\n    return getCachedIntl(Intl.NumberFormat, locales, options);\n  },\n  PluralRules: function PluralRules(locales?: any, options?: any) {\n    return getCachedIntl(Intl.PluralRules, locales, options);\n  },\n  RelativeTimeFormat: function RelativeTimeFormat(\n    locales?: any,\n    options?: any\n  ) {\n    return getCachedIntl(Intl.RelativeTimeFormat, locales, options);\n  },\n  Segmenter: function Segmenter(locales?: any, options?: any) {\n    return getCachedIntl((Intl as any).Segmenter, locales, options);\n  },\n} as any; // Cast to 'any' internally to avoid TS readonly errors\n\nexport { CachedIntl as Intl };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAM,iBAAiB;AACvB,MAAM,wBAAQ,IAAI,KAA4B;;;;AAwC9C,MAAa,iBACX,MACA,QACA,YACoB;CACpB,MAAM,SAAS,UAAUA,6CAAsB;CAG/C,MAAM,MAAM,GAAG,OAAO,GADP,UAAU,KAAK,UAAU,QAAQ,GAAG;CAGnD,IAAI,YAAY,MAAM,IAAI,KAAK;AAE/B,KAAI,CAAC,WAAW;AACd,8BAAY,IAAI,KAAK;AACrB,QAAM,IAAI,MAAM,UAAU;;CAG5B,IAAI,WAAW,UAAU,IAAI,IAAI;AAEjC,KAAI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