{"version":3,"file":"localeStore.mjs","names":[],"sources":["../../../src/web/stores/localeStore.ts"],"sourcesContent":["import {\n  canonicalLocale,\n  DEFAULT_LOCALE,\n  type LocaleContribution,\n  type LocaleTag,\n  matchLocale,\n  registerLocaleCatalog,\n  setLocale,\n} from '@agimon-ai/po-core/web';\n\nimport { allPluginLocales } from '../lib/pluginRegistry';\n\n/** The language the person picked; absent means follow the browser. */\nexport const LOCALE_STORAGE_KEY = 'po.web.locale';\n/** A copy of the picked pack's messages, so the first paint is in that language before plugins load. */\nconst SNAPSHOT_KEY = `${LOCALE_STORAGE_KEY}.snapshot`;\n\nlet removeSnapshot: (() => void) | undefined;\n\nfunction storage(): Storage | undefined {\n  try {\n    return window.localStorage;\n  } catch {\n    return undefined;\n  }\n}\n\n/** The remembered pick, or undefined when the page follows the browser. */\nexport function storedLocale(): LocaleTag | undefined {\n  try {\n    return canonicalLocale(storage()?.getItem(LOCALE_STORAGE_KEY));\n  } catch {\n    return undefined;\n  }\n}\n\n/** The snapshot is read back from localStorage, so it is checked like any pack before it is used. */\nfunction readSnapshot(tag: LocaleTag): LocaleContribution | undefined {\n  try {\n    const raw = storage()?.getItem(SNAPSHOT_KEY);\n    const value = raw ? (JSON.parse(raw) as Partial<LocaleContribution>) : undefined;\n    if (value?.name !== tag || typeof value.messages !== 'object' || value.messages === null) return undefined;\n    if (!Object.values(value.messages).every((message) => typeof message === 'string')) return undefined;\n    return value as LocaleContribution;\n  } catch {\n    return undefined;\n  }\n}\n\nfunction remember(locale: LocaleContribution | null): void {\n  try {\n    if (locale === null) {\n      storage()?.removeItem(LOCALE_STORAGE_KEY);\n      storage()?.removeItem(SNAPSHOT_KEY);\n      return;\n    }\n    storage()?.setItem(LOCALE_STORAGE_KEY, locale.name);\n    storage()?.setItem(SNAPSHOT_KEY, JSON.stringify(locale));\n  } catch (error) {\n    // Private mode or a full quota: the language still applies now, but the next first paint is English.\n    console.warn('Po could not remember the language', error);\n  }\n}\n\nfunction show(tag: LocaleTag): void {\n  setLocale(tag);\n  document.documentElement.lang = tag;\n}\n\nfunction browserLanguages(): readonly string[] {\n  return typeof navigator === 'undefined' ? [] : (navigator.languages ?? [navigator.language]);\n}\n\n/** The languages the picker offers: each pack the person's computers contribute. English is always offered beside them. */\nexport function availableLocales(): readonly LocaleContribution[] {\n  return allPluginLocales().map(({ locale }) => locale);\n}\n\n/** Puts the remembered language on the page before the first render, from its snapshot. */\nexport function applyStoredLocale(): void {\n  const stored = storedLocale();\n  const snapshot = stored === undefined ? undefined : readSnapshot(stored);\n  if (snapshot !== undefined) {\n    removeSnapshot?.();\n    removeSnapshot = registerLocaleCatalog(snapshot.name, snapshot.messages);\n  }\n  show(snapshot?.name ?? DEFAULT_LOCALE);\n}\n\n/**\n * Settles the language once plugins are installed: the picked pack's current messages replace the\n * snapshot, a pack that is gone is forgotten, and with no pick the browser's language is matched.\n */\nexport function reconcileStoredLocale(): void {\n  removeSnapshot?.();\n  removeSnapshot = undefined;\n  const locales = availableLocales();\n  const stored = storedLocale();\n  if (stored !== undefined && stored !== DEFAULT_LOCALE) {\n    const picked = locales.find((locale) => locale.name === stored);\n    remember(picked ?? null);\n    if (picked !== undefined) return show(picked.name);\n  }\n  if (stored === DEFAULT_LOCALE) return show(DEFAULT_LOCALE);\n  show(\n    matchLocale(\n      browserLanguages(),\n      locales.map((locale) => locale.name),\n    ) ?? DEFAULT_LOCALE,\n  );\n}\n\n/** Switches the language and remembers it. `null` goes back to following the browser. */\nexport function selectLocale(tag: LocaleTag | null): void {\n  if (tag === null) {\n    remember(null);\n    return reconcileStoredLocale();\n  }\n  if (tag === DEFAULT_LOCALE) {\n    try {\n      storage()?.setItem(LOCALE_STORAGE_KEY, DEFAULT_LOCALE);\n      storage()?.removeItem(SNAPSHOT_KEY);\n    } catch (error) {\n      console.warn('Po could not remember the language', error);\n    }\n    return show(DEFAULT_LOCALE);\n  }\n  const picked = availableLocales().find((locale) => locale.name === canonicalLocale(tag));\n  if (picked === undefined) return;\n  remember(picked);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