// i18n.jsx — global language state + t() helper.
// Two languages: 'ru' (default) and 'kz'.
// Persisted in localStorage. Components subscribe via useLang().

(function () {
  if (window.__gol_lang) return; // already initialised
  let stored = "ru";
  try { stored = localStorage.getItem("gol_lang") || "ru"; } catch (e) {}
  window.__gol_lang = stored === "kz" ? "kz" : "ru";
  window.__gol_lang_listeners = new Set();
  window.setLang = function (l) {
    if (l !== "ru" && l !== "kz") return;
    if (l === window.__gol_lang) return;
    window.__gol_lang = l;
    try { localStorage.setItem("gol_lang", l); } catch (e) {}
    window.__gol_lang_listeners.forEach((fn) => fn(l));
  };
})();

function useLang() {
  const [lang, setLangState] = React.useState(window.__gol_lang);
  React.useEffect(() => {
    const fn = (l) => setLangState(l);
    window.__gol_lang_listeners.add(fn);
    return () => window.__gol_lang_listeners.delete(fn);
  }, []);
  return [lang, window.setLang];
}

function t(ru, kz) {
  return window.__gol_lang === "kz" ? kz : ru;
}

window.useLang = useLang;
window.t = t;
