/* global React */
const { useEffect: useEffectLegal, useState: useStateLegal, useRef: useRefLegal } = React;

/* ----------------------------------------------------------------- */
/*  ASPACE — Legal Page Framework                                     */
/* ----------------------------------------------------------------- */

function FadeUpL({ children, delay = 0, style }) {
  const ref = useRefLegal(null);
  const [seen, setSeen] = useStateLegal(false);
  useEffectLegal(() => {
    if (!ref.current) return;
    const io = new IntersectionObserver(([e]) => { if (e.isIntersecting) setSeen(true); }, { threshold: 0.18 });
    io.observe(ref.current);
    return () => io.disconnect();
  }, []);
  return (
    <div ref={ref} style={{
      ...style,
      opacity: seen ? 1 : 0,
      transform: seen ? 'translateY(0)' : 'translateY(20px)',
      transition: `opacity 880ms ease ${delay}ms, transform 880ms cubic-bezier(0.22,1,0.36,1) ${delay}ms`,
    }}>{children}</div>
  );
}

function LegalHero({ eyebrow, lines }) {
  return (
    <section data-screen-label={`Legal / Hero / ${eyebrow}`} style={{
      background: '#0A0A0B', color: '#fff',
      padding: 'clamp(140px, 18vh, 200px) clamp(20px, 5vw, 80px) clamp(80px, 10vh, 140px)',
      position: 'relative', overflow: 'hidden',
    }}>
      <div style={{
        position: 'absolute', top: 'clamp(110px, 14vh, 150px)', left: 0, right: 0,
        padding: '0 clamp(20px, 5vw, 80px)',
        display: 'flex', justifyContent: 'space-between',
        fontSize: 11, letterSpacing: '0.18em', textTransform: 'uppercase',
        color: 'rgba(255,255,255,0.55)',
      }}>
        <span>{eyebrow}</span>
        <span>Updated · May 2026</span>
      </div>

      <div style={{ marginTop: 'clamp(60px, 8vh, 100px)' }}>
        {lines.map((l, i) => (
          <div key={i} style={{ overflow: 'hidden', lineHeight: 0.92 }}>
            <div style={{
              fontFamily: "'Inter Tight',sans-serif", fontWeight: 500,
              fontSize: 'clamp(48px, 9vw, 160px)',
              letterSpacing: '-0.035em',
              color: l.blue ? 'var(--aspace-blue)' : '#fff',
              animation: `legalIn 1100ms cubic-bezier(0.22,1,0.36,1) ${200 + i * 90}ms backwards`,
            }}>{l.t}</div>
          </div>
        ))}
      </div>

      <style>{`
        @keyframes legalIn {
          from { transform: translateY(110%); filter: blur(8px); opacity: 0; }
          to   { transform: translateY(0);    filter: blur(0);    opacity: 1; }
        }
      `}</style>
    </section>
  );
}

