/* ============================================================
   Shared components + inline SVG icons
   ============================================================ */
const { useState, useEffect, useRef } = React;

/* ---- Minimal stroke icons (no decorative SVG art, just UI glyphs) ---- */
const Icon = ({ name, size = 20, stroke = 1.7, ...p }) => {
  const paths = {
    calendar: <><rect x="3" y="4.5" width="18" height="16" rx="2.5"/><path d="M3 9h18M8 2.5v4M16 2.5v4"/></>,
    pin: <><path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0116 0z"/><circle cx="12" cy="10" r="2.6"/></>,
    users: <><circle cx="9" cy="8" r="3.2"/><path d="M2.5 20a6.5 6.5 0 0113 0M16 5.2a3.2 3.2 0 010 5.6M21.5 20a6.5 6.5 0 00-4-6"/></>,
    gavel: <><path d="M14 3l7 7-3 3-7-7zM10.5 6.5l7 7M3 21h9M4.5 13.5l5 5"/></>,
    check: <path d="M20 6L9 17l-5-5"/>,
    plus: <path d="M12 5v14M5 12h14"/>,
    arrowRight: <path d="M5 12h14M13 6l6 6-6 6"/>,
    arrowLeft: <path d="M19 12H5M11 6l-6 6 6 6"/>,
    mail: <><rect x="3" y="5" width="18" height="14" rx="2.5"/><path d="M3.5 6.5L12 13l8.5-6.5"/></>,
    phone: <path d="M5 3h3.5l1.8 4.5-2.2 1.4a13 13 0 006 6l1.4-2.2L20 16.5V20a1.5 1.5 0 01-1.7 1.5C10.3 20.7 3.3 13.7 2.5 5.7A1.5 1.5 0 014 4z"/>,
    instagram: <><rect x="3" y="3" width="18" height="18" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="17.4" cy="6.6" r="1" fill="currentColor" stroke="none"/></>,
    whatsapp: <path d="M3 21l1.6-4.3A8.4 8.4 0 1112 20.5a8.5 8.5 0 01-4.2-1.1L3 21zm6-12c-.3 0-.6.1-.8.5s-.9 1-.9 2.3 1 2.7 1.1 2.9c.1.2 1.9 3 4.7 4.1 2.3.9 2.8.7 3.3.7s1.6-.6 1.8-1.3c.2-.6.2-1.2.2-1.3-.1-.1-.3-.2-.6-.4s-1.6-.8-1.8-.9c-.2-.1-.4-.1-.6.1s-.7.9-.8 1.1c-.2.2-.3.2-.6.1s-1.2-.5-2.3-1.4c-.8-.7-1.4-1.6-1.6-1.9s0-.4.1-.6l.4-.5c.1-.2.2-.3.3-.5s0-.4 0-.5-.6-1.5-.8-2c-.2-.5-.4-.4-.6-.4z" strokeWidth="0" fill="currentColor"/>,
    book: <><path d="M4 4.5A1.5 1.5 0 015.5 3H20v15.5H5.5A1.5 1.5 0 004 20"/><path d="M4 4.5v15.5"/></>,
    clock: <><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3.5 2"/></>,
    star: <path d="M12 3l2.6 5.6 6.1.7-4.5 4.2 1.2 6L12 16.8 6.6 19.5l1.2-6L3.3 9.3l6.1-.7z"/>,
    sparkle: <path d="M12 3l1.6 5.4L19 10l-5.4 1.6L12 17l-1.6-5.4L5 10l5.4-1.6z"/>,
    shield: <path d="M12 3l7 3v5c0 4.4-3 7.6-7 9-4-1.4-7-4.6-7-9V6z"/>,
    globe: <><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3c2.6 2.7 2.6 15.3 0 18M12 3c-2.6 2.7-2.6 15.3 0 18"/></>,
    document: <><path d="M6 2.5h8l4 4V21a.5.5 0 01-.5.5h-11A.5.5 0 016 21z"/><path d="M14 2.5V7h4M8.5 13h7M8.5 16.5h7"/></>,
    menu: <path d="M3 6h18M3 12h18M3 18h18"/>,
    close: <path d="M6 6l12 12M18 6L6 18"/>,
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
      stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round" {...p}>
      {paths[name]}
    </svg>
  );
};

