// Signal cover for product pages — the "Signal from noise" particle intro in
// a single-scene mode, drawn by site/3d/cover-3-signal.js (runtime:
// site/3d/cover-lab.js, styles: site/home-cover.css).
//   <SignalCover mode="chat"  … />  HyperAccounts: the AI-agent chat window
//   <SignalCover mode="racks" … />  Local AI: the data-centre row
//   <SignalCover mode="office" … /> About: the warehouse and office, van pulling up
// Render it first, then the page inside <div id="start" className="hf-page">:
// the page wipes up over the cover (see .hf-cover-track + .hf-page in
// home-cover.css). If WebGL, ES modules or the CDN are unavailable — or the
// scene hasn't started within 8 s — the cover removes itself and the page
// simply starts at the top as before.
function signalCoverSupports3D() {
  if (window.__hfCoverFallback) return false;
  try {
    if (!('noModule' in HTMLScriptElement.prototype)) return false;
    if (HTMLScriptElement.supports && !HTMLScriptElement.supports('importmap')) return false;
    const c = document.createElement('canvas');
    return !!(c.getContext('webgl2') || c.getContext('webgl'));
  } catch (e) { return false; }
}

// Intros play once per visit (browser tab session), per page: the first time a
// page is opened its intro shows; coming back to it later in the same session
// skips straight to the page. Refreshing any page starts a fresh visit (its intro
// plays, and every other page's intro will play again on its first view), and a
// link to a specific section (e.g. index.html#maturity) always goes straight there.
// The decision is made once per page load, so re-renders can't flip it.
function hfIntroShouldShow(key) {
  window.__hfIntro = window.__hfIntro || {};
  if (key in window.__hfIntro) return window.__hfIntro[key];
  let show = true;
  try {
    const nav = performance.getEntriesByType ? performance.getEntriesByType('navigation')[0] : null;
    const reload = nav ? nav.type === 'reload' : !!(performance.navigation && performance.navigation.type === 1);
    const deepLink = location.hash && location.hash !== '#' && location.hash !== '#start';
    // a refresh starts a fresh visit: every page's intro plays again on its next first view
    if (reload && !window.__hfIntroReset) { window.__hfIntroReset = true; Object.keys(sessionStorage).filter((x) => x.indexOf('hf-intro-seen:') === 0).forEach((x) => sessionStorage.removeItem(x)); }
    const k = 'hf-intro-seen:' + key, seen = sessionStorage.getItem(k) === '1';
    show = !deepLink && (reload || !seen);
    if (show) sessionStorage.setItem(k, '1');
  } catch (e) { show = true; }
  return (window.__hfIntro[key] = show);
}

function SignalCover({ mode, label, tag, introKey }) {
  const [ok, setOk] = React.useState(() => hfIntroShouldShow(introKey || mode) && signalCoverSupports3D());
  const stageRef = React.useRef(null);
  React.useEffect(() => {
    if (!ok) return;
    const fail = () => { window.__hfCoverFallback = true; setOk(false); };
    window.addEventListener('hf-cover-fallback', fail);
    const id = setTimeout(() => { if (stageRef.current && !stageRef.current.classList.contains('cl-ready')) fail(); }, 8000);
    return () => { window.removeEventListener('hf-cover-fallback', fail); clearTimeout(id); };
  }, [ok]);
  if (!ok) return null;
  return (
    <div className="hf-cover-track hf-cover-track--signal">
      <section ref={stageRef} className="cl-stage" data-signal={mode} aria-label={label}>
        <canvas aria-hidden="true"></canvas>
        <p className="cl-sr">{label}. {tag}</p>
        <p className="cl-tag" aria-hidden="true">{tag}</p>
        <p className="cl-hint" aria-hidden="true"></p>
        <a href="#start" className="cl-cue" aria-label={`Scroll to enter ${label}`}>
          Scroll
          <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
            <path d="M3 6l5 5 5-5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
        </a>
      </section>
    </div>
  );
}

Object.assign(window, { SignalCover, hfIntroShouldShow });
