// components.jsx — shared atoms & icons (Yukaa Media redesign)

const CALENDLY_URL = "https://calendly.com/yukaamedia/discovery-call";

/* ── Icons ────────────────────────────────────────────────────── */
const Icon = {
  arrUR: (p = {}) => (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" {...p}>
      <path d="M4 12L12 4M12 4H6M12 4V10" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  arrR: (p = {}) => (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" {...p}>
      <path d="M3 8H13M13 8L8.5 3.5M13 8L8.5 12.5" 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 1V13M1 7H13" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
    </svg>
  ),
  minus: (p = {}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}>
      <path d="M1 7H13" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
    </svg>
  ),
  star: (p = {}) => (
    <svg viewBox="0 0 24 24" fill="currentColor" {...p}>
      <path d="M12 2l2.4 7.4H22l-6 4.4 2.3 7.2L12 16.6l-6.3 4.4L8 13.8 2 9.4h7.6L12 2z"/>
    </svg>
  ),
  check: (p = {}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}>
      <path d="M2 7.5L5.5 11L12 3.5" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  spark: (p = {}) => (
    <svg viewBox="0 0 24 24" fill="none" {...p}>
      <path d="M12 3l1.8 5.2L19 10l-5.2 1.8L12 17l-1.8-5.2L5 10l5.2-1.8L12 3z" fill="currentColor"/>
    </svg>
  ),
  menu: (p = {}) => (
    <svg width="20" height="20" viewBox="0 0 20 20" fill="none" {...p}>
      <path d="M3 6h14M3 14h14" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  close: (p = {}) => (
    <svg width="20" height="20" viewBox="0 0 20 20" fill="none" {...p}>
      <path d="M4 4l12 12M16 4L4 16" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  quote: (p = {}) => (
    <svg width="34" height="34" viewBox="0 0 34 34" fill="none" {...p}>
      <path d="M10 22V14a4 4 0 014-4M22 22V14a4 4 0 014-4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
    </svg>
  ),
};

/* ── Eyebrow w/ pulsing dot ───────────────────────────────────── */
function Eyebrow({ children }) {
  return <span className="eyebrow">{children}</span>;
}

/* ── Pill (badge) ─────────────────────────────────────────────── */
function Pill({ children, accent, mono = true, dot = true, style }) {
  return (
    <span className={`pill ${accent ? "pill--accent" : ""}`} style={style}>
      {dot && <span className="dot"></span>}
      {children}
    </span>
  );
}

/* ── Buttons ──────────────────────────────────────────────────── */
function PrimaryButton({ children, href = CALENDLY_URL, large, style }) {
  return (
    <a className={`btn btn--primary ${large ? "btn--lg" : ""}`} href={href} target="_blank" rel="noopener noreferrer" style={style}>
      {children}
      <span className="arr"><Icon.arrUR /></span>
    </a>
  );
}
function GhostButton({ children, href = CALENDLY_URL, large, style, onClick }) {
  const props = onClick ? { onClick } : { href, target: "_blank", rel: "noopener noreferrer" };
  const Tag = onClick ? "button" : "a";
  return (
    <Tag className={`btn btn--ghost ${large ? "btn--lg" : ""}`} style={style} {...props}>
      {children}
      <span className="arr"><Icon.arrUR /></span>
    </Tag>
  );
}

/* ── Reveal — no-op wrapper now (kept for layout structure) ──── */
function Reveal({ children, delay = 0, as: Tag = "div", className = "", style, ...rest }) {
  return (
    <Tag className={`reveal ${className}`} style={style} {...rest}>
      {children}
    </Tag>
  );
}

/* ── CountUp ──────────────────────────────────────────────────── */
function CountUp({ end, suffix = "", duration = 1200 }) {
  const ref = React.useRef(null);
  const [val, setVal] = React.useState(end);
  const startedRef = React.useRef(false);

  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;

    const begin = () => {
      if (startedRef.current) return;
      startedRef.current = true;
      setVal(0);
      const start = performance.now();
      const stepMs = 16;
      const steps = Math.ceil(duration / stepMs);
      let i = 0;
      const id = setInterval(() => {
        i++;
        const p = Math.min(1, i / steps);
        const eased = 1 - Math.pow(1 - p, 3);
        setVal(eased * end);
        if (p >= 1) clearInterval(id);
      }, stepMs);
      // hard safety net
      setTimeout(() => { clearInterval(id); setVal(end); }, duration + 600);
    };

    const check = () => {
      const r = el.getBoundingClientRect();
      const vh = window.innerHeight || document.documentElement.clientHeight;
      if (r.top < vh && r.bottom > 0) { begin(); return true; }
      return false;
    };

    if (check()) return;

    let io;
    if ("IntersectionObserver" in window) {
      io = new IntersectionObserver(([e]) => {
        if (e.isIntersecting) { begin(); io && io.disconnect(); }
      }, { threshold: 0.3 });
      io.observe(el);
    }
    const onScroll = () => { if (check()) { window.removeEventListener("scroll", onScroll); io && io.disconnect(); } };
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => { io && io.disconnect(); window.removeEventListener("scroll", onScroll); };
  }, [end, duration]);

  const display = end % 1 === 0 ? Math.round(val) : val.toFixed(1);
  return <span ref={ref}>{display}{suffix}</span>;
}

/* expose globally so other Babel scripts see them */
Object.assign(window, {
  Icon, Eyebrow, Pill, PrimaryButton, GhostButton, Reveal, CountUp, CALENDLY_URL,
});
