// primitives.jsx — small shared building blocks
// (helpers used across sections)

/* ── useInView: animate sections on scroll ──────────────── */
function useInView(threshold = 0.15) {
  const ref = React.useRef(null);
  const [seen, setSeen] = React.useState(false);
  React.useEffect(() => {
    if (!ref.current || seen) return;
    const io = new IntersectionObserver(
      (entries) => {
        entries.forEach((e) => { if (e.isIntersecting) { setSeen(true); io.disconnect(); } });
      },
      { threshold }
    );
    io.observe(ref.current);
    return () => io.disconnect();
  }, [seen, threshold]);
  return [ref, seen];
}

/* ── CountUp: animated number from 0 to target ───────────── */
function CountUp({ to, duration = 1400, suffix = '', prefix = '', decimals = 0, start = true }) {
  const [val, setVal] = React.useState(0);
  React.useEffect(() => {
    if (!start) return;
    let raf;
    const startT = performance.now();
    const tick = (now) => {
      const t = Math.min(1, (now - startT) / duration);
      const eased = 1 - Math.pow(1 - t, 3);
      setVal(eased * to);
      if (t < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [to, duration, start]);

  const formatted = decimals > 0
    ? val.toFixed(decimals)
    : Math.round(val).toLocaleString('pl-PL');
  return <span>{prefix}{formatted}{suffix}</span>;
}

/* ── Eyebrow chip ───────────────────────────────────────── */
function Eyebrow({ children, dot = true }) {
  return (
    <span className="eyebrow">
      {dot && <span className="dot" />}
      {children}
    </span>
  );
}

/* ── ArrowRight ─────────────────────────────────────────── */
function ArrowRight({ size = 14 }) {
  return (
    <svg className="arrow" width={size} height={size} viewBox="0 0 14 14" fill="none">
      <path d="M2 7h10M8 3l4 4-4 4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

/* ── Logo (Gorilla Commerce & Solutions) ────────────────── */
function Logo({ size = 18, mark = size + 10 }) {
  return (
    <div className="brand-logo" style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
      <span
        aria-hidden="true"
        className="brand-mark"
        style={{
          width: mark, height: mark,
          backgroundColor: 'currentColor',
          WebkitMask: 'url(assets/gorilla-logo.svg) center / contain no-repeat',
          mask: 'url(assets/gorilla-logo.svg) center / contain no-repeat',
          display: 'inline-block',
          flexShrink: 0,
        }}
      />
      <span className="brand-name" style={{
        fontFamily: 'var(--font-display)',
        fontWeight: 600, letterSpacing: '-0.02em',
        fontSize: size, lineHeight: 1,
        whiteSpace: 'nowrap',
      }}>
        Gorilla <span className="brand-name-sub" style={{ color: 'var(--ink-3)', fontWeight: 400 }}>Commerce&nbsp;&&nbsp;Solutions</span>
      </span>
      <style>{`
        @media (max-width: 520px){
          .brand-logo .brand-name-sub { display: none; }
        }
      `}</style>
    </div>
  );
}

Object.assign(window, { useInView, CountUp, Eyebrow, ArrowRight, Logo });
