"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 BodySnippet_exports = {};
__export(BodySnippet_exports, {
  BodySnippet: () => BodySnippet
});
module.exports = __toCommonJS(BodySnippet_exports);
var import_react = require("react");
const SCRIPT_TAG = "script";
function BodySnippet({ code, cleanup }) {
  (0, import_react.useEffect)(() => {
    const container = document.createElement("div");
    container.innerHTML = code;
    const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT);
    const scripts = [];
    while (walker.nextNode()) {
      if (walker.currentNode instanceof HTMLScriptElement)
        scripts.push(walker.currentNode);
    }
    scripts.forEach((inlineScript) => {
      const executableScript = document.createElement(SCRIPT_TAG);
      executableScript.textContent = inlineScript.textContent;
      Array.from(inlineScript.attributes).forEach(({ name, value }) => {
        executableScript.setAttribute(name, value);
      });
      inlineScript.parentNode?.replaceChild(executableScript, inlineScript);
    });
    const nodes = Array.from(container.childNodes);
    document.body.append(...nodes);
    return () => {
      nodes.forEach((node) => {
        node.parentNode?.removeChild(node);
      });
      if (cleanup == null)
        return;
      try {
        const cleanUp = new Function(cleanup);
        cleanUp();
      } catch {
      }
    };
  }, [code, cleanup]);
  return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BodySnippet
});
//# sourceMappingURL=BodySnippet.js.map