// i18n.jsx — language context + toggle
const LangCtx = React.createContext('en');
function useLang() { return React.useContext(LangCtx); }
function useT() {
  const l = React.useContext(LangCtx);
  return React.useCallback((en, zh, ...vals) => {
    let s = en;
    if (l === 'zh' && zh != null) s = zh;
    else if (l === 'es') { const es = (window.ES || {})[en]; if (es != null) s = es; }
    vals.forEach((v) => { s = s.replace('%s', v); });
    return s;
  }, [l]);
}
const LANG_LABELS = [['en', 'EN', 'English'], ['zh', '中文', '中文'], ['es', 'ES', 'Español']];
function currentFile() {
  const f = location.pathname.split('/').pop();
  if (f && f.endsWith('.html')) return f;
  const routeFiles = {
    '/': 'index.html', '/about': 'about-us.html', '/programs': 'programs.html',
    '/tuition': 'tuition.html', '/advantage': 'advantage.html', '/safety': 'safety-facilities.html',
    '/gallery': 'gallery.html', '/faqs': 'faqs.html', '/tour': 'book-a-tour.html',
  };
  return routeFiles[window.PAGE || '/'] || 'index.html';
}
function LangToggle({ lang, dark }) {
  const file = currentFile();
  return (
    <div className="langtoggle" data-dark={dark ? '1' : undefined}>
      {LANG_LABELS.map(([k, label, full]) => (
        k === lang
          ? <span key={k} data-on="1" aria-current="true">{label}</span>
          : <a key={k} href={'../' + k + '/' + file} hrefLang={k === 'zh' ? 'zh-Hans' : k} title={full}>{label}</a>
      ))}
    </div>
  );
}
Object.assign(window, { LangCtx, useLang, useT, LangToggle, currentFile });
