import React, { useContext, useMemo } from "react";
import { RicosContext } from "./../../ricos-context/index.js";
import { containsUrl } from "./../../_shared/link/contains-url.js";
import { normalizeUrl } from "./../../_shared/link/normalize-url.js";
import { HTML_VIEWER_ROOT } from "../data-hooks.js";
import {
  INIT_HEIGHT,
  INIT_WIDTH,
  SRC_TYPE_HTML,
  SRC_TYPE_URL,
  SRC_TYPE_WIDGET
} from "../defaults.js";
import { htmlFixer } from "../html-fixer.js";
import { IframeHtml } from "../iframe-html.js";
import { IframeUrl } from "../iframe-url.js";
import { IframeWidget } from "../iframe-widget.js";
import htmlStyles from "../statics/styles/HtmlComponent-scss.js";
const getPageURL = (siteDomain) => {
  if (!siteDomain) {
    return;
  }
  const regex = /http.+com/gm;
  const res = regex.exec(siteDomain);
  if (res) {
    return res[0];
  }
};
const HtmlViewer = React.forwardRef(
  ({ settings, htmlData, iframeSandboxDomain, onIframeHeightChange, isViewer = false }, ref) => {
    const { isMobile } = useContext(RicosContext);
    const [iframeHeight, setIframeHeight] = React.useState();
    const setHeight = (height) => {
      if (height > 0 && height !== iframeHeight) {
        setIframeHeight(height);
      }
    };
    const getIframeHeight = useMemo(() => {
      const { containerData, autoHeight } = htmlData;
      if (autoHeight !== false && iframeHeight) {
        onIframeHeightChange?.(iframeHeight);
        return iframeHeight;
      }
      const customHeight = containerData?.height?.custom;
      const parsedCustomHeight = parseInt(customHeight || "");
      if (!customHeight || typeof customHeight === "string" && customHeight.endsWith("%") || isNaN(parsedCustomHeight)) {
        return iframeHeight || settings.height || INIT_HEIGHT;
      }
      return parsedCustomHeight;
    }, [htmlData, settings.height, iframeHeight]);
    const style = {
      width: isMobile ? "auto" : htmlData.containerData?.width?.custom || settings.width || INIT_WIDTH + "px",
      height: getIframeHeight + "px",
      maxHeight: getIframeHeight + "px"
    };
    const html = useMemo(() => {
      const pageURL = getPageURL(settings.siteDomain);
      return htmlFixer(htmlData.html, pageURL);
    }, [htmlData.html]);
    return /* @__PURE__ */ React.createElement(
      "div",
      {
        className: htmlStyles.htmlComponent,
        style,
        "data-hook": HTML_VIEWER_ROOT,
        ref
      },
      htmlData.widgetId ? /* @__PURE__ */ React.createElement(
        IframeWidget,
        {
          key: SRC_TYPE_WIDGET,
          widgetId: htmlData.widgetId,
          isViewer,
          onHeightChange: setHeight,
          colorPalette: settings.getColorPalette?.()
        }
      ) : html ? /* @__PURE__ */ React.createElement(
        IframeHtml,
        {
          iframeSandboxDomain: iframeSandboxDomain || "https://www.filesusr.com",
          key: SRC_TYPE_HTML,
          html,
          onHeightChange: setHeight,
          isViewer
        }
      ) : htmlData.url && containsUrl(htmlData.url) ? /* @__PURE__ */ React.createElement(
        IframeUrl,
        {
          key: SRC_TYPE_URL,
          tabIndex: 0,
          src: normalizeUrl(htmlData.url),
          isViewer
        }
      ) : /* @__PURE__ */ React.createElement("div", { className: htmlStyles.htmlComponent_placeholder })
    );
  }
);
export {
  HtmlViewer
};
//# sourceMappingURL=html-viewer.js.map
