"use strict";
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);
var script_exports = {};
__export(script_exports, {
  APP_SCRIPT_ID_PREFIX: () => APP_SCRIPT_ID_PREFIX,
  addScriptToDOM: () => addScriptToDOM,
  removeDynamicImportedScripts: () => removeDynamicImportedScripts,
  removePrefetchLinks: () => removePrefetchLinks,
  removeScriptFromDOM: () => removeScriptFromDOM
});
module.exports = __toCommonJS(script_exports);
var import_url = require("../../../utils/url.js");
const APP_SCRIPT_ID_PREFIX = "emui-script-";
const HEAD_SCRIPTS = /(?:emuiDiagnostics|global|global-prod|emuiUserMonitoring)(?:..*)?.js/;
const isHeadScript = (scriptSrc) => HEAD_SCRIPTS.test(scriptSrc);
const isJSDOM = () => window.navigator.userAgent.includes("jsdom");
const addScriptToDOM = ({ name, hostUrl, documentEle }, fileName, index, isJsModule = false) => {
  if (!hostUrl)
    throw new Error("Unable to add scripts to DOM. hostUrl is required.");
  return new Promise((resolve, reject) => {
    const ele = documentEle.createElement("script");
    if (!ele) reject(new Error("Unable to insert Application scripts."));
    ele.id = `${APP_SCRIPT_ID_PREFIX}${name}-${index}`;
    const url = new URL(fileName, hostUrl);
    ele.src = (0, import_url.removeDoubleSlash)(url.href);
    ele.onload = resolve.bind(null, ele.id);
    ele.onerror = reject.bind(null, ele.id);
    if (isJSDOM() || !isJsModule) ele.async = false;
    else ele.type = "module";
    if (isHeadScript(ele.src)) documentEle.head.appendChild(ele);
    else documentEle.body.appendChild(ele);
  });
};
const removeScriptFromDOM = (elementId = "", documentEle = document) => new Promise((resolve) => {
  const ele = documentEle.getElementById(elementId);
  if (!ele) console.warn(new Error(`script with id ${elementId} not found`));
  ele.remove();
  resolve();
});
const removeDynamicImportedScripts = (hostUrl, documentEle) => {
  const hostPattern = new RegExp(hostUrl, "i");
  const scriptElements = documentEle.getElementsByTagName("script");
  for (let index = scriptElements.length - 1; index >= 0; index -= 1) {
    const scriptEle = scriptElements[index];
    const { src } = scriptEle;
    if (hostPattern.test(src)) scriptEle.remove();
  }
};
const removePrefetchLinks = (hostUrl, documentEle) => {
  const hostPattern = new RegExp(hostUrl, "i");
  const prefetchElements = documentEle.querySelectorAll('[rel="prefetch"]');
  for (let index = prefetchElements.length - 1; index >= 0; index -= 1) {
    const ele = prefetchElements[index];
    const { href } = ele;
    if (hostPattern.test(href)) ele.remove();
  }
};