/* ---- Scroll reveal wrapper ---- */
const Reveal = ({ children, delay = 0, as: Tag = "div", className = "", ...p }) => {
  const ref = useRef(null);
  const [shown, setShown] = useState(false);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    // Already in view on mount? reveal immediately.
    const r = el.getBoundingClientRect();
    if (r.top < window.innerHeight && r.bottom > 0) { setShown(true); return; }
    let fired = false;
    const io = new IntersectionObserver(
      ([e]) => { if (e.isIntersecting) { fired = true; setShown(true); io.disconnect(); } },
      { threshold: 0.12, rootMargin: "0px 0px -8% 0px" }
    );
    io.observe(el);
    // Failsafe: if the observer never fires (some embedded iframes), reveal anyway.
    const t = setTimeout(() => { if (!fired) setShown(true); }, 1200);
    return () => { io.disconnect(); clearTimeout(t); };
  }, []);
  return (
    <Tag ref={ref} className={`reveal ${shown ? "in" : "pre"} ${className}`}
      style={{ animationDelay: `${delay}ms` }} {...p}>
      {children}
    </Tag>
  );
};

/* ---- Animated counting stat ---- */
const CountStat = ({ to, suffix = "", label }) => {
  const ref = useRef(null);
  const [val, setVal] = useState(0);
  useEffect(() => {
    const el = ref.current;
    let started = false;
    const run = () => {
      if (started) return;
      started = true;
      const dur = 1400, start = performance.now();
      const tick = (now) => {
        const t = Math.min(1, (now - start) / dur);
        const eased = 1 - Math.pow(1 - t, 3);
        setVal(Math.round(to * eased));
        if (t < 1) requestAnimationFrame(tick);
      };
      requestAnimationFrame(tick);
    };
    const r = el.getBoundingClientRect();
    if (r.top < window.innerHeight && r.bottom > 0) { run(); return; }
    const io = new IntersectionObserver(([e]) => {
      if (!e.isIntersecting) return;
      io.disconnect();
      run();
    }, { threshold: 0.5 });
    io.observe(el);
    const t = setTimeout(run, 1400);
    return () => { io.disconnect(); clearTimeout(t); };
  }, [to]);
  return (
    <div className="stat" ref={ref}>
      <div className="num">{val}{suffix}</div>
      <div className="lbl">{label}</div>
    </div>
  );
};

/* ---- Section header ---- */
const SectionHead = ({ eyebrow, title, sub, center, light }) => (
  <div className={`section-head ${center ? "section-head--center" : ""}`}>
    <span className={`eyebrow ${center ? "eyebrow--center" : ""}`}>{eyebrow}</span>
    <h2 className="display h-lg">{title}</h2>
    {sub && <p className="lead">{sub}</p>}
  </div>
);

/* ---- Balochistan-inspired geometric divider (repeating diamond lattice) ---- */
const GeoDivider = ({ tone = "gold" }) => {
  const id = useRef("geo-" + Math.random().toString(36).slice(2, 8)).current;
  const stroke = tone === "light" ? "rgba(247,239,224,.5)" : "rgba(185,122,34,.45)";
  const dot = tone === "light" ? "rgba(240,226,203,.8)" : "rgba(185,122,34,.7)";
  return (
    <div className="geo-divider" aria-hidden="true">
      <svg width="100%" height="34" preserveAspectRatio="xMidYMid">
        <defs>
          <pattern id={id} width="44" height="34" patternUnits="userSpaceOnUse">
            <path d="M22 3 L41 17 L22 31 L3 17 Z" fill="none" stroke={stroke} strokeWidth="1" />
            <path d="M0 17 L3 17 M41 17 L44 17" stroke={stroke} strokeWidth="1" />
            <circle cx="22" cy="17" r="2" fill={dot} />
          </pattern>
        </defs>
        <rect width="100%" height="34" fill={`url(#${id})`} />
      </svg>
    </div>
  );
};

Object.assign(window, { Icon, Reveal, CountStat, SectionHead, GeoDivider });
