/* ============================================================
   components-core.jsx — shared across all pages
   Constants · useToast · useBooking · Arrow · Nav · Hero · Footer
   ============================================================ */
const { useState, useEffect, useRef, useCallback } = React;

const TOPMATE  = "https://topmate.io/abhileen_singh_saluja";
const WHATSAPP = "https://api.whatsapp.com/send?phone=971567453636";
const LINKEDIN = "https://www.linkedin.com/in/abhileen-singh-saluja-078623141/?skipRedirect=true";
const EMAIL    = "hi@thathyperactivesardar.com";

/* Newsletter target — leave "" to capture in-page (local) until the real
   Substack/Beehiiv URL is wired in. When set, the form POSTs/links there. */
const NEWSLETTER_ACTION = "";

function useToast() {
  const [msg, setMsg] = useState(null);
  const fire = useCallback((m) => {
    setMsg(m);
    clearTimeout(window.__toastT);
    window.__toastT = setTimeout(() => setMsg(null), 2600);
  }, []);
  const node = (<div className={"toast" + (msg ? " show" : "")} aria-live="polite">{msg}</div>);
  return [node, fire];
}

/* one hook every page uses to wire the booking CTAs + toast */
function useBooking() {
  const [toast, fireToast] = useToast();
  const go = (url, label) => { window.open(url, "_blank", "noopener"); fireToast(label); };
  const onBook  = () => go(TOPMATE, <span>Opening <b>Topmate</b> — pick a slot ↗</span>);
  const onWhats = () => go(WHATSAPP, <span>Opening <b>WhatsApp</b> ↗</span>);
  const onMail  = () => { window.location.href = "mailto:" + EMAIL; fireToast(<span>Drafting an email to <b>hi@</b> ↗</span>); };
  return { toast, fireToast, onBook, onWhats, onMail };
}

function Arrow() {
  return (<svg className="arrow" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>);
}