function LegalBody({ sections }) {
  const [active, setActive] = useStateLegal(sections[0]?.id);
  const refs = useRefLegal({});

  useEffectLegal(() => {
    const obs = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) setActive(e.target.id);
      });
    }, { rootMargin: '-40% 0px -55% 0px' });
    sections.forEach(s => {
      const el = document.getElementById(s.id);
      if (el) obs.observe(el);
    });
    return () => obs.disconnect();
  }, [sections]);

  return (
    <section style={{
      background: '#F4F2EC',
      padding: 'clamp(80px, 10vh, 140px) clamp(20px, 5vw, 80px) clamp(80px, 10vh, 120px)',
    }}>
      <div className="legal-grid" style={{
        display: 'grid',
        gridTemplateColumns: '220px minmax(0, 1fr)',
        gap: 'clamp(40px, 6vw, 100px)',
        maxWidth: 1320, margin: '0 auto',
      }}>
        {/* Sticky anchor nav */}
        <aside style={{
          position: 'sticky', top: 'clamp(120px, 14vh, 160px)',
          alignSelf: 'start',
          height: 'fit-content',
        }} className="legal-nav">
          <div style={{ fontSize: 10, letterSpacing: '0.22em', textTransform: 'uppercase', color: 'rgba(10,10,11,0.5)', marginBottom: 18 }}>Index</div>
          <ul style={{ listStyle: 'none', padding: 0, margin: 0, display: 'grid', gap: 10 }}>
            {sections.map((s, i) => (
              <li key={s.id}>
                <a href={`#${s.id}`} style={{
                  display: 'flex', gap: 12, alignItems: 'center',
                  fontSize: 13, color: active === s.id ? '#0A0A0B' : 'rgba(10,10,11,0.5)',
                  textDecoration: 'none',
                  fontWeight: active === s.id ? 500 : 400,
                  letterSpacing: '-0.005em',
                  transition: 'color 320ms ease',
                }}>
                  <span style={{
                    width: active === s.id ? 22 : 12, height: 1,
                    background: active === s.id ? 'var(--aspace-blue)' : 'rgba(10,10,11,0.2)',
                    transition: 'all 360ms cubic-bezier(0.22,1,0.36,1)',
                  }}/>
                  <span>{String(i+1).padStart(2,'0')}. {s.title}</span>
                </a>
              </li>
            ))}
          </ul>
        </aside>

        <div>
          {sections.map((s, i) => (
            <article key={s.id} id={s.id} style={{
              padding: 'clamp(40px, 5vw, 80px) 0',
              borderTop: i === 0 ? 'none' : '1px solid rgba(10,10,11,0.12)',
            }}>
              <FadeUpL>
                <div style={{ fontSize: 11, letterSpacing: '0.22em', textTransform: 'uppercase', color: 'rgba(10,10,11,0.5)', marginBottom: 18 }}>
                  Section {String(i+1).padStart(2,'0')}
                </div>
              </FadeUpL>
              <FadeUpL delay={120}>
                <h2 style={{
                  fontFamily: "'Inter Tight',sans-serif", fontWeight: 500,
                  fontSize: 'clamp(32px, 4vw, 56px)',
                  letterSpacing: '-0.025em',
                  lineHeight: 1.05,
                  margin: '0 0 clamp(28px, 3vw, 48px)',
                  maxWidth: 720,
                }}>{s.title}</h2>
              </FadeUpL>
              <FadeUpL delay={220}>
                <div style={{
                  fontFamily: "'Inter Tight',sans-serif",
                  fontSize: 'clamp(15px, 1.1vw, 17px)',
                  lineHeight: 1.7,
                  color: 'rgba(10,10,11,0.78)',
                  maxWidth: 620,
                  display: 'grid', gap: 'clamp(16px, 1.4vw, 22px)',
                }}>
                  {s.body.map((b, j) => {
                    if (b.kind === 'p')    return <p key={j} style={{ margin: 0 }}>{b.t}</p>;
                    if (b.kind === 'h')    return <div key={j} style={{ fontWeight: 500, color: '#0A0A0B', marginTop: 6 }}>{b.t}</div>;
                    if (b.kind === 'list') return (
                      <ul key={j} style={{ margin: 0, padding: 0, listStyle: 'none', display: 'grid', gap: 12 }}>
                        {b.items.map((it, k) => (
                          <li key={k} style={{ display: 'grid', gridTemplateColumns: '24px 1fr', gap: 12, alignItems: 'baseline' }}>
                            <span style={{ width: 14, height: 1, marginTop: 12, background: 'var(--aspace-blue)' }}/>
                            <span>{it}</span>
                          </li>
                        ))}
                      </ul>
                    );
                    return null;
                  })}
                </div>
              </FadeUpL>
            </article>
          ))}
        </div>
      </div>

      <style>{`
        @media (max-width: 900px) {
          .legal-grid { grid-template-columns: 1fr !important; }
          .legal-nav { position: static !important; margin-bottom: 24px; }
          .legal-nav ul { display: flex !important; flex-wrap: wrap; gap: 6px 14px !important; }
          .legal-nav ul a span:first-child { display: none; }
        }
      `}</style>
    </section>
  );
}

