// Shared hooks & utilities — exported to window for all section files

const useInView = (threshold = 0.12) => {
  const ref = React.useRef(null);
  const [inView, setInView] = React.useState(false);
  React.useEffect(() => {
    const obs = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) { setInView(true); obs.disconnect(); }
    }, { threshold });
    if (ref.current) obs.observe(ref.current);
    return () => obs.disconnect();
  }, []);
  return [ref, inView];
};

const useCounter = (target, inView, duration = 1400) => {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    if (!inView) return;
    let current = 0;
    const step = target / (duration / 16);
    const timer = setInterval(() => {
      current += step;
      if (current >= target) { setCount(target); clearInterval(timer); }
      else setCount(Math.floor(current));
    }, 16);
    return () => clearInterval(timer);
  }, [inView, target]);
  return count;
};

const scrollToSection = (id) => {
  const el = document.getElementById(id);
  if (!el) return;
  el.scrollIntoView({ behavior: 'smooth', block: 'start' });
};

const SectionLabel = ({ children }) => (
  <p style={{ fontFamily: "'Barlow Condensed', sans-serif", letterSpacing: '0.25em', fontSize: '0.7rem', fontWeight: 700, color: 'var(--red)', textTransform: 'uppercase', marginBottom: '0.75rem' }}>
    {children}
  </p>
);

const SectionTitle = ({ children, light = false }) => (
  <h2 style={{ fontFamily: "'Barlow Condensed', sans-serif", fontWeight: 800, fontSize: 'clamp(2rem, 4vw, 3.2rem)', lineHeight: 1.05, color: light ? 'var(--text)' : 'var(--text)', letterSpacing: '-0.01em', marginBottom: '0.5rem' }}>
    {children}
  </h2>
);

const RedLine = ({ width = 48, style = {} }) => (
  <div style={{ width, height: 3, background: 'var(--red)', borderRadius: 2, marginBottom: '2rem', ...style }} />
);

const Placeholder = ({ label = 'Image placeholder', width = '100%', height = 280, style = {} }) => (
  <div style={{ width, height, background: 'var(--card)', border: '1px dashed var(--border)', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 8, borderRadius: 4, position: 'relative', overflow: 'hidden', ...style }}>
    <svg width="100%" height="100%" style={{ position: 'absolute', inset: 0, opacity: 0.3 }} preserveAspectRatio="none">
      <defs>
        <pattern id={`stripe-${label.replace(/\s/g,'')}`} patternUnits="userSpaceOnUse" width="20" height="20" patternTransform="rotate(45)">
          <line x1="0" y1="0" x2="0" y2="20" stroke="var(--border)" strokeWidth="8" />
        </pattern>
      </defs>
      <rect width="100%" height="100%" fill={`url(#stripe-${label.replace(/\s/g,'')})`} />
    </svg>
    <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="var(--muted)" strokeWidth="1.5" style={{ position: 'relative', zIndex: 1 }}>
      <rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/>
    </svg>
    <span style={{ fontFamily: 'monospace', fontSize: '0.68rem', color: 'var(--muted)', position: 'relative', zIndex: 1, textAlign: 'center', padding: '0 1rem' }}>{label}</span>
  </div>
);

Object.assign(window, { useInView, useCounter, scrollToSection, SectionLabel, SectionTitle, RedLine, Placeholder });
