// Shared atomic components for the USANRetirement landing page

const { useState, useEffect, useRef } = React;

// ---------- Waveform (Clara) ----------
function Waveform({ bars = 28, height = 80, gap = 4, barWidth = 5 }) {
  const heights = React.useMemo(() => {
    return Array.from({ length: bars }, (_, i) => {
      // smooth wave-y baseline
      const t = i / bars;
      const base = 25 + Math.sin(t * Math.PI * 2.2) * 25 + Math.cos(t * Math.PI * 4) * 18;
      return Math.max(15, Math.min(95, base + Math.random() * 18));
    });
  }, [bars]);

  return (
    <div className="wave" style={{ height, gap }}>
      {heights.map((h, i) => (
        <div
          key={i}
          className="wave-bar"
          style={{
            width: barWidth,
            height: `${h}%`,
            animationDelay: `${(i * 0.06) % 1.6}s`,
            animationDuration: `${1.4 + (i % 3) * 0.2}s`,
          }}
        />
      ))}
    </div>
  );
}

// ---------- Soft tinted photo placeholder ----------
function Photo({ label, ratio = '4/3', tint = 'warm', src, style = {}, className = '' }) {
  const tintMap = {
    warm: 'linear-gradient(135deg, var(--accent-soft) 0%, var(--cream-300) 100%)',
    cool: 'linear-gradient(135deg, var(--sage-soft) 0%, var(--cream-200) 100%)',
    sun: 'linear-gradient(135deg, oklch(0.93 0.05 80) 0%, var(--cream-300) 100%)',
    dusk: 'linear-gradient(135deg, oklch(0.88 0.06 50) 0%, oklch(0.92 0.03 30) 100%)',
  };
  if (src) {
    const { backgroundPosition, ...restStyle } = style;
    return (
      <div
        className={`photo-ph ${className}`}
        style={{
          aspectRatio: ratio,
          backgroundImage: `url("${src}"), ${tintMap[tint] || tintMap.warm}`,
          backgroundSize: 'cover, cover',
          backgroundRepeat: 'no-repeat',
          backgroundPosition: backgroundPosition || 'center',
          ...restStyle,
        }}
      />
    );
  }
  return (
    <div
      className={`photo-ph ${className}`}
      style={{ aspectRatio: ratio, background: tintMap[tint] || tintMap.warm, ...style }}
    >
      <span className="ph-label">{label}</span>
    </div>
  );
}

// ---------- Section header ----------
function SectionHeader({ eyebrow, title, intro, align = 'left', maxWidth = 720 }) {
  return (
    <div style={{ textAlign: align, marginLeft: align === 'center' ? 'auto' : 0, marginRight: align === 'center' ? 'auto' : 0, maxWidth }}>
      {eyebrow && <div className="eyebrow">{eyebrow}</div>}
      <h2>{title}</h2>
      {intro && <p style={{ marginTop: 18, color: 'var(--ink-700)', fontSize: 19, lineHeight: 1.55 }}>{intro}</p>}
    </div>
  );
}

// ---------- Reveal-on-scroll wrapper ----------
function Reveal({ children, delay = 0, as = 'div', ...props }) {
  const ref = useRef(null);
  const [visible, setVisible] = useState(false);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const obs = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting) {
          setTimeout(() => setVisible(true), delay);
          obs.disconnect();
        }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -60px 0px' });
    obs.observe(el);
    return () => obs.disconnect();
  }, [delay]);
  const Tag = as;
  return (
    <Tag ref={ref} className={`fade-in ${visible ? 'is-visible' : ''} ${props.className || ''}`} style={props.style}>
      {children}
    </Tag>
  );
}

// ---------- Icon set (minimal stroke icons) ----------
const Icon = {
  Phone: (p) => (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M5 4h4l2 5-2.5 1.5a11 11 0 005 5L15 13l5 2v4a2 2 0 01-2 2A16 16 0 013 6a2 2 0 012-2z"/>
    </svg>
  ),
  Sun: (p) => (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="12" r="4"/>
      <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/>
    </svg>
  ),
  Pill: (p) => (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="3" y="9" width="18" height="6" rx="3" transform="rotate(-30 12 12)"/>
      <path d="M9 7l6 10"/>
    </svg>
  ),
  Chat: (p) => (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M21 11.5a8.4 8.4 0 01-.9 3.8 8.5 8.5 0 01-7.6 4.7 8.4 8.4 0 01-3.8-.9L3 21l1.9-5.7a8.4 8.4 0 01-.9-3.8 8.5 8.5 0 014.7-7.6 8.4 8.4 0 013.8-.9h.5a8.5 8.5 0 018 8v.5z"/>
    </svg>
  ),
  Heart: (p) => (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M20.84 4.61a5.5 5.5 0 00-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 00-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 000-7.78z"/>
    </svg>
  ),
  Search: (p) => (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="11" cy="11" r="7"/>
      <path d="m21 21-4.3-4.3"/>
    </svg>
  ),
  Bell: (p) => (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M6 8a6 6 0 0112 0c0 7 3 9 3 9H3s3-2 3-9"/>
      <path d="M10.3 21a1.94 1.94 0 003.4 0"/>
    </svg>
  ),
  Check: (p) => (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <polyline points="20 6 9 17 4 12"/>
    </svg>
  ),
  X: (p) => (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <line x1="18" y1="6" x2="6" y2="18"/>
      <line x1="6" y1="6" x2="18" y2="18"/>
    </svg>
  ),
  ArrowRight: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <line x1="5" y1="12" x2="19" y2="12"/>
      <polyline points="12 5 19 12 12 19"/>
    </svg>
  ),
  Plus: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" {...p}>
      <line x1="12" y1="5" x2="12" y2="19"/>
      <line x1="5" y1="12" x2="19" y2="12"/>
    </svg>
  ),
  Minus: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" {...p}>
      <line x1="5" y1="12" x2="19" y2="12"/>
    </svg>
  ),
  Lock: (p) => (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="3" y="11" width="18" height="11" rx="2" ry="2"/>
      <path d="M7 11V7a5 5 0 0110 0v4"/>
    </svg>
  ),
};

Object.assign(window, { Waveform, Photo, SectionHeader, Reveal, Icon });
