/* chrome.jsx — lang context, brand Nav + Footer, logo, icons. Reuses site.css classes. */

const RetoLangCtx = React.createContext({ lang: "es", t: RETO_I18N.es, setLang: () => {} });
/* initialLang lets a parent app force the locale explicitly (e.g. the
   main aguilera-ingenieros site sets it from its own AILangProvider so
   ES/EN stays in sync). Falls back to localStorage when not provided.
   The parent also re-keys this whole subtree on lang change, so the
   initial-state lazy init below picks up the new value cleanly. */
const RetoLangProvider = ({ children, initialLang }) => {
  const [lang, setLangState] = React.useState(() => {
    if (initialLang === "es" || initialLang === "en") return initialLang;
    const s = localStorage.getItem("reto_lang");
    return (s === "es" || s === "en") ? s : "es";
  });
  const setLang = React.useCallback((l) => {
    setLangState(l); localStorage.setItem("reto_lang", l); document.documentElement.lang = l;
  }, []);
  React.useEffect(() => { document.documentElement.lang = lang; }, [lang]);
  const value = React.useMemo(() => ({ lang, setLang, t: RETO_I18N[lang] }), [lang, setLang]);
  return <RetoLangCtx.Provider value={value}>{children}</RetoLangCtx.Provider>;
};
const useReto = () => React.useContext(RetoLangCtx);