/* ---------------- NAV (multi-page) ---------------- */
function Nav({ active, onBook }) {
  const [menuOpen, setMenuOpen] = useState(false);
  useEffect(() => {
    document.body.style.overflow = menuOpen ? "hidden" : "";
    return () => { document.body.style.overflow = ""; };
  }, [menuOpen]);
  const link = (href, label, key) => (
    <a href={href} className={active === key ? "active" : ""}>{label}</a>
  );
  const mlink = (href, label, key) => (
    <a href={href} className={"m-link" + (active === key ? " active" : "")} onClick={()=>setMenuOpen(false)}>{label}</a>
  );
  return (
    <nav className={"nav" + (menuOpen ? " open" : "")}>
      <div className="wrap">
        <a className="nav-mark" href="index.html"><svg className="ths-mark" viewBox="500 55 905 905" fill="var(--saffron)" role="img" aria-label="THS"><path d="M946,77.42v352.72h29V77.42h114v671.96c-15.86,21.42-22.46,50.62-46.04,65.16l-20.53,74.15-47.43,45.03v-383.69h-29v384.69h-113.5c-.11,0-1.5-1.39-1.5-1.5V78.92c0-.11,1.39-1.5,1.5-1.5h113.5Z"></path><path d="M803,174.34h-60l-.99,762.4-110.01-1.5v-15.99l54.01-56.95-55.01,32.47,1-719.43h-63.01s0-96.92,0-96.92h233.5c3.39,0,.49,14.13.46,16.44-.19,13.95,1.16,28.15,1.09,41.96-.06,12.54-.95,25.01-1.05,37.52Z"></path><path d="M1229,827.82c5.49,5.92,2.98,10.64,14.56,9.04,4.25-.59,8.8-6.75,10.4-10.59l.03-240.55,24.55-22.66,27.48-27.5-60.53,32.44c-2.13-.05-16.62-16.08-18.98-18.95-1.32-1.61-3.67-3.95-3.11-6.02l59.09-81.9,56.95,62.01c5.43,7.66,10.23,15.77,13.29,24.69,3.03,103.56.4,207.71,1.33,311.49-3.85,29.14-31.82,72.95-63.5,75.44-19.44,1.53-40.7-1.39-60.33.18l-34.71,54.74.51-54.96c-37.9.64-71.84-45.49-74.05-80.38v-225.71c23.86-9.54,47.77-19.23,71.8-28.38,10.23-3.9,21.79-9,32.25-11.24,1.13,1.66.98,3.55,1.04,5.45,2.32,77.16-1.76,154.73-.11,231.93-.41,2.13,1.92,1.27,2.07,1.44Z"></path><path d="M1229,391.17c1.87,19.9,27.76,35.88,37.84,52.27l-48.86,94.61-78.99-85.7c-6.46-9.4-12.36-19.07-15.54-30.16-2.42-92.97-2.41-186.9,0-279.88,5.01-24.63,36.9-61.55,63.01-63.94,35.84,2.12,74.6-2.67,110.09,0,25.44,1.91,57.95,36.38,58.5,62.45v210.72s-103.04,39.63-103.04,39.63v-221.32c0-.5-5.54-7.78-5.99-8-2.11-1.03-9.66-.96-11.58.44-.42.3-5.94,7.28-6.43,8.06-.38.61-1.42,1.09-1.06,2.39,2.7,71.62-3.39,145.61.06,216.95.12,2.45-.72,3.27,2,1.48Z"></path><path d="M1089,934.74h-61c5.06-36.61,14.27-72.52,20.45-108.96,12.91-16.44,26.28-32.66,40.55-47.92v156.87Z"></path><path d="M1229,391.17c-2.72,1.78-1.88.97-2-1.48-3.45-71.34,2.63-145.33-.06-216.95-.36-1.3.68-1.78,1.06-2.39-.57,4.26.87,8.27,1.05,12.44,3,68.66-2.34,139.51-.05,208.38Z"></path><path d="M1226,589.01c.1-.02,1.27-2.08,2.01-.5l.99,239.31c-.15-.16-2.48.69-2.07-1.44-1.65-77.2,2.43-154.77.11-231.93-.06-1.9.09-3.78-1.04-5.45Z"></path></svg>That Hyperactive Sardar</a>
        <div className="nav-right">
          <div className="nav-links">
            {link("work.html", "Work", "work")}
            {link("index.html#ship", "What I ship", "ship")}
            {link("about.html", "The journey", "about")}
            {link("articles.html", "Hyperthoughts", "articles")}
            {link("contact.html", "Contact", "contact")}
          </div>
          <a className="nav-book" href="contact.html" onClick={(e)=>{ if(onBook){ e.preventDefault(); onBook(); } }}>Book a call</a>
          <button className="nav-burger" aria-label={menuOpen ? "Close menu" : "Open menu"} aria-expanded={menuOpen} onClick={()=>setMenuOpen((o)=>!o)}>
            <span></span><span></span><span></span>
          </button>
        </div>
      </div>
      <div className={"nav-mobile" + (menuOpen ? " show" : "")} aria-hidden={!menuOpen}>
        {mlink("work.html", "Work", "work")}
        {mlink("index.html#ship", "What I ship", "ship")}
        {mlink("about.html", "The journey", "about")}
        {mlink("articles.html", "Hyperthoughts", "articles")}
        {mlink("contact.html", "Contact", "contact")}
      </div>
    </nav>
  );
}

/* ---------------- HERO — rapid-cut zooming photo cycle (home) ---------------- */
const HERO_SHOTS = [
  "assets/hero-main.png",
  "assets/hero-1.png",
  "assets/hero-2.png",
  "assets/hero-3.png",
  "assets/hero-4.png",
];

