"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.loadScript = void 0;
const _excluded = ["async", "domain", "id", "inline", "src", "onLoad"];
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
const VALID_PROPS = ['crossOrigin', 'integrity'];
const loadScript = function () {
  let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    {
      async = true,
      domain = 'mirai',
      id,
      inline,
      src,
      onLoad = () => {}
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  return new Promise((resolve, reject) => {
    const scriptId = "".concat(domain, "-").concat(id);
    const exists = document.getElementById(scriptId);
    const cleanup = () => {
      const el = document.getElementById(scriptId);
      if (el) el.parentNode.removeChild(el);
    };
    if (!inline && !src || exists) return resolve(cleanup);
    const script = document.createElement('script');
    script.id = scriptId;
    script.async = async;
    Object.keys(others || {}).filter(key => VALID_PROPS.includes(key)).forEach(key => script[key] = others[key]);
    if (src) script.src = src;else if (inline) script.textContent = inline;
    script.onerror = error => reject(error);
    script.onload = () => {
      if (src && inline) {
        const inlineScript = document.createElement('script');
        inlineScript.textContent = inline;
        document.head.appendChild(inlineScript);
      }
      onLoad();
      // eslint-disable-next-line no-console
      if (src) console.log("\uD83D\uDCDC <script ".concat(scriptId, " ").concat(src ? "src=".concat(src, " ") : '', "/>"));
      resolve(cleanup);
    };
    document.head.appendChild(script);
  });
};
exports.loadScript = loadScript;
//# sourceMappingURL=loadScript.js.map