"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var HeadSnippet_exports = {};
__export(HeadSnippet_exports, {
  BaseHeadSnippet: () => BaseHeadSnippet,
  cleanUpSnippet: () => cleanUpSnippet,
  getSnippetElementsFromDOM: () => getSnippetElementsFromDOM,
  renderSnippetAndExecuteScripts: () => renderSnippetAndExecuteScripts,
  snippetToElements: () => snippetToElements
});
module.exports = __toCommonJS(HeadSnippet_exports);
var import_react = require("react");
var import_html_react_parser = __toESM(require("html-react-parser"));
function BaseHeadSnippet({ snippet }) {
  (0, import_react.useEffect)(() => {
    const snippetElements = getSnippetElementsFromDOM(snippet);
    const isAlreadyInTheDOM = snippetElements.length > 0;
    if (isAlreadyInTheDOM)
      return;
    return renderSnippetAndExecuteScripts(snippet, window.document.head);
  }, [snippet]);
  return null;
}
const SNIPPET_ID_ATRIBUTE_NAME = "data-makeswift-snippet-id";
const VALID_HEAD_ELEMENT_TYPES = [
  "title",
  "base",
  "link",
  "style",
  "meta",
  "script",
  "noscript",
  "template"
];
function snippetToElements(snippet) {
  return import_react.Children.map((0, import_html_react_parser.default)(snippet.code), (element) => {
    if (typeof element === "string")
      return element;
    if (!VALID_HEAD_ELEMENT_TYPES.includes(element.type))
      return null;
    const key = element.key ? `${snippet.id}:${element.key}` : snippet.id;
    return (0, import_react.createElement)(element.type, {
      ...element.props,
      key,
      [SNIPPET_ID_ATRIBUTE_NAME]: snippet.id
    });
  });
}
function renderSnippetAndExecuteScripts(snippet, container) {
  const virtualContainer = container.ownerDocument.createElement(container.tagName);
  virtualContainer.innerHTML = snippet.code;
  const elements = Array.from(virtualContainer.querySelectorAll("*"));
  const scripts = elements.filter((el) => el instanceof HTMLScriptElement);
  scripts.forEach((script) => {
    const clone = script.ownerDocument.createElement(script.tagName);
    clone.textContent = script.textContent;
    Array.from(script.attributes).forEach(({ name, value }) => {
      clone.setAttribute(name, value);
    });
    clone.setAttribute(SNIPPET_ID_ATRIBUTE_NAME, snippet.id);
    script.parentNode?.replaceChild(clone, script);
  });
  const nodes = Array.from(virtualContainer.childNodes);
  container.append(...nodes);
  return () => {
    nodes.forEach((node) => {
      node.parentNode?.removeChild(node);
    });
    cleanUpSnippet(snippet);
  };
}
function cleanUpSnippet(snippet) {
  if (snippet.cleanup == null)
    return;
  try {
    const cleanUp = new Function(snippet.cleanup);
    cleanUp();
  } catch {
  }
}
function getSnippetElementsFromDOM(snippet) {
  return document.head.querySelectorAll(`[${SNIPPET_ID_ATRIBUTE_NAME}="${snippet.id}"]`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BaseHeadSnippet,
  cleanUpSnippet,
  getSnippetElementsFromDOM,
  renderSnippetAndExecuteScripts,
  snippetToElements
});
//# sourceMappingURL=HeadSnippet.js.map