/* global React */
const DS = window.SubEarthDesignSystem_9ae0a6;

function Nav({ onBuy }) {
  const { Button } = window.SubEarthDesignSystem_9ae0a6;
  const [scrolled, setScrolled] = React.useState(false);
  React.useEffect(() => {
    const el = document.querySelector('[data-scroll]') || window;
    const onScroll = () => {
      const y = el === window ? window.scrollY : el.scrollTop;
      setScrolled(y > 24);
    };
    el.addEventListener('scroll', onScroll, { passive: true });
    return () => el.removeEventListener('scroll', onScroll);
  }, []);

  const links = [
    { label: 'Chonkify', href: '#top' },
    { label: 'Included', href: '#included' },
    { label: 'Plan', href: '#plan' },
    { label: 'FAQ', href: '#faq' },
  ];
  return (
    <header style={{
      position: 'sticky', top: 0, zIndex: 50,
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '0 var(--gutter)', height: '72px',
      background: scrolled ? 'var(--surface-glass)' : 'transparent',
      backdropFilter: scrolled ? 'var(--blur)' : 'none',
      WebkitBackdropFilter: scrolled ? 'var(--blur)' : 'none',
      boxShadow: scrolled ? 'inset 0 -1px 0 var(--line)' : 'inset 0 -1px 0 transparent',
      transition: 'background var(--dur) var(--ease-out), box-shadow var(--dur) var(--ease-out)',
    }}>
      <Wordmark />
      <nav style={{ display: 'flex', gap: '34px' }} className="nav-links">
        {links.map((l) => (
          <a key={l.label} href={l.href} style={{
            fontFamily: 'var(--font-mono)', fontSize: 'var(--fs-2xs)',
            letterSpacing: 'var(--tr-mono)', textTransform: 'uppercase',
            color: 'var(--text-tertiary)', transition: 'color var(--dur) var(--ease-out)',
          }}
          onMouseEnter={(e) => e.currentTarget.style.color = 'var(--text-primary)'}
          onMouseLeave={(e) => e.currentTarget.style.color = 'var(--text-tertiary)'}>{l.label}</a>
        ))}
      </nav>
      <div style={{ display: 'flex', alignItems: 'center', gap: '14px' }}>
        <span style={{
          fontFamily: 'var(--font-mono)', fontSize: 'var(--fs-2xs)',
          letterSpacing: 'var(--tr-mono)', color: 'var(--text-tertiary)',
        }}>$39</span>
        <Button variant="primary" size="sm" onClick={onBuy}>Buy</Button>
      </div>
    </header>
  );
}

function Wordmark({ size = 22 }) {
  return (
    <a href="#top" style={{
      display: 'inline-flex', alignItems: 'baseline',
      fontFamily: 'var(--font-display)', fontWeight: 700,
      fontSize: size + 'px', letterSpacing: '-0.03em', color: 'var(--text-primary)',
    }}>
      sub
      <span style={{
        display: 'inline-block', width: size * 0.22 + 'px', height: size * 0.22 + 'px',
        borderRadius: '3px', background: 'var(--paper)', margin: '0 3px',
        boxShadow: 'var(--glow-sm)', transform: 'translateY(-1px)',
      }} />
      earth
    </a>
  );
}

// Make both globally available for Footer.jsx
window.Nav = Nav;
window.Wordmark = Wordmark;
window.__appReady.Nav = true;
