'use strict';
/* ──────────────────────────────────────────────────────────────────
   KaTeX bootstrap for the exam-prep module.

   Page must include the KaTeX CSS + JS via CDN tags BEFORE this script.
   The `onKatexLoad` global is wired up via the KaTeX auto-render onload.
   ────────────────────────────────────────────────────────────────── */

(function () {
  let loaded = false;
  const pending = [];

  // Global called from auto-render.min.js onload handler in the HTML.
  window.onKatexLoad = function () {
    loaded = true;
    while (pending.length) {
      try { renderMathInElement(pending.shift(), KATEX_OPTS); } catch {}
    }
  };

  const KATEX_OPTS = {
    delimiters: [
      { left: '$$', right: '$$', display: true  },
      { left: '$',  right: '$',  display: false },
    ],
    throwOnError: false,
  };

  function run(el) {
    if (!el) return;
    if (!loaded) { pending.push(el); return; }
    try { renderMathInElement(el, KATEX_OPTS); } catch {}
  }

  window.EP = window.EP || {};
  window.EP.katex = { run, isLoaded: () => loaded };
})();
