{"version":3,"file":"chunk-TranslationsContext.mjs","names":["dtLocaleEnUs","useTranslation","useI18NextTranslation"],"sources":["../../src/providers/ThemeProvider/TranslationsContext.ts"],"sourcesContent":["//\n// Copyright 2022 DXOS.org\n//\n\n// Per-locale entry, not `date-fns/locale`: that barrel re-exports every locale (~1MB of\n// source) for the one default used here.\nimport { enUS as dtLocaleEnUs } from 'date-fns/locale/en-US';\nimport { type i18n as I18n } from 'i18next';\nimport { createContext, useCallback, useContext, useMemo, useSyncExternalStore } from 'react';\nimport { useTranslation as useI18NextTranslation } from 'react-i18next';\n\n// Kept out of `TranslationsProvider.tsx`: react-refresh only fast-refreshes a module whose exports are all\n// components, so a context and its hook exported beside them force a full page reload on every edit.\n\nexport const initialNs = 'dxos-common';\nexport const initialDtLocale = dtLocaleEnUs;\n\nexport const TranslationsContext = createContext({\n  appNs: initialNs,\n  dtLocale: initialDtLocale,\n});\n\nexport const useTranslation = (...args: Parameters<typeof useI18NextTranslation>) => {\n  const result = useI18NextTranslation(...args);\n  const version = useResourceVersion(result.i18n);\n  // A fresh `t` per version, since compiled components memoize what they render on its identity.\n  const t = useMemo(() => result.t.bind(undefined), [result.t, version]);\n  const { dtLocale } = useContext(TranslationsContext);\n  return { ...result, t, dtLocale };\n};\n\n/**\n * Changes when bundles are added. Plugins add theirs as they activate, often after first render,\n * and react-i18next only listens from an effect, so it misses a bundle added before that effect runs.\n */\nconst useResourceVersion = (i18n: I18n | undefined): number => {\n  const subscribe = useCallback(\n    (onChange: () => void) => {\n      i18n?.store?.on('added', onChange);\n      return () => i18n?.store?.off('added', onChange);\n    },\n    [i18n],\n  );\n  // An instance that was never initialised has no store.\n  const getSnapshot = () => (i18n?.store ? resourceVersion(i18n) : 0);\n  return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n};\n\nconst versions = new WeakMap<I18n, { value: number }>();\n\nconst resourceVersion = (i18n: I18n): number => {\n  let version = versions.get(i18n);\n  if (!version) {\n    const counter = { value: 0 };\n    i18n.store.on('added', () => counter.value++);\n    versions.set(i18n, counter);\n    version = counter;\n  }\n  return version.value;\n};\n"],"mappings":";;;;AAcA,IAAa,YAAY;AACzB,IAAa,kBAAkBA;AAE/B,IAAa,sBAAsB,cAAc;CAC/C,OAAO;CACP,UAAU;AACZ,CAAC;AAED,IAAaC,oBAAkB,GAAG,SAAmD;CACnF,MAAM,SAASC,eAAsB,GAAG,IAAI;CAC5C,MAAM,UAAU,mBAAmB,OAAO,IAAI;CAE9C,MAAM,IAAI,cAAc,OAAO,EAAE,KAAK,KAAA,CAAS,GAAG,CAAC,OAAO,GAAG,OAAO,CAAC;CACrE,MAAM,EAAE,aAAa,WAAW,mBAAmB;CACnD,OAAO;EAAE,GAAG;EAAQ;EAAG;CAAS;AAClC;;;;;AAMA,IAAM,sBAAsB,SAAmC;CAC7D,MAAM,YAAY,aACf,aAAyB;EACxB,MAAM,OAAO,GAAG,SAAS,QAAQ;EACjC,aAAa,MAAM,OAAO,IAAI,SAAS,QAAQ;CACjD,GACA,CAAC,IAAI,CACP;CAEA,MAAM,oBAAqB,MAAM,QAAQ,gBAAgB,IAAI,IAAI;CACjE,OAAO,qBAAqB,WAAW,aAAa,WAAW;AACjE;AAEA,IAAM,2BAAW,IAAI,QAAiC;AAEtD,IAAM,mBAAmB,SAAuB;CAC9C,IAAI,UAAU,SAAS,IAAI,IAAI;CAC/B,IAAI,CAAC,SAAS;EACZ,MAAM,UAAU,EAAE,OAAO,EAAE;EAC3B,KAAK,MAAM,GAAG,eAAe,QAAQ,OAAO;EAC5C,SAAS,IAAI,MAAM,OAAO;EAC1B,UAAU;CACZ;CACA,OAAO,QAAQ;AACjB"}