/* components.jsx — atomic shared components for the Ditto Vault landing page
   Nav, footer, stat-row, partner-mark, etc. All Tailwind/shadcn-equivalent atoms.
   Each component renders the same on desktop and mobile — layout decisions live
   in the section files, atomic components stay viewport-agnostic.
*/

// ── Tiny SVG icon set ──────────────────────────────────────────────────────
// Only the marks we actually use. Strokes use currentColor so callers can
// recolor by setting `color:` on the parent.
const Icon = {
  arrow:  (p) => <svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 8h10M9 4l4 4-4 4"/></svg>,
  arrowR: (p) => <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 12h14M13 6l6 6-6 6"/></svg>,
  arrowLong: (p) => <svg viewBox="0 0 64 16" width="64" height="16" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M2 8h58M52 3l8 5-8 5"/></svg>,
  lock:   (p) => <svg viewBox="0 0 16 16" width="12" height="12" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="3" y="7" width="10" height="7" rx="1"/><path d="M5.5 7V5a2.5 2.5 0 015 0v2"/></svg>,
  check:  (p) => <svg viewBox="0 0 16 16" width="12" height="12" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 8.5L6.5 12 13 4.5"/></svg>,
  dot:    (p) => <svg viewBox="0 0 8 8" width="8" height="8" fill="currentColor" {...p}><circle cx="4" cy="4" r="3"/></svg>,
  github: (p) => <svg viewBox="0 0 16 16" width="14" height="14" fill="currentColor" {...p}><path d="M8 0C3.58 0 0 3.58 0 8a8 8 0 005.47 7.59c.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2 .37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82a7.42 7.42 0 014 0c1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.01 8.01 0 0016 8c0-4.42-3.58-8-8-8z"/></svg>,
  x:      (p) => <svg viewBox="0 0 16 16" width="13" height="13" fill="currentColor" {...p}><path d="M12.4 1h2.3l-5 5.7L15.6 15h-4.6L7.4 9.7 3.3 15H1l5.3-6.1L.6 1h4.7l3.3 4.8L12.4 1zm-.8 12.6h1.3L4.5 2.3H3.1l8.5 11.3z"/></svg>,
  ext:    (p) => <svg viewBox="0 0 12 12" width="10" height="10" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M4 3h5v5M9 3l-6 6"/></svg>,
};

// ── Top nav — floating pill, sticky, active-section aware ────────────────
// Active highlight is driven by `useActiveSection` in app.jsx; passed in as
// `active` here. Mobile rendering switches to a compact pill + a slide-down
// full-viewport menu panel.
const NAV_LINKS = [
  { id: "vault",       label: "Vault" },
  { id: "onramp",      label: "Onramp" },
  { id: "marketplace", label: "Marketplace" },
  { id: "dvn",         label: "DVN" },
  { id: "roadmap",     label: "Roadmap" },
];

function HamburgerIcon({ open }) {
  // Two-line hamburger that morphs into an X. Pure SVG; rotates via CSS
  // transforms (GPU-accelerated, no layout shift).
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" aria-hidden="true">
      <line x1="2.5" y1={open ? "8" : "5.5"} x2="13.5" y2={open ? "8" : "5.5"}
            style={{ transform: open ? "rotate(45deg)" : "none", transformOrigin: "center", transition: "transform 240ms cubic-bezier(0.23, 1, 0.32, 1)" }} />
      <line x1="2.5" y1={open ? "8" : "10.5"} x2="13.5" y2={open ? "8" : "10.5"}
            style={{ transform: open ? "rotate(-45deg)" : "none", transformOrigin: "center", transition: "transform 240ms cubic-bezier(0.23, 1, 0.32, 1)" }} />
    </svg>
  );
}

