"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 preview_switcher_exports = {};
__export(preview_switcher_exports, {
  PreviewSwitcher: () => PreviewSwitcher
});
module.exports = __toCommonJS(preview_switcher_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_react_dom = require("react-dom");
var import_head_tags = require("../page/head-tags");
var import_use_is_in_builder = require("../../hooks/use-is-in-builder");
var import_preview_toolbar = require("./preview-toolbar");
function PreviewSwitcher({ isPreview }) {
  const shadowContainerRef = (0, import_react.useRef)(null);
  const [shadowRoot, setShadowRoot] = (0, import_react.useState)(null);
  const isInBuilder = (0, import_use_is_in_builder.useIsInBuilder)();
  const showToolbar = !isInBuilder && isPreview;
  (0, import_react.useEffect)(() => {
    if (!showToolbar)
      return;
    if (shadowContainerRef.current && shadowContainerRef.current.shadowRoot == null && shadowRoot == null) {
      const root = shadowContainerRef.current.attachShadow({ mode: "open" });
      setShadowRoot(root);
    }
  }, [showToolbar, shadowRoot, setShadowRoot]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    showToolbar && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: "makeswift-preview-switcher", ref: shadowContainerRef, children: shadowRoot ? (0, import_react_dom.createPortal)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_preview_toolbar.PreviewToolbar, {}), shadowRoot) : null }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_head_tags.PageMeta,
      {
        name: "makeswift-preview-info",
        content: JSON.stringify({ isPreview, inBuilder: isInBuilder })
      }
    )
  ] });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PreviewSwitcher
});
//# sourceMappingURL=preview-switcher.js.map