function LegalCloser() {
  return (
    <section style={{
      background: '#F4F2EC',
      padding: 'clamp(80px, 10vh, 140px) clamp(20px, 5vw, 80px) clamp(60px, 8vh, 100px)',
      borderTop: '1px solid rgba(10,10,11,0.12)',
    }}>
      <div style={{
        maxWidth: 1320, margin: '0 auto',
        display: 'grid', gridTemplateColumns: '1fr auto', gap: 24,
        alignItems: 'end', flexWrap: 'wrap',
      }} className="legal-closer">
        <div style={{
          fontFamily: "'Inter Tight',sans-serif", fontWeight: 500,
          fontSize: 'clamp(28px, 3.4vw, 48px)',
          letterSpacing: '-0.022em', lineHeight: 1.1,
          maxWidth: 720,
        }}>
          Questions about anything written here? Talk to a human.
        </div>
        <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
          <a href="#contact" data-open-contact className="legal-cta legal-dark">
            <span>Contact ASPACE</span>
            <span className="legal-arrow"><svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 12h14M13 6l6 6-6 6"/></svg></span>
          </a>
          <button onClick={() => window.openCookiePrefs && window.openCookiePrefs()} className="legal-cta legal-light">
            <span>Manage cookies</span>
          </button>
        </div>
      </div>
      <style>{`
        @media (max-width: 760px) {
          .legal-closer { grid-template-columns: 1fr !important; }
        }
        .legal-cta {
          all: unset; cursor: pointer;
          display: inline-flex; align-items: center; gap: 12px;
          padding: 12px 14px 12px 22px;
          border-radius: 999px;
          font-family: 'Inter Tight', sans-serif;
          font-weight: 500; font-size: 14px;
          transition: transform 320ms cubic-bezier(0.22,1.2,0.36,1), background 280ms ease, color 280ms ease;
          text-decoration: none;
        }
        .legal-cta:hover { transform: translateY(-1px); }
        .legal-dark { background: #0A0A0B; color: #fff; padding-right: 8px; }
        .legal-dark .legal-arrow {
          width: 30px; height: 30px; border-radius: 50%;
          background: var(--aspace-blue); color: #000;
          display: inline-flex; align-items: center; justify-content: center;
          transition: transform 320ms cubic-bezier(0.22,1.2,0.36,1);
        }
        .legal-dark:hover .legal-arrow { transform: rotate(-45deg); }
        .legal-light { background: transparent; color: #0A0A0B; padding: 12px 22px; border: 1px solid rgba(10,10,11,0.18); }
        .legal-light:hover { background: #0A0A0B; color: #fff; border-color: #0A0A0B; }
      `}</style>
    </section>
  );
}

function LegalFooter() {
  return (
    <footer style={{
      background: '#0A0A0B', color: '#fff',
      padding: 'clamp(60px, 8vh, 100px) clamp(20px, 5vw, 80px) clamp(36px, 5vh, 60px)',
    }}>
      <div style={{ maxWidth: 1320, margin: '0 auto' }}>
        <div style={{
          display: 'grid',
          gridTemplateColumns: 'repeat(auto-fit, minmax(180px, 1fr))',
          gap: 'clamp(40px, 5vw, 80px)',
          marginBottom: 'clamp(40px, 6vw, 80px)',
        }}>
          <div>
            <div style={{ fontFamily: "'Inter Tight',sans-serif", fontWeight: 600, fontSize: 26, letterSpacing: '-0.02em' }}>ASPACE</div>
            <div style={{ fontSize: 13, color: 'rgba(255,255,255,0.55)', marginTop: 12, lineHeight: 1.6 }}>
              Joop Geesinkweg 306<br/>1114 AB Amsterdam-Duivendrecht
            </div>
          </div>
          {[
            { h: 'Legal', items: [['Privacy','privacy.html'],['Terms','terms.html'],['Cookies','cookies.html']] },
            { h: 'Studio', items: [['About','ASPACE About.html'],['Studios','ASPACE Studios.html'],['Contact','#contact']] },
            { h: 'Live', items: [['Studio availability','ASPACE Studios.html'],['AI assistant','#ai'],['Newsletter','#contact']] },
          ].map((c, i) => (
            <div key={i}>
              <div style={{ fontSize: 11, letterSpacing: '0.18em', color: 'rgba(255,255,255,0.45)', textTransform: 'uppercase', marginBottom: 16 }}>{c.h}</div>
              <ul style={{ listStyle: 'none', padding: 0, margin: 0, display: 'grid', gap: 10 }}>
                {c.items.map(([t, h]) => {
                  const isContact = h === '#contact';
                  return (
                    <li key={t}>
                      <a href={h} {...(isContact ? { 'data-open-contact': true } : {})}
                        style={{ color: '#fff', textDecoration: 'none', fontSize: 14, opacity: 0.85 }}>{t}</a>
                    </li>
                  );
                })}
              </ul>
            </div>
          ))}
        </div>
        <div style={{
          display: 'flex', justifyContent: 'space-between', alignItems: 'center',
          paddingTop: 24, borderTop: '1px solid rgba(255,255,255,0.12)',
          fontSize: 11, letterSpacing: '0.14em', color: 'rgba(255,255,255,0.5)', textTransform: 'uppercase',
          flexWrap: 'wrap', gap: 16,
        }}>
          <span>© 2026 ASPACE Amsterdam</span>
          <span>Built with intent — Duivendrecht</span>
        </div>
      </div>
    </footer>
  );
}

window.LegalHero = LegalHero;
window.LegalBody = LegalBody;
window.LegalCloser = LegalCloser;
window.LegalFooter = LegalFooter;