function Nav({ active = null, mobile = false }) {
  const [open, setOpen] = React.useState(false);

  // Close menu on Escape / when window resizes past mobile breakpoint.
  React.useEffect(() => {
    if (!open) return;
    const onKey = (e) => { if (e.key === "Escape") setOpen(false); };
    window.addEventListener("keydown", onKey);
    document.body.style.overflow = "hidden";
    return () => {
      window.removeEventListener("keydown", onKey);
      document.body.style.overflow = "";
    };
  }, [open]);

  if (mobile) {
    return (
      <>
        <div className="ditto-nav-mobile-wrap">
          <div className="ditto-nav-mobile">
            <a href="#hero" className="nav-brand" aria-label="Top">
              <DittoMark height={22} />
            </a>
            <div className="nav-mobile-actions">
              <button
                className="btn btn-primary"
                style={{ height: 34, padding: "0 14px", fontSize: 13 }}
                onClick={() => window.openWaitlist && window.openWaitlist()}
              >
                Join
              </button>
              <button
                className="nav-menu-toggle"
                aria-label={open ? "Close menu" : "Open menu"}
                aria-expanded={open}
                onClick={() => setOpen((o) => !o)}
              >
                <HamburgerIcon open={open} />
              </button>
            </div>
          </div>
        </div>
        {open && (
          <div className="nav-mobile-panel" role="dialog" aria-modal="true" onClick={() => setOpen(false)}>
            <div className="nav-mobile-list" onClick={(e) => e.stopPropagation()}>
              {NAV_LINKS.map((l) => (
                <a
                  key={l.id}
                  href={`#${l.id}`}
                  className={active === l.id ? "is-active" : ""}
                  onClick={() => setOpen(false)}
                >
                  <span>{l.label}</span>
                  <span className="nav-arrow"><Icon.arrowR /></span>
                </a>
              ))}
              <div className="nav-mobile-divider" />
              <a
                href="https://dittonetwork.io"
                target="_blank"
                rel="noopener noreferrer"
                onClick={() => setOpen(false)}
              >
                <span>Ditto Network</span>
                <span className="nav-arrow"><Icon.ext /></span>
              </a>
              <a
                href="https://github.com/dittonetwork/ditto-ccvault"
                target="_blank"
                rel="noopener noreferrer"
                onClick={() => setOpen(false)}
              >
                <span>GitHub</span>
                <span className="nav-arrow"><Icon.github /></span>
              </a>
            </div>
          </div>
        )}
      </>
    );
  }

  return (
    <div className="ditto-nav-wrap">
      <nav className="ditto-nav-pill" aria-label="Primary">
        <div className="nav-section-left">
          <a href="#hero" className="nav-brand" aria-label="Top">
            <DittoMark height={26} />
          </a>
          <div className="nav-links">
            {NAV_LINKS.map((l) => (
              <a
                key={l.id}
                href={`#${l.id}`}
                className={"nav-link" + (active === l.id ? " is-active" : "")}
              >
                {l.label}
              </a>
            ))}
          </div>
        </div>
        <div className="nav-section-right">
          <a
            href="https://dittonetwork.io"
            target="_blank"
            rel="noopener noreferrer"
            className="nav-link-muted"
          >
            <span>Ditto Network</span>
            <Icon.ext />
          </a>
          <a
            href="https://github.com/dittonetwork/ditto-ccvault"
            target="_blank"
            rel="noopener noreferrer"
            className="nav-link-icon"
            aria-label="GitHub"
          >
            <Icon.github />
          </a>
          <button
            className="btn btn-primary"
            style={{ height: 36, padding: "0 16px", fontSize: 13 }}
            onClick={() => window.openWaitlist && window.openWaitlist()}
          >
            Join waitlist
          </button>
        </div>
      </nav>
    </div>
  );
}

// ── Ditto wordmark (white version, scaled by height) ─────────────────────
function DittoMark({ height = 22 }) {
  return (
    <img
      src="assets/ditto-wordmark-white.png"
      alt="Ditto"
      style={{ height, width:"auto", display:"block", opacity:0.94 }}
    />
  );
}

function DittoSymbol({ size = 24 }) {
  return (
    <img
      src="assets/ditto-symbol-white.png"
      alt="Ditto"
      style={{ width:size, height:size, display:"block" }}
    />
  );
}

