/* The explainer film on the landing page. The film itself is a small three.js diorama that lives in /explainer3d/
   (its own page, so three.js loads only there); this section frames it and tells it when it is on screen.
   Exports: ExplainerSection */

function ExplainerSection() {
  const frame = useRef(null);
  const [full, setFull] = useState(false);   // the fallback for browsers without real fullscreen (iPhone): the frame covers the page
  useEffect(() => {
    const el = frame.current;
    if (!el) return;
    const tell = visible => { try { el.contentWindow.postMessage({ type: 'xp-visible', visible }, window.location.origin); } catch (e) {} };
    const io = new IntersectionObserver(es => tell(es[0].isIntersecting), { threshold: 0.5 });
    io.observe(el);
    const onLoad = () => { const r = el.getBoundingClientRect(); tell(r.top < window.innerHeight * 0.6 && r.bottom > window.innerHeight * 0.4); };
    el.addEventListener('load', onLoad);
    const onMsg = e => { if (e.origin === window.location.origin && e.data && e.data.type === 'xp-full') { setFull(!!e.data.on); document.body.style.overflow = e.data.on ? 'hidden' : ''; } };
    window.addEventListener('message', onMsg);
    return () => { io.disconnect(); el.removeEventListener('load', onLoad); window.removeEventListener('message', onMsg); };
  }, []);
  return (
    <section id="video" className="ll-section ll-section-warm">
      <div className="ll-container">
        <div className="ll-head">
          <span className="ll-kicker">50 שניות</span>
          <h2 className="ll-h2">למה Reco, ואיך זה עובד.</h2>
        </div>
        <div style={{ maxWidth: 960, margin: '0 auto' }}>
          <div style={full ? { position: 'fixed', inset: 0, zIndex: 1000, background: '#b4d2de' } : { position: 'relative', width: '100%', aspectRatio: '16 / 9', borderRadius: 28, overflow: 'hidden', background: '#b4d2de', boxShadow: '0 40px 90px rgba(16,36,63,0.16), 0 0 0 1px rgba(16,36,63,0.06)' }}>
            <iframe ref={frame} src="/explainer3d/index.html" title="Reco – איך זה עובד" loading="lazy" allow="autoplay; fullscreen" allowFullScreen
              style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', border: 0, display: 'block' }} />
          </div>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { ExplainerSection });
