var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/utils/load.ts
var load_exports = {};
__export(load_exports, {
  default: () => exec
});
module.exports = __toCommonJS(load_exports);
var cache = {};
function createScript(options) {
  const script = document.createElement("script");
  script.type = options.type || "text/javascript";
  script.async = !!options.async;
  script.id = options.id || options.url;
  if (options.url) {
    script.src = options.url;
  }
  if (options.text) {
    script.text = options.text;
  }
  return script;
}
function loadScript(head, script) {
  return new Promise(function(resolve, reject) {
    let done = false;
    script.onload = script.onreadystatechange = function() {
      if (!done && (!script.readyState || script.readyState === "loaded" || script.readyState === "complete")) {
        done = true;
        script.onload = null;
        resolve(script);
      }
    };
    script.onerror = reject;
    head.appendChild(script);
  });
}
function getScriptById(id) {
  const script = id && document.getElementById(id);
  if (script) {
    return script;
  }
}
function getScriptByUrl(url) {
  const script = url && document.querySelector("script[src='" + url + "']");
  if (script) {
    return script;
  }
}
function exec(options, parent) {
  if (typeof options === "string") {
    options = {
      url: options,
      debug: false
    };
  }
  const cacheId = options.id || options.url;
  const cacheEntry = cache[cacheId];
  if (cacheEntry) {
    if (!!options.debug) {
      console.log("load-js: cache hit", cacheId);
    }
    return cacheEntry;
  }
  if (options.allowExternal !== false) {
    let el;
    if (options.id) {
      el = getScriptById(options.id);
    }
    if (!el) {
      el = getScriptByUrl(options.url);
    }
    if (el) {
      const promise = Promise.resolve(el);
      if (cacheId) {
        cache[cacheId] = promise;
      }
      return promise;
    }
  }
  if (!options.url && !options.text) {
    throw new Error("load-js: must provide a url or text to load");
  }
  return loadScript(parent, createScript(options));
}
