// Two distinct nav components.
//
// 1) HeroWordmark — the centered Amos wordmark that lives INSIDE the hero.
//    Scrolls away naturally with the hero. No CTA, no background.
//
// 2) StickyBar — a separate sticky bar at the page level. Hidden until the
//    hero's CTA scrolls past, then slides down from the top with Amos on the
//    far left and "Join the waitlist" on the far right.

function HeroWordmark({ inverse = false }) {
  const fg = inverse ? "var(--linen)" : "var(--cacao)";
  return (
    <nav
      className="hero-nav"
      style={{
        position: "relative",
        width: "100%",
        zIndex: 5,
        display: "flex",
        justifyContent: "center",
        alignItems: "center",
        padding: "28px 56px",
        background: "transparent",
        color: fg,
        boxSizing: "border-box",
      }}
      aria-label="Amos"
    >
      <a href="#" style={{ display: "flex", alignItems: "center", textDecoration: "none", color: fg }} aria-label="Amos">
        <AmosWordmark height={28} />
      </a>
    </nav>
  );
}

function StickyBar({ inverse = false, onOpen }) {
  const [visible, setVisible] = React.useState(false);

  React.useEffect(() => {
    let raf = 0;
    let cancelled = false;

    const tick = () => {
      raf = 0;
      if (cancelled) return;
      const target = document.querySelector("[data-hero-cta]");
      if (!target) { setVisible(false); return; }
      const r = target.getBoundingClientRect();
      // Show once the hero CTA's bottom edge is above the viewport top.
      setVisible(r.bottom < 0);
    };

    const onScroll = () => {
      if (raf) return;
      raf = requestAnimationFrame(tick);
    };

    tick();
    const id = setTimeout(tick, 200);

    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onScroll);
    document.addEventListener("scroll", onScroll, { passive: true, capture: true });

    return () => {
      cancelled = true;
      clearTimeout(id);
      if (raf) cancelAnimationFrame(raf);
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onScroll);
      document.removeEventListener("scroll", onScroll, { capture: true });
    };
  }, []);

  const fg = inverse ? "var(--linen)" : "var(--cacao)";
  const bg = inverse ? "rgba(31,14,0,0.86)" : "rgba(228,211,196,0.92)";
  const border = inverse ? "1px solid rgba(239,227,209,0.10)" : "1px solid rgba(52,24,0,0.08)";

  const onClickCta = (e) => {
    e.preventDefault();
    if (onOpen) onOpen();
  };

  return (
    <div
      aria-hidden={!visible}
      className="amos-no-fade"
      style={{
        position: "fixed",
        top: 0,
        left: 0,
        right: 0,
        zIndex: 40,
        width: "100%",
        // Slide in/out — no layout impact since we're fixed.
        transform: visible ? "translateY(0)" : "translateY(-100%)",
        opacity: visible ? 1 : 0,
        pointerEvents: visible ? "auto" : "none",
        transition: "transform .35s var(--ease-out), opacity .25s var(--ease-out)",
      }}
    >
      <nav
        className="sticky-bar-inner"
        style={{
          width: "100%",
          display: "flex",
          justifyContent: "space-between",
          alignItems: "center",
          padding: "14px 56px",
          background: bg,
          borderBottom: border,
          backdropFilter: "blur(12px)",
          WebkitBackdropFilter: "blur(12px)",
          color: fg,
          boxSizing: "border-box",
        }}
        aria-label="Amos"
      >
        <a href="#" style={{ display: "flex", alignItems: "center", textDecoration: "none", color: fg }} aria-label="Amos">
          <AmosWordmark height={24} />
        </a>
        <a
          href="#"
          onClick={onClickCta}
          className="sticky-bar-cta"
          style={{
            fontFamily: "var(--font-sans)",
            fontSize: 13,
            fontWeight: 500,
            padding: "9px 16px",
            borderRadius: 10,
            border: "1px solid",
            borderColor: inverse ? "rgba(239,227,209,0.28)" : "var(--border-strong)",
            color: fg,
            textDecoration: "none",
          }}
        >
          Join the waitlist <span className="arrow" style={{ marginLeft: 6 }}>→</span>
        </a>
      </nav>
    </div>
  );
}

// Backward-compat: keep TeaserNav as an alias for HeroWordmark so existing
// callers don't break.
const TeaserNav = HeroWordmark;

Object.assign(window, { HeroWordmark, StickyBar, TeaserNav });
