/* global React */
// Meefi landing — shared primitives + Lucide icons (inlined)
const { useState, useEffect, useRef, useLayoutEffect, useMemo, useCallback } = React;

// ============================================================
// Icon system — Lucide subset
// ============================================================
const SVG = ({ children, size = 22, stroke = 1.5, color = "currentColor", style, className }) => (
  <svg
    width={size}
    height={size}
    viewBox="0 0 24 24"
    fill="none"
    stroke={color}
    strokeWidth={stroke}
    strokeLinecap="round"
    strokeLinejoin="round"
    style={style}
    className={className}
    aria-hidden="true"
  >{children}</svg>
);

const Icons = {
  Banknote:     (p) => <SVG {...p}><rect x="2" y="6" width="20" height="12" rx="2"/><circle cx="12" cy="12" r="2"/><path d="M6 12h.01M18 12h.01"/></SVG>,
  CreditCard:   (p) => <SVG {...p}><rect width="20" height="14" x="2" y="5" rx="2"/><line x1="2" x2="22" y1="10" y2="10"/></SVG>,
  Receipt:      (p) => <SVG {...p}><path d="M4 2v20l2-1 2 1 2-1 2 1 2-1 2 1 2-1 2 1V2l-2 1-2-1-2 1-2-1-2 1-2-1-2 1Z"/><path d="M16 8h-6a2 2 0 1 0 0 4h4a2 2 0 1 1 0 4H8"/><path d="M12 17.5v-11"/></SVG>,
  FileText:     (p) => <SVG {...p}><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/></SVG>,
  BarChart3:    (p) => <SVG {...p}><path d="M3 3v18h18"/><path d="M7 16V8M12 16v-5M17 16v-3"/></SVG>,
  Wallet:       (p) => <SVG {...p}><path d="M19 5H5a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2Z"/><path d="M16 12h.01"/></SVG>,
  Shield:       (p) => <SVG {...p}><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></SVG>,
  Lock:         (p) => <SVG {...p}><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></SVG>,
  Settings:     (p) => <SVG {...p}><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></SVG>,
  ArrowUpRight: (p) => <SVG {...p}><path d="M7 17 17 7"/><path d="M7 7h10v10"/></SVG>,
  ArrowRight:   (p) => <SVG {...p}><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></SVG>,
  Check:        (p) => <SVG {...p}><path d="M20 6 9 17l-5-5"/></SVG>,
  ChevronDown:  (p) => <SVG {...p}><path d="m6 9 6 6 6-6"/></SVG>,
  Plus:         (p) => <SVG {...p}><path d="M12 5v14M5 12h14"/></SVG>,
  Zap:          (p) => <SVG {...p}><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></SVG>,
  TrendingUp:   (p) => <SVG {...p}><polyline points="22 7 13.5 15.5 8.5 10.5 2 17"/><polyline points="16 7 22 7 22 13"/></SVG>,
  Globe:        (p) => <SVG {...p}><circle cx="12" cy="12" r="10"/><path d="M2 12h20"/><path d="M12 2a15 15 0 0 1 0 20 15 15 0 0 1 0-20"/></SVG>,
  Users:        (p) => <SVG {...p}><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></SVG>,
  Sparkles:     (p) => <SVG {...p}><path d="m12 3-1.9 5.8a2 2 0 0 1-1.3 1.3L3 12l5.8 1.9a2 2 0 0 1 1.3 1.3L12 21l1.9-5.8a2 2 0 0 1 1.3-1.3L21 12l-5.8-1.9a2 2 0 0 1-1.3-1.3Z"/><path d="M5 3v4M3 5h4M19 17v4M17 19h4"/></SVG>,
  X:            (p) => <SVG {...p}><path d="M18 6 6 18"/><path d="m6 6 12 12"/></SVG>,
};