function Hero() {
  const [idx, setIdx] = useState(0);
  const [flash, setFlash] = useState(false);
  const [splashIdx, setSplashIdx] = useState(0);
  const [splashOut, setSplashOut] = useState(false);
  const [splashDone, setSplashDone] = useState(false);
  const ref = useRef(null);
  const reduceRef = useRef(false);

  useEffect(() => {
    reduceRef.current = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
    if (reduceRef.current) { setSplashDone(true); return; }
    const timers = [];
    // Splash: cycle each photo at 380ms
    HERO_SHOTS.forEach((_, i) => {
      timers.push(setTimeout(() => setSplashIdx(i), i * 380));
    });
    const introDuration = HERO_SHOTS.length * 380;
    // Start fade-out of splash
    timers.push(setTimeout(() => setSplashOut(true), introDuration));
    // Remove splash from DOM after fade completes
    timers.push(setTimeout(() => setSplashDone(true), introDuration + 520));
    // Start background hero cycle after splash is gone
    timers.push(setTimeout(() => {
      let n = 0;
      const tick = () => {
        n += 1;
        setIdx((i) => (i + 1) % HERO_SHOTS.length);
        if (n % HERO_SHOTS.length === 0) { setFlash(true); setTimeout(() => setFlash(false), 110); }
      };
      const iv = setInterval(tick, 2500);
      timers.push(iv);
    }, introDuration + 520));
    return () => timers.forEach((t) => { clearTimeout(t); clearInterval(t); });
  }, []);

  useEffect(() => {
    const el = ref.current; if (!el) return;
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
    let raf = 0;
    const onScroll = () => {
      if (raf) return;
      raf = requestAnimationFrame(() => {
        raf = 0;
        const h = el.offsetHeight || 1;
        const p = Math.min(1, Math.max(0, window.scrollY / h));
        el.style.setProperty("--scroll", p.toFixed(3));
      });
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  return (
    <React.Fragment>
      {!splashDone && (
        <div className={"splash" + (splashOut ? " out" : "")} aria-hidden="true">
          {HERO_SHOTS.map((src, i) => (
            <img key={src} className={"splash-shot" + (i === splashIdx ? " on" : "")} src={src} alt="" />
          ))}
        </div>
      )}
      <header className="hero" id="top" ref={ref}>
        <div className="hero-stage">
          {HERO_SHOTS.map((src, i) => (
            <img
              key={src}
              className={"hero-shot" + (i === idx ? " on" : "")}
              src={src}
              alt={i === 0 ? "That Hyperactive Sardar" : ""}
              aria-hidden={i === 0 ? undefined : true}
            />
          ))}
        </div>
        {flash && <div className="hero-flash" aria-hidden="true"></div>}
        <div className="hero-scrim" aria-hidden="true"></div>
        <div className="hero-stripe" aria-hidden="true"></div>
      </header>
    </React.Fragment>
  );
}

/* ---------------- FOOTER (shared, multi-page) ---------------- */
function Footer() {
  return (
    <footer>
      <div className="wrap">
        <div className="ft-grid">
          <div>
            <div className="stamp">A.S</div>
            <p className="ft-tag">Internet culture, translated into business growth.</p>
          </div>
          <div>
            <h5>Site</h5>
            <a href="work.html">Work</a>
            <a href="index.html#ship">What I ship</a>
            <a href="about.html">The journey</a>
            <a href="articles.html">Hyperthoughts</a>
            <a href="contact.html">Contact</a>
          </div>
          <div>
            <h5>Elsewhere</h5>
            <a href={LINKEDIN} target="_blank" rel="noopener">LinkedIn</a>
            <a href="https://instagram.com/abhileen.ai" target="_blank" rel="noopener">@abhileen.ai</a>
            <a href="https://instagram.com/a2zindubai" target="_blank" rel="noopener">@a2zindubai</a>
            <a href="https://instagram.com/A2zshots" target="_blank" rel="noopener">@A2zshots</a>
          </div>
          <div>
            <h5>Direct</h5>
            <a href={TOPMATE} target="_blank" rel="noopener">Topmate</a>
            <a href={WHATSAPP} target="_blank" rel="noopener">WhatsApp</a>
            <a href={"mailto:" + EMAIL}>hi@thathyperactivesardar.com</a>
          </div>
        </div>
        <div className="ft-legal">
          <span>© 2026 That Hyperactive Sardar · Dubai</span>
          <span>Anonymised per client NDA where work is referenced</span>
          <span>thathyperactivesardar.com</span>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, { useToast, useBooking, Arrow, Nav, Hero, Footer, HERO_SHOTS, TOPMATE, WHATSAPP, LINKEDIN, EMAIL, NEWSLETTER_ACTION });
