// shared/chrome.jsx — Nav, Footer, scroll-reveal observer, page-transition init.
// Exposes globals: WhitenNav, WhitenFooter, WhitenAnnouncement, useReveal, Mark, Arrow

const Mark = ({ size = 28 }) => (
  <span className="mark" style={{ width: size, height: size, fontSize: size * 0.72 }}>w</span>
);

const Arrow = ({ size = 14, dir = "right" }) => {
  const rot = { right: 0, up: -45, down: 135, left: 180 }[dir] || 0;
  return (
    <svg className="ico-arr" viewBox="0 0 14 14" width={size} height={size}
         style={{ transform: `rotate(${rot}deg)` }} aria-hidden="true">
      <path d="M2 7h10M8 3l4 4-4 4" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  );
};

const NAV_ITEMS = [
  { href: "index.html",         label: "Home",       key: "home" },
  { href: "services.html",      label: "Services",   key: "services" },
  { href: "workflows.html",     label: "Workflows",  key: "workflows" },
  { href: "case-studies.html",  label: "Case studies", key: "cases" },
  { href: "how-it-works.html",  label: "How it works", key: "how" }
];

function WhitenAnnouncement({ children }) {
  return (
    <div className="ann">
      <span className="dot"></span>
      {children || <>Now booking June 2026 — 2 build slots left</>}
    </div>
  );
}

function WhitenNav({ active }) {
  return (
    <header className="nav-host">
      <nav className="nav">
        <a href="index.html" className="nav-brand" aria-label="WhitenApp home">
          <Mark />
          <span>Whiten<span className="wm">App</span></span>
        </a>
        <div className="nav-links">
          {NAV_ITEMS.map(it => (
            <a key={it.key} href={it.href} className={active === it.key ? "active" : ""}>{it.label}</a>
          ))}
        </div>
        <a href="index.html#book" className="nav-cta">
          Book a free assessment <span className="arr"><Arrow size={14} /></span>
        </a>
      </nav>
    </header>
  );
}

function WhitenFooter() {
  return (
    <footer className="footer">
      <div style={{ maxWidth: 1440, margin: "0 auto" }}>
        <div style={{
          display: "grid",
          gridTemplateColumns: "1.4fr 1fr 1fr 1fr",
          gap: 48,
          alignItems: "start"
        }}>
          <div>
            <div className="nav-brand" style={{ fontSize: 22, color: "var(--bg)" }}>
              <Mark size={36} />
              <span>Whiten<span className="wm" style={{ color: "var(--accent)" }}>App</span></span>
            </div>
            <p style={{ marginTop: 18, maxWidth: 360, color: "rgba(244,241,234,.6)", fontSize: 15, lineHeight: 1.55 }}>
              An AI &amp; automation studio for mid-market ops teams who'd rather their people did the interesting work.
            </p>
            <a href="index.html#book" className="btn btn-accent" style={{ marginTop: 28 }}>
              Book a free assessment <Arrow />
            </a>
          </div>

          <FooterCol title="Site" links={[
            ["Home", "index.html"],
            ["Services", "services.html"],
            ["Workflows", "workflows.html"],
            ["Case studies", "case-studies.html"],
            ["How it works", "how-it-works.html"]
          ]} />
          <FooterCol title="Company" links={[
            ["About", "#"], ["Reviews", "#"], ["Blog", "#"], ["Privacy", "#"]
          ]} />
          <FooterCol title="Connect" links={[
            ["aki@whitenapp.com.au", "mailto:aki@whitenapp.com.au"],
            ["https://www.linkedin.com/company/whitenapp-ai/", "#"]
          ]} />
        </div>

        <div className="ft-wm">WhitenApp</div>

        <div style={{
          display: "flex", justifyContent: "space-between", alignItems: "center",
          marginTop: 30, paddingTop: 24, borderTop: "1px solid rgba(244,241,234,.12)",
          fontSize: 12.5, color: "rgba(244,241,234,.4)", fontFamily: "var(--f-mono)"
        }}>
          <span>© 2026 WhitenApp AI · Sydney / Remote</span>
          <span>Built for owners who are doing everything</span>
        </div>
      </div>
    </footer>
  );
}

function FooterCol({ title, links }) {
  return (
    <div>
      <div style={{
        fontFamily: "var(--f-mono)", fontSize: 11, letterSpacing: ".12em",
        textTransform: "uppercase", color: "rgba(244,241,234,.4)", marginBottom: 16
      }}>{title}</div>
      <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 10 }}>
        {links.map(([label, href]) => (
          <li key={label}>
            <a href={href} style={{ color: "rgba(244,241,234,.85)", fontSize: 15 }}>{label}</a>
          </li>
        ))}
      </ul>
    </div>
  );
}

// ─── reveal-on-scroll observer ───
function useReveal() {
  React.useEffect(() => {
    if (document.documentElement.getAttribute('data-motion') === '0') {
      document.querySelectorAll('.reveal').forEach(el => el.classList.add('in'));
      return;
    }
    // immediately reveal anything already in (or near) the viewport so the
    // first paint doesn't look blank
    const reveal = (el) => {
      const r = el.getBoundingClientRect();
      if (r.top < window.innerHeight * 1.05 && r.bottom > 0) {
        // small stagger to keep some animation life
        const delay = parseFloat(getComputedStyle(el).getPropertyValue('--rd')) || 0;
        if (delay) setTimeout(() => el.classList.add('in'), delay * 1000);
        else el.classList.add('in');
        return true;
      }
      return false;
    };

    const io = new IntersectionObserver((entries) => {
      entries.forEach(en => {
        if (en.isIntersecting) {
          en.target.classList.add('in');
          io.unobserve(en.target);
        }
      });
    }, { threshold: 0.08, rootMargin: "0px 0px -6% 0px" });

    document.querySelectorAll('.reveal').forEach(el => {
      if (!reveal(el)) io.observe(el);
    });
    return () => io.disconnect();
  }, []);
}

Object.assign(window, {
  Mark, Arrow, WhitenNav, WhitenFooter, WhitenAnnouncement, useReveal, NAV_ITEMS
});