// ============================================================
// Magnetic button — subtle pull toward cursor
// ============================================================
function MagneticButton({ children, variant = "primary", href, onClick, magnet = 8, style, className, ...rest }) {
  const Tag = href ? "a" : "button";
  return (
    <Tag
      href={href}
      onClick={onClick}
      data-hover="1"
      className={`btn-magnetic ${variant}${className ? " " + className : ""}`}
      style={style}
      {...rest}
    >
      {children}
    </Tag>
  );
}

// ============================================================
// Section heading — two-line setup + payoff (with eyebrow)
// ============================================================
function SectionHeading({ eyebrow, statement, twist }) {
  return (
    <div className="section-h reveal">
      {eyebrow && (
        <div className="eyebrow">
          <span className="dot"/> {eyebrow}
        </div>
      )}
      <h2>
        <span style={{ whiteSpace: "nowrap" }}>{statement}</span>
        {twist && <span className="twist">{twist}</span>}
      </h2>
    </div>
  );
}

// ============================================================
// Reveal-on-scroll wrapper — adds .is-visible when in viewport
// ============================================================
function useReveal() {
  useEffect(() => {
    const els = document.querySelectorAll(".reveal:not(.is-visible)");
    if (!("IntersectionObserver" in window)) {
      els.forEach((e) => e.classList.add("is-visible"));
      return;
    }
    const io = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            entry.target.classList.add("is-visible");
            io.unobserve(entry.target);
          }
        });
      },
      { threshold: 0.12, rootMargin: "0px 0px -60px 0px" }
    );
    els.forEach((e) => io.observe(e));
    return () => io.disconnect();
  });
}

// ============================================================
// Animated counter — counts up when visible
// ============================================================
function Counter({ value, prefix = "", suffix = "", duration = 1400 }) {
  const [n, setN] = useState(0);
  const ref = useRef(null);
  const started = useRef(false);

  useEffect(() => {
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver((entries) => {
      entries.forEach((entry) => {
        if (entry.isIntersecting && !started.current) {
          started.current = true;
          const start = performance.now();
          const step = (t) => {
            const p = Math.min(1, (t - start) / duration);
            // ease-out-quint
            const eased = 1 - Math.pow(1 - p, 5);
            setN(value * eased);
            if (p < 1) requestAnimationFrame(step);
          };
          requestAnimationFrame(step);
          io.disconnect();
        }
      });
    }, { threshold: 0.4 });
    io.observe(el);
    return () => io.disconnect();
  }, [value, duration]);

  const display = Math.round(n);
  const fmt = (x) => `${prefix}${x.toLocaleString("es-MX")}${suffix}`;
  // Reserve the final value's width with a hidden spacer so the number
  // never changes the layout width as it counts up from 0 → value.
  // Without this the growing digits widen the flex row mid-animation and
  // tip the trust-metrics line into a wrap. The live value is overlaid in
  // the same grid cell and stays centred inside the reserved box.
  return (
    <span ref={ref} className="ticker-digit" style={{ display: "inline-grid", justifyItems: "center" }}>
      <span aria-hidden="true" style={{ gridArea: "1 / 1", visibility: "hidden" }}>{fmt(value)}</span>
      <span style={{ gridArea: "1 / 1" }}>{fmt(display)}</span>
    </span>
  );
}

// ============================================================
// Footnote reference — small numbered superscript placed right
// after a claim that needs legal backing. Styling lives in
// styles.css (.fn-ref); line-height:0 keeps it from disturbing
// the text flow / line height on small screens.
// ============================================================
function Fn({ n }) {
  return <sup className="fn-ref">{n}</sup>;
}

// Meta Pixel "Lead" conversion event — fired from the Apply now / Book a
// demo CTAs so ad campaigns can attribute clicks.
function trackLead() {
  if (typeof window.fbq === "function") window.fbq("track", "Lead");
}

Object.assign(window, {
  Icons, SVG, MagneticButton, SectionHeading, useReveal, Counter, Fn, trackLead,
});
