/* global React */
const { useState, useEffect, useRef } = React;

/* ---- lucide-style stroke icon factory ---- */
function mkIcon(paths) {
  return function Icon({ size = 22, stroke = 1.8, className = "", style }) {
    return (
      <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
        stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round"
        className={className} style={style} aria-hidden="true">
        {paths}
      </svg>
    );
  };
}

const IconMenu        = mkIcon(<><path d="M3 6h18"/><path d="M3 12h18"/><path d="M3 18h18"/></>);
const IconArrowRight  = mkIcon(<><path d="M5 12h14"/><path d="M13 6l6 6-6 6"/></>);
const IconArrowUpRight= mkIcon(<><path d="M7 17 17 7"/><path d="M8 7h9v9"/></>);
const IconCheck       = mkIcon(<path d="M4 12.5 9 17.5 20 6.5"/>);
const IconTicket      = mkIcon(<><path d="M3 9a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2 2 2 0 0 0 0 6 2 2 0 0 1-2 2H5a2 2 0 0 1-2-2 2 2 0 0 0 0-6Z"/><path d="M13 7v10"/></>);
const IconCalendar    = mkIcon(<><rect x="3" y="4.5" width="18" height="17" rx="2.5"/><path d="M3 9.5h18M8 3v4M16 3v4"/></>);
const IconMapPin      = mkIcon(<><path d="M20 10c0 5.5-8 11.5-8 11.5S4 15.5 4 10a8 8 0 0 1 16 0Z"/><circle cx="12" cy="10" r="2.6"/></>);
const IconUsers       = mkIcon(<><circle cx="9" cy="8" r="3.2"/><path d="M2.5 20a6.5 6.5 0 0 1 13 0"/><path d="M16 5.2a3.2 3.2 0 0 1 0 6.1"/><path d="M17.5 14.2A6.5 6.5 0 0 1 21.5 20"/></>);
const IconScan        = mkIcon(<><path d="M4 8V6a2 2 0 0 1 2-2h2M16 4h2a2 2 0 0 1 2 2v2M20 16v2a2 2 0 0 1-2 2h-2M8 20H6a2 2 0 0 1-2-2v-2"/><path d="M4 12h16"/></>);
const IconDoor        = mkIcon(<><path d="M5 21V4a1 1 0 0 1 1-1h8.5a1 1 0 0 1 1 1v17"/><path d="M3 21h16"/><path d="M19 21V8l-3.5-2"/><circle cx="12.5" cy="12.5" r=".9" fill="currentColor" stroke="none"/></>);
const IconGrid        = mkIcon(<><rect x="3" y="3" width="7.5" height="7.5" rx="1.5"/><rect x="13.5" y="3" width="7.5" height="7.5" rx="1.5"/><rect x="3" y="13.5" width="7.5" height="7.5" rx="1.5"/><rect x="13.5" y="13.5" width="7.5" height="7.5" rx="1.5"/></>);
const IconZap         = mkIcon(<path d="M13 2 4 14h7l-1 8 9-12h-7l1-8Z"/>);
const IconShield      = mkIcon(<><path d="M12 2.5 4.5 5.5v6c0 4.5 3.2 8.2 7.5 10 4.3-1.8 7.5-5.5 7.5-10v-6L12 2.5Z"/><path d="M9 12l2 2 4-4"/></>);
const IconGlobe       = mkIcon(<><circle cx="12" cy="12" r="9"/><path d="M3 12h18"/><path d="M12 3c2.5 2.5 3.8 5.7 3.8 9S14.5 18.5 12 21c-2.5-2.5-3.8-5.7-3.8-9S9.5 5.5 12 3Z"/></>);
const IconWallet      = mkIcon(<><rect x="3" y="6" width="18" height="14" rx="2.5"/><path d="M3 10h18"/><circle cx="16.5" cy="14" r="1.2" fill="currentColor" stroke="none"/></>);
const IconPhone       = mkIcon(<><rect x="6.5" y="2.5" width="11" height="19" rx="3"/><path d="M10.5 18.5h3"/></>);
const IconMonitor     = mkIcon(<><rect x="2.5" y="4" width="19" height="13" rx="2"/><path d="M8.5 21h7M12 17v4"/></>);
const IconStar        = mkIcon(<path d="M12 3.5l2.6 5.3 5.9.9-4.3 4.1 1 5.8L12 17.9 6.8 19.6l1-5.8L3.5 9.7l5.9-.9L12 3.5Z"/>);
const IconPlus        = mkIcon(<><path d="M12 5v14M5 12h14"/></>);
const IconTrending    = mkIcon(<><path d="M3 17l6-6 4 4 7-7"/><path d="M14 8h6v6"/></>);
const IconClock       = mkIcon(<><circle cx="12" cy="12" r="8.5"/><path d="M12 7.5V12l3 2"/></>);
const IconQr          = mkIcon(<><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><path d="M14 14h3v3M21 14v7M14 21h7"/></>);

