/* bloo.app — i18n: language and billing region come only from the URL path.
   The international root is always English/USD; /br/ is always pt-BR/BRL.
   Exposes window.I18n = { LocaleProvider, useLocale, useT }.
   Pages read copy via useT()'s t(en, pt) pair-picker
   rather than a separate key file — keeps each string's two language versions
   side by side for easy upkeep. Must load BEFORE shared.jsx and every page script. */
(function () {
  const SITE = window.BlooSiteConfig;
  if (!SITE) throw new Error('site-config.js must load before i18n.jsx');
  const LocaleCtx = React.createContext(['en', () => {}, SITE.regions.intl]);

  function regionFromPath() {
    return SITE.regions[SITE.regionKeyFromPath(window.location.pathname)];
  }

  function LocaleProvider({ children }) {
    const [region] = React.useState(regionFromPath);
    const locale = region.uiLocale;

    React.useEffect(() => {
      document.documentElement.lang = region.locale;
      document.documentElement.dataset.region = region.key;
    }, [region]);

    const value = React.useMemo(() => [locale, () => {}, region], [locale, region]);
    return <LocaleCtx.Provider value={value}>{children}</LocaleCtx.Provider>;
  }

  function useLocale() {
    return React.useContext(LocaleCtx);
  }

  /* const { t, locale } = useT();  t('English copy', 'Cópia em português') */
  function useT() {
    const [locale, , region] = React.useContext(LocaleCtx);
    const t = React.useCallback((en, pt) => (locale === 'pt' ? pt : en), [locale]);
    return { locale, region, t };
  }

  window.I18n = { LocaleProvider, useLocale, useT };
})();
