var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
(!("UNL" in window) || typeof window.UNL != "object" || !("autoLoader" in window.UNL) || typeof window.UNL.autoLoader != "object" || !("config" in window.UNL.autoLoader) || typeof window.UNL.autoLoader.config != "object") && await import("./head-2.js?v=6.1.1");
document.dispatchEvent(new Event("autoLoaderPreLoad"));
const globalOptInSelector = window.UNL.autoLoader.config.globalOptInSelector ?? null, globalOptOutSelector = window.UNL.autoLoader.config.globalOptOutSelector ?? null, configPluginList = window.UNL.autoLoader.config.plugins ?? {}, enabled = window.UNL.autoLoader.config.enabled ?? !0, watch = window.UNL.autoLoader.config.watch ?? !0;
let watchList = [];
if (enabled) {
  window.UNL.autoLoader.plugins = {};
  const pluginLoadPromises = Object.entries(configPluginList).map(async ([pluginName, pluginConfig]) => {
    if (!("url" in pluginConfig)) {
      console.error(`Missing URL in autoloader plugin config: ${pluginName}`);
      return;
    }
    let pluginModule = null;
    try {
      pluginModule = await import(pluginConfig.url);
    } catch (err) {
      console.error(`Error loading plugin: ${pluginName}`), console.error(err);
      return;
    }
    if (typeof pluginModule.getPluginType != "function") {
      console.error(`Plugin missing export: getPluginType (function): ${pluginName}`);
      return;
    }
    if (typeof pluginModule.initialize != "function") {
      console.error(`Plugin missing export: initialize (function): ${pluginName}`);
      return;
    }
    if (typeof pluginModule.getQuerySelector != "function") {
      console.error(`Plugin missing export: getQuerySelector (function): ${pluginName}`);
      return;
    }
    if (pluginModule.getPluginType() === "single") {
      if (typeof pluginModule.isOnPage != "function") {
        console.error(`Plugin missing export: isOnPage (function): ${pluginName}`);
        return;
      }
    } else if (pluginModule.getPluginType() === "multi") {
      if (typeof pluginModule.loadElement != "function") {
        console.error(`Plugin missing export: loadElement (function): ${pluginName}`);
        return;
      }
      if (typeof pluginModule.loadElements != "function") {
        console.error(`Plugin missing export: loadElements (function): ${pluginName}`);
        return;
      }
    }
    window.UNL.autoLoader.plugins[pluginName] = {
      loaded: !1,
      optInSelector: pluginConfig?.optInSelector ?? null,
      optOutSelector: pluginConfig?.optOutSelector ?? null,
      customConfig: pluginConfig?.customConfig ?? {},
      onPluginInitialized: pluginConfig?.onPluginInitialized ?? null,
      onPluginLoadedElement: pluginConfig?.onPluginLoadedElement ?? null,
      module: pluginModule,
      elements: []
    }, typeof pluginModule.getPluginLoadAfterWatch == "function" && pluginModule.getPluginLoadAfterWatch() === !1 && (await loadPlugin(pluginModule, window.UNL.autoLoader.plugins[pluginName], pluginName), window.UNL.autoLoader.plugins[pluginName].loaded = !0), watchList.push(pluginName);
  });
  await Promise.all(pluginLoadPromises);
}
if (enabled && watch) {
  const mutationCallback = /* @__PURE__ */ __name(async (mutationList) => {
    for (const mutationRecord of mutationList)
      for (const nodeAdded of mutationRecord.addedNodes)
        if (nodeAdded instanceof Element)
          for (const singlePluginName of watchList) {
            const pluginData = window.UNL.autoLoader.plugins[singlePluginName], pluginModule = pluginData.module;
            await checkPluginMutation(nodeAdded, pluginModule, pluginData, singlePluginName), pluginData.loaded = !0;
          }
  }, "mutationCallback"), observer = new MutationObserver(mutationCallback), observerConfig = {
    subtree: !0,
    childList: !0
  };
  observer.observe(document.body, observerConfig);
}
if (enabled) {
  const initPromises = Object.entries(window.UNL.autoLoader.plugins).map(async ([singlePluginName, pluginData]) => {
    const pluginModule = pluginData.module;
    pluginData.loaded === !1 && await loadPlugin(pluginModule, pluginData, singlePluginName);
  });
  await Promise.all(initPromises);
}
window.UNL.autoLoader.loaded = !0;
"loadCallbackQueue" in window.UNL.autoLoader && Array.isArray(window.UNL.autoLoader.loadCallbackQueue) && (window.UNL.autoLoader.loadCallbackQueue.forEach((singleCallback) => {
  singleCallback();
}), delete window.UNL.autoLoader.loadCallbackQueue);
window.UNL.autoLoader.onLoad = (callbackFunc) => {
  callbackFunc();
};
document.dispatchEvent(new Event("autoLoaderPostLoad"));
async function loadPlugin(pluginModule, pluginData, singlePluginName) {
  if (pluginModule.getPluginType() === "single") {
    if (pluginModule.isOnPage()) {
      try {
        const element = await pluginModule.initialize(pluginData.customConfig);
        element !== null && (pluginData.elements.push(element), typeof pluginData.onPluginLoadedElement == "function" && pluginData.onPluginLoadedElement({
          loadedElement: element
        }));
      } catch (err) {
        console.error(`Error initializing plugin ${singlePluginName}:`, err);
      }
      watchList = watchList.filter((singlePluginToFilter) => singlePluginToFilter !== singlePluginName);
    }
  } else if (pluginModule.getPluginType() === "multi") {
    let matchingElements = Array.from(document.querySelectorAll(pluginModule.getQuerySelector()));
    if (matchingElements.length === 0)
      return;
    globalOptOutSelector !== null && (matchingElements = matchingElements.filter((matchingElement) => !matchingElement.matches(globalOptOutSelector))), pluginData.optOutSelector !== null && (matchingElements = matchingElements.filter((matchingElement) => !matchingElement.matches(pluginData.optOutSelector))), globalOptInSelector !== null && (matchingElements = matchingElements.filter((matchingElement) => matchingElement.matches(globalOptInSelector))), pluginData.optInSelector !== null && (matchingElements = matchingElements.filter((matchingElement) => matchingElement.matches(pluginData.optInSelector)));
    try {
      const elements = await pluginModule.loadElements(matchingElements, pluginData.customConfig);
      pluginData.elements = pluginData.elements.concat(elements), typeof pluginData.onPluginLoadedElement == "function" && elements.forEach((singleElement) => {
        pluginData.onPluginLoadedElement({
          loadedElement: singleElement
        });
      });
    } catch (err) {
      console.error(`Error loading plugin element for ${singlePluginName}:`, err);
    }
  }
}
__name(loadPlugin, "loadPlugin");
async function checkPluginMutation(nodeAdded, pluginModule, pluginData, singlePluginName) {
  let foundElements = [];
  nodeAdded.matches(pluginModule.getQuerySelector()) && foundElements.push(nodeAdded), foundElements = foundElements.concat(Array.from(nodeAdded.querySelectorAll(pluginModule.getQuerySelector())));
  for (const singleFoundElement of foundElements) {
    if (globalOptOutSelector !== null && singleFoundElement.matches(globalOptOutSelector) || globalOptInSelector !== null && !singleFoundElement.matches(globalOptInSelector) || pluginData.optInSelector !== null && !singleFoundElement.matches(pluginData.optInSelector) || pluginData.optOutSelector !== null && singleFoundElement.matches(pluginData.optOutSelector))
      return;
    if (pluginModule.getPluginType() === "single")
      try {
        const element = await pluginModule.initialize(pluginData.customConfig);
        element !== null && (pluginData.elements.push(element), typeof pluginData.onPluginLoadedElement == "function" && pluginData.onPluginLoadedElement({
          loadedElement: element
        })), watchList = watchList.filter((singlePluginToFilter) => singlePluginToFilter !== singlePluginName);
      } catch (err) {
        console.error(`Error initializing plugin ${singlePluginName}:`, err);
      }
    else if (pluginModule.getPluginType() === "multi")
      try {
        const element = await pluginModule.loadElement(singleFoundElement, pluginData.customConfig);
        pluginData.elements = pluginData.elements.concat(element), typeof pluginData.onPluginLoadedElement == "function" && pluginData.onPluginLoadedElement({
          loadedElement: element
        });
      } catch (err) {
        console.error(`Error loading plugin element for ${singlePluginName}:`, err);
      }
  }
}
__name(checkPluginMutation, "checkPluginMutation");
//# sourceMappingURL=auto-loader.js.map