const RIcon = {
  Menu:  ({ s = 22, c = "#F5F5F3" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.6"><path d="M3 6h18M3 12h18M3 18h18" /></svg>,
  Close: ({ s = 24, c = "#F5F5F3" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.6"><path d="M6 6l12 12M18 6l-12 12" /></svg>,
  Arrow: ({ s = 18, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5"><path d="M5 12h14M13 5l7 7-7 7" /></svg>,
  Check: ({ s = 18, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8"><path d="M20 6 9 17l-5-5" /></svg>,
  Copy:  ({ s = 15, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5"><rect x="9" y="9" width="11" height="11" rx="1" /><path d="M5 15V5a1 1 0 0 1 1-1h10" /></svg>,
  Expand:({ s = 16, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5"><path d="M8 3H5a2 2 0 0 0-2 2v3M16 3h3a2 2 0 0 1 2 2v3M8 21H5a2 2 0 0 1-2-2v-3M16 21h3a2 2 0 0 0 2-2v-3" /></svg>,
  Shrink:({ s = 16, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5"><path d="M4 8h3a1 1 0 0 0 1-1V4M20 8h-3a1 1 0 0 1-1-1V4M4 16h3a1 1 0 0 1 1 1v3M20 16h-3a1 1 0 0 0-1 1v3" /></svg>,
  Lock:  ({ s = 15, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5"><rect x="4" y="11" width="16" height="9" rx="1" /><path d="M8 11V7a4 4 0 0 1 8 0v4" /></svg>,
  Bolt:  ({ s = 16, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5" strokeLinejoin="round"><path d="M13 2 4 14h6l-1 8 9-12h-6l1-8z" /></svg>,
  Drop:  ({ s = 14, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5" strokeLinejoin="round"><path d="M12 3s6 6.5 6 10.5a6 6 0 0 1-12 0C6 9.5 12 3 12 3z" /></svg>,
  Air:   ({ s = 14, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5" strokeLinecap="round"><path d="M3 8h11a3 3 0 1 0-3-3M3 16h14a3 3 0 1 1-3 3M3 12h18" /></svg>,
  Plus:  ({ s = 18, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5"><path d="M12 5v14M5 12h14" /></svg>,
  Alert: ({ s = 18, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.6" strokeLinecap="round"><path d="M12 9v4M12 17h.01M10.3 3.9 1.8 18a2 2 0 0 0 1.7 3h17a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0z" /></svg>,
  Info:  ({ s = 14, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.5" strokeLinecap="round"><circle cx="12" cy="12" r="9" /><path d="M12 11v5M12 8h.01" /></svg>,
  LinkedIn: ({ s = 16, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill={c}><path d="M4.98 3.5C4.98 4.88 3.87 6 2.5 6S0 4.88 0 3.5 1.12 1 2.5 1s2.48 1.12 2.48 2.5zM0 8h5v16H0zm7.5 0h4.78v2.2h.07c.67-1.2 2.3-2.47 4.74-2.47 5.07 0 6 3.34 6 7.68V24h-5v-7.1c0-1.7-.03-3.88-2.37-3.88-2.37 0-2.73 1.85-2.73 3.75V24h-5z" /></svg>,
  Vimeo: ({ s = 16, c = "currentColor" }) => <svg width={s} height={s} viewBox="0 0 24 24" fill={c}><path d="M23.5 6.2a3 3 0 0 0-2.1-2.1C19.5 3.5 12 3.5 12 3.5s-7.5 0-9.4.6A3 3 0 0 0 .5 6.2C0 8.1 0 12 0 12s0 3.9.5 5.8a3 3 0 0 0 2.1 2.1c1.9.6 9.4.6 9.4.6s7.5 0 9.4-.6a3 3 0 0 0 2.1-2.1c.5-1.9.5-5.8.5-5.8s0-3.9-.5-5.8zM9.6 15.6V8.4l6.3 3.6-6.3 3.6z" /></svg>,
};

const RetoLogo = ({ size = 30 }) => {
  const h = size * 1.6, w = Math.round(h * (1055 / 467));
  const src = (window.__resources && window.__resources.aguileraLogo) || "assets/logo_main_dark.png";
  return <img src={src} alt="Aguilera Ingenieros" width={w} height={Math.round(h)}
    style={{ display: "block", height: h + "px", width: w + "px", maxWidth: "none", flex: "none", aspectRatio: "1055 / 467", objectFit: "contain" }} />;
};

/* RetoLangToggle — game-scoped lang toggle, separate from the main
   site's LangToggle. Named distinctly so it doesn't collide on the
   `window.LangToggle` global; before this rename, both this file and
   components/core.jsx wrote a `LangToggle` to window — chrome.jsx
   loads second so its useReto()-bound version was overwriting the
   site one, which made the main Nav's lang toggle a no-op outside
   the game (useReto called outside RetoLangProvider returns a default
   ctx whose setLang is `() => {}`). */
const RetoLangToggle = () => {
  const { lang, setLang } = useReto();
  return (
    <div className="lang-toggle lang-toggle--dark">
      {["es", "en"].map(l => (
        <span key={l} className={l === lang ? "on" : ""} onClick={() => setLang(l)}>{l.toUpperCase()}</span>
      ))}
    </div>
  );
};

const RetoNav = () => {
  const { t } = useReto();
  const [scrolled, setScrolled] = React.useState(false);
  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 60);
    window.addEventListener("scroll", onScroll); onScroll();
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
  return (
    <nav className={"ai-nav " + (scrolled ? "ai-nav--solid" : "")}>
      <div className="ai-nav__inner">
        <a className="ai-nav__logo"><RetoLogo size={30} /></a>
        <ul className="ai-nav__links">
          {t.nav.map(n => (
            <li key={n.id}>
              <a className={"ai-nav__link " + (n.id === "talento" ? "is-active" : "")}>{n.label}</a>
            </li>
          ))}
        </ul>
        <div className="ai-nav__right">
          <RetoLangToggle />
        </div>
      </div>
    </nav>
  );
};

const RetoFooter = () => {
  const { t } = useReto();
  return (
    <footer className="ai-footer reto-footer">
      <div className="ai-footer__inner reto-footer__inner">
        <div className="ai-footer__col ai-footer__col--brand">
          <RetoLogo size={28} />
          <div className="ai-footer__meta">{t.footer.meta}</div>
          <div className="reto-footer__tag">{t.footer.tagline}</div>
        </div>
        <div className="ai-footer__col reto-footer__contact">
          <div className="ai-footer__head">Aguilera Ingenieros</div>
          <div className="ai-footer__addr">{t.footer.addr}</div>
          <a href="mailto:sec@aguilera-ingenieros.com">sec@aguilera-ingenieros.com</a>
          <div className="reto-footer__icons">
            <a className="ai-footer__ic" aria-label="LinkedIn"><RIcon.LinkedIn /></a>
            <a className="ai-footer__ic" aria-label="Vimeo"><RIcon.Vimeo /></a>
          </div>
        </div>
      </div>
      <div className="ai-footer__bottom"><span>{t.footer.rights}</span></div>
    </footer>
  );
};

Object.assign(window, { RetoLangProvider, useReto, RIcon, RetoLogo, RetoLangToggle, RetoNav, RetoFooter });
