"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 iframe_widget_exports = {};
__export(iframe_widget_exports, {
  IframeWidget: () => IframeWidget
});
module.exports = __toCommonJS(iframe_widget_exports);
var import_react = __toESM(require("react"));
var import_theme_provider = require("./../ricos-styles/theme-provider.js");
var import_theme_utils = require("./../ricos-styles/themeStrategy/theme-utils.js");
var import_wix_instance_context = require("./../_shared/request-service-provider/wix-instance-context.js");
var import_wix_rich_content_common = require("./../wix-rich-content-common/index.js");
var import_iframe = require("./iframe.js");
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const buildWidgetParams = (colorPalette, fonts) => {
  const params = new URLSearchParams();
  params.set("injectClientSdk", "true");
  if (colorPalette?.length) {
    const [bg, , accent, accentFg, , fg] = colorPalette.map(
      (c) => (0, import_theme_utils.toRgbTuple)(c)?.replace(/,\s*/g, " ")
    );
    if (bg) {
      params.set("bg", bg);
      params.set("fg", fg ?? "");
      params.set("accent", accent ?? "");
      params.set("accentFg", accentFg ?? "");
    }
  }
  if (fonts?.heading) {
    params.set("fontHeading", fonts.heading);
  }
  if (fonts?.body) {
    params.set("fontBody", fonts.body);
  }
  const str = params.toString();
  return str ? `?${str}` : "";
};
const IframeWidget = ({
  widgetId,
  isViewer,
  onHeightChange,
  colorPalette
}) => {
  const theme = (0, import_theme_provider.useTheme)();
  const headingFont = theme.customStyles?.h1?.fontFamily?.split(",")[0].trim();
  const bodyFont = theme.customStyles?.p?.fontFamily?.split(",")[0].trim();
  const fonts = headingFont || bodyFont ? { heading: headingFont ?? "", body: bodyFont ?? "" } : void 0;
  const metaSiteId = (0, import_wix_instance_context.useWixInstanceService)()().metaSiteId;
  const [currId] = (0, import_react.useState)(() => (0, import_wix_rich_content_common.uuid)());
  const isLoadedRef = (0, import_react.useRef)(false);
  const handleIframeLoad = (0, import_react.useCallback)(() => {
    if (isLoadedRef.current) {
      return;
    }
    isLoadedRef.current = true;
    window.ricosHtmlPlugins ||= /* @__PURE__ */ new Set();
    window.ricosHtmlPlugins.add(currId);
  }, [currId]);
  (0, import_react.useEffect)(() => {
    const timeout = setTimeout(handleIframeLoad, 5e3);
    return () => clearTimeout(timeout);
  }, [handleIframeLoad]);
  (0, import_react.useEffect)(() => {
    if (!onHeightChange) {
      return;
    }
    const handleMessage = (event) => {
      const { type, widgetId: messageWidgetId, height } = event.data || {};
      if (type === "widgetViewer:heightResize" && messageWidgetId === widgetId) {
        onHeightChange(height);
      }
    };
    window.addEventListener("message", handleMessage);
    return () => window.removeEventListener("message", handleMessage);
  }, [widgetId, onHeightChange]);
  const isValidWidgetId = UUID_REGEX.test(widgetId);
  const isValidMetaSiteId = !!metaSiteId && UUID_REGEX.test(metaSiteId);
  if (!isValidWidgetId) {
    console.error("IframeWidget: invalid widgetId", widgetId);
  }
  if (metaSiteId && !isValidMetaSiteId) {
    console.error("IframeWidget: invalid metaSiteId", metaSiteId);
  }
  if (!isValidWidgetId || !isValidMetaSiteId) {
    return null;
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_iframe.Iframe,
    {
      onLoad: handleIframeLoad,
      src: `https://widgets.wixapps.net/${metaSiteId}/${widgetId}${buildWidgetParams(colorPalette, fonts)}`,
      isViewer,
      sandbox: "allow-popups allow-presentation allow-forms allow-same-origin allow-scripts"
    }
  );
};
//# sourceMappingURL=iframe-widget.js.map