/* ---- Aperture logomark (reused from brand) ---- */
function Aperture({ size = 26, color = "currentColor", stroke = 8.5, className = "", style }) {
  return (
    <svg viewBox="0 0 100 100" width={size} height={size} fill="none" className={className} style={style} aria-hidden="true">
      <g stroke={color} strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round">
        <path d="M14 40 L14 14 L40 14"/>
        <path d="M86 40 L86 14 L60 14"/>
        <path d="M14 60 L14 86 L40 86"/>
        <path d="M86 60 L86 86 L60 86"/>
      </g>
      <circle cx="50" cy="50" r="7" fill={color}/>
    </svg>
  );
}

function Wordmark({ markSize = 24, fontSize = 19, ls = 5, accent = false, className = "" }) {
  return (
    <span className={"inline-flex items-center gap-2.5 " + className} style={{ lineHeight: 1 }}>
      <Aperture size={markSize} color={accent ? "#FAFAFA" : "currentColor"} stroke={9} />
      <span style={{ fontWeight: 700, letterSpacing: ls, fontSize }}>AXXESS</span>
    </span>
  );
}

/* ---- hooks ---- */
// Intersection-based reveal: add `.reveal` to elements; this flips them to `.in`
function useReveal() {
  useEffect(() => {
    const els = document.querySelectorAll(".reveal");
    if (!("IntersectionObserver" in window)) { els.forEach(e => e.classList.add("in")); return; }
    const io = new IntersectionObserver((ents) => {
      ents.forEach(e => { if (e.isIntersecting) { e.target.classList.add("in"); io.unobserve(e.target); } });
    }, { threshold: 0.16, rootMargin: "0px 0px -8% 0px" });
    els.forEach(e => io.observe(e));
    return () => io.disconnect();
  });
}

// count-up when element scrolls into view
function useCountUp(target, { duration = 1400, decimals = 0 } = {}) {
  const ref = useRef(null);
  const [val, setVal] = useState(0);
  useEffect(() => {
    const el = ref.current; if (!el) return;
    let started = false;
    const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
    const run = () => {
      if (reduce) { setVal(target); return; }
      const t0 = performance.now();
      const tick = (now) => {
        const p = Math.min(1, (now - t0) / duration);
        const e = 1 - Math.pow(1 - p, 3);
        setVal(target * e);
        if (p < 1) requestAnimationFrame(tick);
      };
      requestAnimationFrame(tick);
    };
    const io = new IntersectionObserver((ents) => {
      ents.forEach(en => { if (en.isIntersecting && !started) { started = true; run(); io.unobserve(el); } });
    }, { threshold: 0.5 });
    io.observe(el);
    return () => io.disconnect();
  }, [target]);
  const display = decimals ? val.toFixed(decimals) : Math.round(val).toLocaleString();
  return [ref, display];
}

window.AX = Object.assign(window.AX || {}, {
  IconMenu, IconArrowRight, IconArrowUpRight, IconCheck, IconTicket, IconCalendar, IconMapPin,
  IconUsers, IconScan, IconDoor, IconGrid, IconZap, IconShield, IconGlobe, IconWallet, IconPhone,
  IconMonitor, IconStar, IconPlus, IconTrending, IconClock, IconQr,
  Aperture, Wordmark, useReveal, useCountUp,
});
