import { initAkintu } from "./loader.js";

// Capture currentScript during module evaluation: after an async boundary the
// browser no longer guarantees that it points to this CDN installation tag.
const loaderScript = document.currentScript;

/** Starts the zero-configuration CDN integration from data-api-key. */
function start(): void {
  if (!(loaderScript instanceof HTMLScriptElement)) return;
  const apiKey = loaderScript.dataset.apiKey;
  if (apiKey === undefined || apiKey.trim().length === 0) return;
  void initAkintu({ apiKey });
}

// `defer` normally runs after parsing, but the readiness guard also supports
// async/manual injection without missing mount points declared later in HTML.
if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", start, { once: true });
} else {
  start();
}
