/* Tiny DRY include loader: replaces <div data-include="partials/x.html"></div>
   with the partial's markup, then fires 'partials:loaded'. A <noscript>/static
   fallback inside each slot keeps the page usable if fetch fails (e.g. file://). */
(function () {
  async function run() {
    const slots = Array.from(document.querySelectorAll("[data-include]"));
    await Promise.all(slots.map(async (el) => {
      try {
        const res = await fetch(el.getAttribute("data-include"), { cache: "no-store" });
        if (!res.ok) throw new Error(res.status);
        const tmp = document.createElement("div");
        tmp.innerHTML = await res.text();
        el.replaceWith(...Array.from(tmp.childNodes));
      } catch (e) {
        /* leave whatever static fallback is inside the slot */
      }
    }));
    document.dispatchEvent(new CustomEvent("partials:loaded"));
  }
  if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", run);
  else run();
})();
