/* Shared primitives → window. Logo, Placeholder, RiverFlow, useReveal */
(function () {
  const { useEffect, useRef, useState } = React;

  // Scroll reveal — adds .in when element enters viewport
  window.useReveal = function useReveal() {
    useEffect(() => {
      const els = document.querySelectorAll("[data-reveal]:not(.in)");
      if (!("IntersectionObserver" in window)) {
        els.forEach((e) => e.classList.add("in"));
        return;
      }
      const io = new IntersectionObserver(
        (ents) => ents.forEach((en) => {
          if (en.isIntersecting) { en.target.classList.add("in"); io.unobserve(en.target); }
        }),
        { threshold: 0.12, rootMargin: "0px 0px -8% 0px" }
      );
      els.forEach((e) => io.observe(e));
      return () => io.disconnect();
    });
  };

  // EMA logo lockup
  window.Logo = function Logo({ mark, mode = "dark", compact }) {
    const ink = mode === "light" ? "#fff" : "var(--navy-800)";
    const sub = mode === "light" ? "rgba(255,255,255,.66)" : "var(--slate-600)";
    const logo = "assets/ema_logo_cutout.png";
    return (
      <a href="index.html" style={{ display: "flex", alignItems: "center", gap: 13, flex: "none" }}>
        <img src={logo} alt="EMA" style={{ width: compact ? 38 : 46, height: compact ? 38 : 46, objectFit: "contain" }} />
        {!compact && (
          <span style={{ display: "flex", flexDirection: "column", lineHeight: 1.05 }}>
            <span style={{ fontFamily: "var(--display)", fontWeight: 700, fontSize: 21, letterSpacing: ".06em", color: ink }}>EMA</span>
            <span style={{ fontFamily: "var(--mono)", fontSize: 9.5, letterSpacing: ".16em", textTransform: "uppercase", color: sub, marginTop: 2, whiteSpace: "nowrap" }} className="ema-logo-sub">European Mesopotamia Alliance</span>
          </span>
        )}
      </a>
    );
  };

  // Striped image placeholder with mono label
  window.Placeholder = function Placeholder({ label, ratio = "16/10", tone = "navy", radius = 4, style = {} }) {
    const navy = tone === "navy";
    const stripe = navy ? "rgba(31,56,100,.07)" : "rgba(184,144,47,.10)";
    const base = navy ? "var(--paper-2)" : "rgba(184,144,47,.05)";
    const txt = navy ? "var(--slate-400)" : "var(--gold-600)";
    return (
      <div style={{
        aspectRatio: ratio, width: "100%", borderRadius: radius, position: "relative",
        background: `repeating-linear-gradient(135deg, ${base} 0 16px, ${stripe} 16px 32px)`,
        border: "1px solid var(--hairline)", display: "flex", alignItems: "center",
        justifyContent: "center", overflow: "hidden", ...style,
      }}>
        <span style={{ fontFamily: "var(--mono)", fontSize: 11, letterSpacing: ".08em", textTransform: "uppercase", color: txt, textAlign: "center", padding: "8px 16px" }}>{label}</span>
      </div>
    );
  };

  // River motif — winding twin-strand path echoing the logo
  window.RiverFlow = function RiverFlow({ w = 1200, h = 220, opacity = 1, style = {} }) {
    const d = "M-20 60 C 200 60, 240 180, 460 180 S 720 40, 940 60 S 1180 170, 1320 150";
    const d2 = "M-20 110 C 220 110, 260 30, 480 30 S 740 170, 960 150 S 1180 40, 1320 70";
    return (
      <svg viewBox={`0 0 ${w} ${h}`} preserveAspectRatio="none" style={{ width: "100%", height: "100%", opacity, ...style }} aria-hidden="true">
        <path className="river-flow" d={d} stroke="var(--navy-600)" strokeWidth="2.5" />
        <path className="river-flow" d={d2} stroke="var(--gold-500)" strokeWidth="2.5" />
      </svg>
    );
  };
})();
