/* ANPA Quintán — i18n (galego / castelán) */
let LANG = (typeof localStorage !== 'undefined' && localStorage.getItem('anpa-lang')) || 'gl';
function getLang() { return LANG; }
function setLang(l) {
  LANG = l;
  try { localStorage.setItem('anpa-lang', l); } catch (e) {}
  try { document.documentElement.lang = (l === 'es' ? 'es' : 'gl'); } catch (e) {}
  window.dispatchEvent(new Event('anpa-lang'));
}
/* t('texto galego', 'texto castelán') */
function t(gl, es) { return LANG === 'es' ? es : gl; }

/* hook: re-renderiza ao cambiar de idioma */
function useLang() {
  const [, force] = React.useState(0);
  React.useEffect(() => {
    const on = () => force(x => x + 1);
    window.addEventListener('anpa-lang', on);
    return () => window.removeEventListener('anpa-lang', on);
  }, []);
  return { lang: LANG, setLang, t };
}

/* meses para datas das novas */
const MONTHS = {
  gl: ['xan', 'feb', 'mar', 'abr', 'maio', 'xuño', 'xul', 'ago', 'set', 'out', 'nov', 'dec'],
  es: ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic'],
};
function fmtDate(iso) {
  const d = new Date(iso);
  if (isNaN(d)) return '';
  return `${d.getDate()} ${MONTHS[LANG === 'es' ? 'es' : 'gl'][d.getMonth()]} ${d.getFullYear()}`;
}

/* banderitas SVG */
function FlagES({ size = 20 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" style={{ borderRadius: 4, display: 'block' }} aria-hidden="true">
      <rect width="24" height="24" fill="#c60b1e" />
      <rect y="6" width="24" height="12" fill="#ffc400" />
    </svg>
  );
}
function FlagGL({ size = 20 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" style={{ borderRadius: 4, display: 'block' }} aria-hidden="true">
      <rect width="24" height="24" fill="#fff" />
      <path d="M0 2 L22 24 L24 24 L24 22 L2 0 Z" fill="#0080c8" />
    </svg>
  );
}

/* selector de idioma (dúas banderitas) */
function LangToggle({ compact }) {
  const { lang, setLang } = useLang();
  const opt = (code, Flag, label) => {
    const on = lang === code;
    return (
      <button onClick={() => setLang(code)} aria-label={label} aria-pressed={on} title={label}
        style={{
          display: 'flex', alignItems: 'center', gap: 6, cursor: 'pointer',
          border: '1.5px solid ' + (on ? 'var(--blue-700)' : 'var(--line)'),
          background: on ? 'var(--blue-50)' : 'var(--paper)',
          borderRadius: 9, padding: compact ? '5px 8px' : '6px 9px', lineHeight: 0,
          opacity: on ? 1 : .62, transition: 'opacity .12s, border-color .12s',
        }}>
        <Flag size={compact ? 18 : 19} />
        <span style={{ fontSize: 12.5, fontWeight: 800, color: on ? 'var(--blue-700)' : 'var(--ink-mute)', letterSpacing: '.03em' }}>{code.toUpperCase()}</span>
      </button>
    );
  };
  return (
    <div style={{ display: 'flex', gap: 6, alignItems: 'center' }}>
      {opt('gl', FlagGL, 'Galego')}
      {opt('es', FlagES, 'Castellano')}
    </div>
  );
}

Object.assign(window, { getLang, setLang, t, useLang, fmtDate, FlagES, FlagGL, LangToggle });