// ── Stat row — compact horizontal stat strip used in Sections 2 & 5 ──────
function StatRow({ items, mobile = false }) {
  return (
    <div className="stat-row" style={mobile ? { gridAutoFlow:"row", gridTemplateColumns:"1fr 1fr" } : null}>
      {items.map((s, i) => (
        <div key={i} className="stat" style={mobile && i % 2 === 0 ? { borderRight:"1px solid var(--line)" } : null}>
          <div className="stat-value">{s.value}</div>
          <div className="stat-label">{s.label}</div>
        </div>
      ))}
    </div>
  );
}

// ── Partner wordmark — generic styled wordmark, monochrome ───────────────
// We don't have real licensed logos here; we render the partner name in a
// neutral grotesk so it reads as an institutional wordmark next to the
// product ticker. Frontend can swap these for licensed SVGs.
function PartnerMark({ name, sub }) {
  return (
    <div style={{ display:"flex", flexDirection:"column", gap:2, color:"var(--text-2)" }}>
      <div style={{ fontFamily:"var(--font-display)", fontWeight:600, fontSize:13, letterSpacing:"-0.01em", lineHeight:1.1, textTransform:"uppercase" }}>
        {name}
      </div>
      {sub && <div className="mono" style={{ fontSize:10, color:"var(--text-4)", letterSpacing:"0.04em" }}>{sub}</div>}
    </div>
  );
}

// ── Section header — big numeric anchor + heading + intro paragraph ──────
// The repeated mono "/ 02 / VAULT" eyebrow pattern across every section reads
// as AI scaffolding; replaced with a single large accent number that anchors
// the section visually without the tracked-uppercase label noise. `label` is
// still accepted for backward compat but no longer rendered.
function SectionHeader({ number, label, heading, body, headingWidth = "20ch" }) {
  return (
    <div style={{ display:"flex", flexDirection:"column", gap:18, position:"relative" }}>
      {number && (
        <div className="mono section-anchor" aria-hidden="true">
          {String(number).padStart(2,"0")}
        </div>
      )}
      <h2 className="s-heading" style={{ margin:0, maxWidth:headingWidth }}>{heading}</h2>
      {body && <p className="lede" style={{ margin:0 }}>{body}</p>}
    </div>
  );
}

// Compact eyebrow — for in-card labels, status strips, etc.
function Eyebrow({ children, accent = false }) {
  return (
    <div className="eyebrow" style={accent ? { color: "var(--accent)" } : null}>
      <span className="slash">/</span>
      <span>{children}</span>
    </div>
  );
}

// ── Section frame — sets gutter + background, optional grid hairlines ────
function SectionFrame({ children, bg, hairs = true, gated = false, style }) {
  return (
    <section
      className={gated ? "gated-soft" : ""}
      style={{
        position:"relative",
        background: bg ?? "var(--bg)",
        padding:"0 var(--gutter-desktop)",
        ...(style || {}),
      }}
    >
      {hairs && <div className="hairgrid" />}
      <div style={{ position:"relative", zIndex:1 }}>
        {children}
      </div>
    </section>
  );
}

// ── Code-ish technical pill ──────────────────────────────────────────────
function TechPill({ children, accent = false }) {
  return (
    <span className="mono" style={{
      display:"inline-flex", alignItems:"center", gap:6,
      padding:"3px 8px",
      border:"1px solid " + (accent ? "var(--accent-line)" : "var(--line-strong)"),
      background: accent ? "var(--accent-soft)" : "var(--bg-elev-2)",
      color: accent ? "var(--accent)" : "var(--text-2)",
      fontSize:11, borderRadius:"var(--r-2)", letterSpacing:0,
    }}>
      {children}
    </span>
  );
}

// Expose to window so other JSX scripts can import.
Object.assign(window, {
  Icon, Nav, DittoMark, DittoSymbol, StatRow, PartnerMark,
  SectionHeader, Eyebrow, SectionFrame, TechPill,
});
