/* eslint-disable */
// Boot sequence — types lines in, then dismisses.

const __BOOT_SCRIPT = [
  { t: "[ WTI-CON v2.6.1  ·  warm boot ]", c: "" , delay: 280 },
  { t: "Establishing secure link\u2026", c: "", delay: 360 },
  { t: "  -> handshake     ", c: "", delay: 200, tail: { t: "[ OK ]", c: "ok" } },
  { t: "  -> key exchange  ", c: "", delay: 220, tail: { t: "[ OK ]", c: "ok" } },
  { t: "  -> AES-256-GCM   ", c: "", delay: 240, tail: { t: "[ OK ]", c: "ok" } },
  { t: "Authenticating channel WT/154.25", c: "", delay: 280 },
  { t: "  -> identity     ", c: "", delay: 200, tail: { t: "[ WTIC2 ]", c: "warn" } },
  { t: "Mounting operations index\u2026", c: "", delay: 260 },
  { t: "  -> OPS-001 Angry Yeti Coffee Co.      ", c: "", delay: 90, tail: { t: "[ ACTIVE ]", c: "ok" } },
  { t: "  -> OPS-002 Throttle & Grease          ", c: "", delay: 90, tail: { t: "[ ACTIVE ]", c: "ok" } },
  { t: "  -> OPS-003 SmallBizSheetzCo           ", c: "", delay: 90, tail: { t: "[ DECOM ]", c: "warn" } },
  { t: "  -> OPS-004 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588             ", c: "", delay: 110, tail: { t: "[ REDACTED ]", c: "red" } },
  { t: "Console ready.", c: "ok", delay: 320 },
];

function BootSequence({ enabled, onDone }) {
  const [lines, setLines] = React.useState([]);
  const [done, setDone] = React.useState(!enabled);
  const onDoneRef = React.useRef(onDone);
  onDoneRef.current = onDone;

  React.useEffect(() => {
    if (!enabled) { onDoneRef.current && onDoneRef.current(); return; }

    let cancelled = false;
    let timeoutId = null;
    const acc = [];

    function finish() {
      if (cancelled) return;
      cancelled = true;
      setLines(__BOOT_SCRIPT.slice());
      setDone(true);
      onDoneRef.current && onDoneRef.current();
    }

    function step(i) {
      if (cancelled) return;
      if (i >= __BOOT_SCRIPT.length) {
        timeoutId = setTimeout(finish, 480);
        return;
      }
      const s = __BOOT_SCRIPT[i];
      acc.push(s);
      setLines(acc.slice());
      timeoutId = setTimeout(() => step(i + 1), s.delay);
    }
    step(0);

    // Safety net: hard cap so background-tab throttling can't strand us.
    const safetyId = setTimeout(finish, 6000);

    // Skip to end if the tab becomes hidden mid-boot.
    const onVis = () => { if (document.hidden) finish(); };
    document.addEventListener("visibilitychange", onVis);

    return () => {
      cancelled = true;
      if (timeoutId) clearTimeout(timeoutId);
      clearTimeout(safetyId);
      document.removeEventListener("visibilitychange", onVis);
    };
  }, [enabled]);

  if (!enabled || done) {
    return <div className="boot boot--done" aria-hidden="true"></div>;
  }
  return (
    <div className="boot" role="status" aria-live="polite">
      <div className="boot__inner">
        {lines.map((l, i) => {
          if (!l) return null;
          return (
            <span key={i} className="boot__line">
              <span className={l.c || ""}>{l.t}</span>
              {l.tail ? <span className={l.tail.c || ""}>{l.tail.t}</span> : null}
            </span>
          );
        })}
        <span className="boot__caret"></span>
      </div>
    </div>
  );
}

window.BootSequence = BootSequence;
