// Shared primitives for Minicorn site
const { useState, useEffect, useRef } = React;

function Reveal({ children, delay = 0, as: Tag = 'div', className = '', style = {} }) {
  const ref = useRef(null);
  const [shown, setShown] = useState(false);
  useEffect(() => {
    const el = ref.current; if (!el) return;
    // Sync check first — if already in view (above the fold), reveal immediately.
    const r = el.getBoundingClientRect();
    if (r.top < window.innerHeight && r.bottom > 0) {
      setShown(true);
      return;
    }
    const io = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) { setShown(true); io.disconnect(); }
    }, { threshold: 0.05 });
    io.observe(el);
    // Safety fallback: ensure visible after 1.5s no matter what.
    const t = setTimeout(() => setShown(true), 1500);
    return () => { io.disconnect(); clearTimeout(t); };
  }, []);
  return (
    <Tag ref={ref} className={`reveal ${shown ? 'in' : ''} ${className}`}
         style={{ transitionDelay: `${delay}ms`, ...style }}>
      {children}
    </Tag>
  );
}

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

// Minicorn horn — twisted unicorn horn used as the logo mark
function Horn({ size = 28 }) {
  const s = size * 0.9;
  return (
    <svg width={s} height={s} viewBox="0 0 32 32" fill="none"
         aria-hidden="true"
         style={{
           flexShrink: 0,
           filter: 'drop-shadow(0 0 6px rgba(251,191,36,0.55))',
         }}>
      <defs>
        <linearGradient id="hornFill" x1="50%" y1="0%" x2="50%" y2="100%">
          <stop offset="0%" stopColor="#FFF6D6"/>
          <stop offset="35%" stopColor="#FBE38A"/>
          <stop offset="70%" stopColor="#F2B431"/>
          <stop offset="100%" stopColor="#A4720B"/>
        </linearGradient>
        <linearGradient id="hornShade" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" stopColor="rgba(255,255,255,0.55)"/>
          <stop offset="50%" stopColor="rgba(255,255,255,0)"/>
          <stop offset="100%" stopColor="rgba(80,50,0,0.35)"/>
        </linearGradient>
      </defs>
      {/* Horn body — tapered cone, vertical */}
      <path d="M16 2 L20.5 28 Q16 30.5 11.5 28 Z" fill="url(#hornFill)"/>
      <path d="M16 2 L20.5 28 Q16 30.5 11.5 28 Z" fill="url(#hornShade)" opacity="0.75"/>
      {/* Spiral ridges */}
      <path d="M12.4 24 Q16 22.6 19.6 24" stroke="rgba(125,82,4,0.6)" strokeWidth="1" fill="none" strokeLinecap="round"/>
      <path d="M13 19 Q16 17.8 19 19" stroke="rgba(125,82,4,0.55)" strokeWidth="0.9" fill="none" strokeLinecap="round"/>
      <path d="M13.7 14 Q16 13.1 18.3 14" stroke="rgba(125,82,4,0.5)" strokeWidth="0.8" fill="none" strokeLinecap="round"/>
      <path d="M14.4 9.5 Q16 8.9 17.6 9.5" stroke="rgba(125,82,4,0.45)" strokeWidth="0.7" fill="none" strokeLinecap="round"/>
      {/* Highlight on the tip */}
      <path d="M16 3 L16.6 7" stroke="rgba(255,255,255,0.95)" strokeWidth="0.8" strokeLinecap="round"/>
    </svg>
  );
}

// Wordmark — using Zain at lowercase per user's note
function Wordmark({ size = 28 }) {
  return (
    <span style={{
      fontFamily: 'var(--display)', fontWeight: 800,
      fontSize: size, lineHeight: 1, letterSpacing: '-0.04em',
      display: 'inline-flex', alignItems: 'center',
      color: 'var(--ink)',
    }}>
      minicorn
    </span>
  );
}

function SectionLabel({ n, children }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 24 }}>
      <span className="eyebrow">[ {String(n).padStart(2,'0')} ]</span>
      <span style={{ height: 1, flex: 0, width: 32, background: 'var(--line-2)' }}/>
      <span className="eyebrow" style={{ color: 'var(--ink-3)' }}>{children}</span>
    </div>
  );
}

// Used everywhere — phone shell with glass screen and a slot
function PhoneFrame({ children, width = 280, scale = 1 }) {
  const w = width;
  const h = w * 2.05;
  return (
    <div style={{
      width: w, height: h, position: 'relative',
      borderRadius: w * 0.13,
      background: 'linear-gradient(160deg, #2a1d4a 0%, #15102d 100%)',
      padding: 6,
      boxShadow:
        '0 40px 80px -20px rgba(0,0,0,0.7), inset 0 0 0 1px rgba(255,255,255,0.08), inset 0 1px 0 rgba(255,255,255,0.18)',
      transform: `scale(${scale})`,
      transformOrigin: 'center top',
    }}>
      <div style={{
        position: 'absolute', top: 14, left: '50%', transform: 'translateX(-50%)',
        width: 90, height: 24, borderRadius: 14, background: '#000', zIndex: 5,
      }}/>
      <div style={{
        width: '100%', height: '100%',
        borderRadius: w * 0.115,
        background: 'linear-gradient(180deg, #110828 0%, #0a0518 100%)',
        position: 'relative', overflow: 'hidden',
      }}>
        {children}
      </div>
    </div>
  );
}

Object.assign(window, { Reveal, Arrow, Horn, Wordmark, SectionLabel, PhoneFrame });
