/* Gridea site — language state (ET / EN).
   In-memory only — no cookie, no localStorage, so the "we store nothing" posture
   holds and no consent banner is needed. Default follows the browser language;
   a full page reload resets to that default, in-app navigation keeps the choice.
   Exposed as globals (window.useLang / setLang / getLang) so every script file
   can use them, matching the codebase's global-attach convention (see GIcon). */
(function () {
  const initial = (typeof navigator !== 'undefined' && (navigator.language || '').toLowerCase().startsWith('en')) ? 'en' : 'et';
  const store = { value: initial, subs: new Set() };

  // Hook: returns the current language and re-renders the component on change.
  function useLang() {
    const [, force] = React.useState(0);
    React.useEffect(() => {
      const cb = () => force((n) => n + 1);
      store.subs.add(cb);
      return () => store.subs.delete(cb);
    }, []);
    return store.value;
  }

  function setLang(v) {
    if ((v === 'et' || v === 'en') && v !== store.value) {
      store.value = v;
      store.subs.forEach((f) => f());
    }
  }

  window.useLang = useLang;
  window.setLang = setLang;
  window.getLang = () => store.value;
})();
