// Shared utilities: animated counters, intersection observer helpers, icons.

const { useState, useEffect, useRef, useMemo, useCallback } = React;

// Animated counter — runs once when element enters viewport
function Counter({ to, suffix = "", duration = 1800, decimals = 0, prefix = "" }) {
  const [v, setV] = useState(0);
  const ref = useRef(null);
  const started = useRef(false);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const io = new IntersectionObserver((es) => {
      es.forEach((e) => {
        if (e.isIntersecting && !started.current) {
          started.current = true;
          const start = performance.now();
          const from = 0;
          const tick = (t) => {
            const p = Math.min(1, (t - start) / duration);
            const eased = 1 - Math.pow(1 - p, 3);
            setV(from + (to - from) * eased);
            if (p < 1) requestAnimationFrame(tick);
          };
          requestAnimationFrame(tick);
        }
      });
    }, { threshold: 0.3 });
    io.observe(el);
    return () => io.disconnect();
  }, [to, duration]);
  const display = decimals === 0
    ? Math.round(v).toLocaleString("es-ES")
    : v.toFixed(decimals);
  return <span ref={ref} className="mono">{prefix}{display}{suffix}</span>;
}

// Icons (minimal hand-tuned, no AI slop)
const I = {
  arrow: (p={}) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M3 11L11 3M11 3H5M11 3V9" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>,
  arrowR: (p={}) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M2 7H12M12 7L8 3M12 7L8 11" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  truck: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M2 7h11v9H2zM13 10h5l3 3v3h-8z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/><circle cx="6" cy="17.5" r="2" stroke="currentColor" strokeWidth="1.4"/><circle cx="17" cy="17.5" r="2" stroke="currentColor" strokeWidth="1.4"/></svg>,
  snow: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M12 3v18M3 12h18M5.5 5.5l13 13M18.5 5.5l-13 13" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>,
  globe: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><circle cx="12" cy="12" r="9" stroke="currentColor" strokeWidth="1.4"/><path d="M3 12h18M12 3c3 3 3 15 0 18M12 3c-3 3-3 15 0 18" stroke="currentColor" strokeWidth="1.4"/></svg>,
  shield: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M12 3l8 3v6c0 5-3.5 8-8 9-4.5-1-8-4-8-9V6l8-3z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/></svg>,
  leaf: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M20 4c0 9-6 15-15 15-2 0-2 0-2 0 0-9 6-15 15-15 2 0 2 0 2 0zM4 19L14 9" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/></svg>,
  pill: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><rect x="3" y="9" width="18" height="6" rx="3" stroke="currentColor" strokeWidth="1.4"/><path d="M12 9v6" stroke="currentColor" strokeWidth="1.4"/></svg>,
  box: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M3 7l9-4 9 4-9 4-9-4zM3 7v10l9 4V11M21 7v10l-9 4" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/></svg>,
  thermo: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><rect x="9" y="3" width="6" height="14" rx="3" stroke="currentColor" strokeWidth="1.4"/><circle cx="12" cy="19" r="3" stroke="currentColor" strokeWidth="1.4"/><path d="M12 8v8" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>,
  signal: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M3 17l4-4 4 3 4-7 6 5" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  pin: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M12 21s7-7 7-12a7 7 0 10-14 0c0 5 7 12 7 12z" stroke="currentColor" strokeWidth="1.4"/><circle cx="12" cy="9" r="2.5" stroke="currentColor" strokeWidth="1.4"/></svg>,
  spark: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M12 3v6M12 15v6M3 12h6M15 12h6M5.6 5.6l4.2 4.2M14.2 14.2l4.2 4.2M5.6 18.4l4.2-4.2M14.2 9.8l4.2-4.2" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round"/></svg>,
  cert: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><circle cx="12" cy="10" r="6" stroke="currentColor" strokeWidth="1.4"/><path d="M8 14l-2 7 6-3 6 3-2-7" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/></svg>,
  check: (p={}) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M3 7.5l3 3 5-7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  plus: (p={}) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M7 2v10M2 7h10" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>,
  minus: (p={}) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M2 7h10" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>,
  menu: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M4 8h16M4 16h16" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>,
  close: (p={}) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" {...p}><path d="M5 5l14 14M19 5L5 19" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>,
  play: (p={}) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M4 3l8 4-8 4V3z" fill="currentColor"/></svg>,
};

// Small live status indicator dot
function LiveDot({ color = "var(--accent-ok)", label }) {
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 8, fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: ".06em", color: "inherit" }}>
      <span style={{
        position: "relative", width: 8, height: 8, borderRadius: 999, background: color,
        boxShadow: `0 0 0 0 ${color}`,
      }}>
        <span style={{
          position: "absolute", inset: -4, borderRadius: 999, border: `1px solid ${color}`,
          animation: "pulse 1.6s ease-out infinite", opacity: .7
        }}/>
      </span>
      {label}
      <style>{`@keyframes pulse { 0% { transform: scale(.6); opacity:.8 } 100% { transform: scale(1.7); opacity:0 } }`}</style>
    </span>
  );
}

// Section header (eyebrow + heading + lede)
function SectionHead({ eyebrow, title, lede, align = "left", inverse = false }) {
  return (
    <div className="reveal" style={{ maxWidth: 720, textAlign: align, margin: align === "center" ? "0 auto" : 0 }}>
      {eyebrow && <div className="eyebrow" style={{ marginBottom: 18 }}>{eyebrow}</div>}
      <h2 style={{ marginBottom: 18 }}>{title}</h2>
      {lede && <p style={{ fontSize: 18, maxWidth: 620, marginLeft: align === "center" ? "auto" : 0, marginRight: align === "center" ? "auto" : 0 }}>{lede}</p>}
    </div>
  );
}

// Marquee
function Marquee({ children, speed = 40 }) {
  return (
    <div style={{ overflow: "hidden", maskImage: "linear-gradient(90deg, transparent, #000 6%, #000 94%, transparent)" }}>
      <div style={{ display: "inline-flex", gap: 48, whiteSpace: "nowrap", animation: `marq ${speed}s linear infinite` }}>
        {children}{children}
      </div>
      <style>{`@keyframes marq { from { transform: translateX(0) } to { transform: translateX(-50%) } }`}</style>
    </div>
  );
}

Object.assign(window, { Counter, I, LiveDot, SectionHead, Marquee });
