// components.jsx — icons, primitives, shared bits

const { motion, AnimatePresence, useScroll, useTransform, useMotionValue, useSpring, useInView } = window.framerMotion || window.Motion || {};

// ─── Minimal line icons ─────────────────────────────────────────────
const IconBase = ({ children, size = 20 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
       stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
    {children}
  </svg>
);

const IconStack = () => <IconBase><path d="M12 3l9 5-9 5-9-5 9-5z"/><path d="M3 13l9 5 9-5"/><path d="M3 18l9 5 9-5"/></IconBase>;
const IconChain = () => <IconBase><path d="M10 13a5 5 0 007 0l3-3a5 5 0 00-7-7l-1 1"/><path d="M14 11a5 5 0 00-7 0l-3 3a5 5 0 007 7l1-1"/></IconBase>;
const IconPhone = () => <IconBase><rect x="7" y="2" width="10" height="20" rx="2"/><path d="M11 18h2"/></IconBase>;
const IconCloud = () => <IconBase><path d="M17 18a4 4 0 00.5-7.97 6 6 0 00-11.5 1A4.5 4.5 0 006.5 19H17z"/></IconBase>;
const IconGrid = () => <IconBase><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></IconBase>;
const IconShield = () => <IconBase><path d="M12 3l8 3v6c0 5-3.5 8.5-8 9-4.5-.5-8-4-8-9V6l8-3z"/><path d="M9 12l2 2 4-4"/></IconBase>;
const IconMail = () => <IconBase><rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/></IconBase>;
const IconPin = () => <IconBase><path d="M12 21s-7-6.5-7-12a7 7 0 0114 0c0 5.5-7 12-7 12z"/><circle cx="12" cy="9" r="2.5"/></IconBase>;
const IconArrow = () => <IconBase><path d="M5 12h14"/><path d="M13 6l6 6-6 6"/></IconBase>;

// ─── Animated brand mark (real logo) ────────────────────────────────
function LogoMark({ size = 28, animate = true }) {
  return (
    <motion.div
      className="logo-mark-wrap"
      style={{ width: size, height: size }}
      initial={animate ? { opacity: 0, scale: 0.6, rotate: -8 } : false}
      animate={animate ? { opacity: 1, scale: 1, rotate: 0 } : false}
      transition={{ duration: 0.9, ease: [0.2, 0.8, 0.2, 1] }}
      aria-hidden="true"
    >
      <motion.span
        className="logo-mark-glow"
        animate={{ opacity: [0.25, 0.55, 0.25] }}
        transition={{ duration: 3.2, repeat: Infinity, ease: "easeInOut" }}
      />
      <img src="./logo.png" alt="" width={size} height={size} className="logo-mark-img"/>
    </motion.div>
  );
}

// ─── Reveal primitive ───────────────────────────────────────────────
function Reveal({ children, delay = 0, y = 24, className = "", as = "div" }) {
  const ref = React.useRef(null);
  const inView = useInView(ref, { once: true, margin: "-10% 0px" });
  const Tag = motion[as] || motion.div;
  const motionLevel = window.__motionLevel ?? 7;
  const scale = motionLevel / 7;
  return (
    <Tag
      ref={ref}
      className={className}
      initial={{ opacity: 0, y: y * scale, filter: `blur(${6 * scale}px)` }}
      animate={inView ? { opacity: 1, y: 0, filter: "blur(0px)" } : {}}
      transition={{ duration: 0.9, delay, ease: [0.2, 0.8, 0.2, 1] }}
    >
      {children}
    </Tag>
  );
}

// ─── Split text: animate per-character ──────────────────────────────
function SplitText({ text, className = "", delay = 0, stagger = 0.03, y = 40 }) {
  const ref = React.useRef(null);
  const inView = useInView(ref, { once: true });
  const chars = [...text];
  return (
    <span ref={ref} className={className} style={{ display: "inline-block" }}>
      {chars.map((c, i) => (
        <motion.span
          key={i}
          style={{ display: "inline-block", whiteSpace: c === " " ? "pre" : "normal" }}
          initial={{ opacity: 0, y, rotateX: -60 }}
          animate={inView ? { opacity: 1, y: 0, rotateX: 0 } : {}}
          transition={{
            duration: 0.8,
            delay: delay + i * stagger,
            ease: [0.2, 0.8, 0.2, 1],
          }}
        >
          {c}
        </motion.span>
      ))}
    </span>
  );
}

// ─── Button link (formerly magnetic — wiggle removed) ───────────────
function MagneticLink({ children, href, primary = false, onClick }) {
  return (
    <a
      href={href}
      onClick={onClick}
      className={`btn ${primary ? "btn-primary" : "btn-secondary"}`}
    >
      <span style={{ position: "relative", zIndex: 1 }}>{children}</span>
      {primary && <span className="btn-glow" aria-hidden="true"/>}
    </a>
  );
}

// ─── Aurora / gradient mesh for hero variants ───────────────────────
function Aurora({ intensity = 1 }) {
  return (
    <div className="aurora" aria-hidden="true">
      <motion.div
        className="aurora-blob aurora-a"
        animate={{
          x: ["0%", "8%", "-4%", "0%"],
          y: ["0%", "-6%", "4%", "0%"],
          scale: [1, 1.15, 0.95, 1],
        }}
        transition={{ duration: 22, repeat: Infinity, ease: "easeInOut" }}
      />
      <motion.div
        className="aurora-blob aurora-b"
        animate={{
          x: ["0%", "-10%", "6%", "0%"],
          y: ["0%", "8%", "-4%", "0%"],
          scale: [1, 0.9, 1.1, 1],
        }}
        transition={{ duration: 28, repeat: Infinity, ease: "easeInOut" }}
      />
      <motion.div
        className="aurora-blob aurora-c"
        animate={{
          x: ["0%", "5%", "-7%", "0%"],
          y: ["0%", "-4%", "6%", "0%"],
        }}
        transition={{ duration: 34, repeat: Infinity, ease: "easeInOut" }}
      />
      <div className="aurora-grain"/>
    </div>
  );
}

// ─── Animated compute/mesh visual (hero variant B) ─────────────────
function MeshVisual() {
  const nodes = React.useMemo(() => {
    const arr = [];
    for (let i = 0; i < 5; i++) {
      for (let j = 0; j < 5; j++) {
        arr.push({ id: `${i}-${j}`, x: 10 + i * 20, y: 10 + j * 20 });
      }
    }
    return arr;
  }, []);

  const edges = React.useMemo(() => {
    const e = [];
    nodes.forEach((n, i) => {
      nodes.slice(i + 1).forEach((m) => {
        const d = Math.hypot(n.x - m.x, n.y - m.y);
        if (d < 22) e.push({ from: n, to: m, d });
      });
    });
    return e;
  }, [nodes]);

  return (
    <div className="mesh-wrap">
      <svg viewBox="0 0 100 100" preserveAspectRatio="none" className="mesh-svg">
        {edges.map((e, i) => (
          <motion.line
            key={i}
            x1={e.from.x} y1={e.from.y} x2={e.to.x} y2={e.to.y}
            stroke="var(--accent)" strokeWidth="0.12" strokeOpacity="0.25"
            initial={{ pathLength: 0 }}
            animate={{ pathLength: 1 }}
            transition={{ duration: 2, delay: i * 0.02, ease: [0.2, 0.8, 0.2, 1] }}
          />
        ))}
        {nodes.map((n, i) => (
          <motion.circle
            key={n.id}
            cx={n.x} cy={n.y} r="0.5"
            fill="var(--accent)"
            initial={{ opacity: 0, scale: 0 }}
            animate={{
              opacity: [0, 1, 0.4, 1],
              scale: [0, 1.2, 0.8, 1],
            }}
            transition={{
              duration: 4,
              delay: i * 0.04,
              repeat: Infinity,
              repeatDelay: Math.random() * 3,
            }}
          />
        ))}
      </svg>
      <div className="mesh-overlay"/>
    </div>
  );
}

// ─── Marquee for trust row / stack ─────────────────────────────────
function Marquee({ items, speed = 40 }) {
  return (
    <div className="marquee">
      <motion.div
        className="marquee-track"
        animate={{ x: ["0%", "-50%"] }}
        transition={{ duration: speed, ease: "linear", repeat: Infinity }}
      >
        {[...items, ...items].map((it, i) => (
          <span key={i} className="marquee-item">{it}</span>
        ))}
      </motion.div>
    </div>
  );
}

Object.assign(window, {
  IconStack, IconChain, IconPhone, IconCloud, IconGrid, IconShield,
  IconMail, IconPin, IconArrow,
  LogoMark, Reveal, SplitText, MagneticLink, Aurora, MeshVisual, Marquee,
});
