"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 ricos_editor_exports = {};
__export(ricos_editor_exports, {
  RicosEditor: () => RicosEditor
});
module.exports = __toCommonJS(ricos_editor_exports);
var import_lodash = __toESM(require("lodash"));
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../ricos-content/index.js");
var import_is_editor_plugin = require(".//utils/is-editor-plugin.js");
var import_next_schema = require("./../ricos-schema/next-schema/index.js");
var import_resolve_theme = require("./../ricos-styles/resolve-theme.js");
var import_use_bi_helpers = require("./../_shared/bi-strategy/use-bi-helpers.js");
var import_extract_plugins_config = require("./../_shared/extract-plugins-config.js");
var import_fetch_embed_data = require("./../_shared/fetch-embed-data.js");
var import_has_plugin = require("./../_shared/has-plugin.js");
var import_ricos_editor_api_context = require("./../_shared/ricos-editor-api-context/ricos-editor-api-context.js");
var import_url_resolvers = require("./../_shared/url-resolvers.js");
var import_use_iframe_sandbox_domain = require("./../_shared/use-iframe-sandbox-domain.js");
var import_wix_rich_content_common = require("./../wix-rich-content-common/index.js");
var import_dev_tools = require("./../wix-rich-content-common/lib/dev-tools.js");
var import_overrides = require("./../wix-rich-content-common/lib/overrides.js");
var import_editor_common = require("./../editor-common/index.js");
var import_load_css = require("./load-css.js");
var import_ricos_error_boundary = require("./tiptap/ricos-error-boundary.js");
const FullRicosEditorLazy = import_react.default.lazy(() => {
  (0, import_load_css.loadCSS)();
  return Promise.resolve().then(() => __toESM(require(
    /* webpackChunkName: "ricos_FullRicosEditor" */
    "./tiptap/full-ricos-editor.js"
  )));
});
const LocaleResourceProviderLazy = import_react.default.lazy(
  () => Promise.resolve().then(() => __toESM(require(
    /* webpackChunkName: "ricos_LocaleResourceProvider" */
    "./RicosContext/locale-resource-provider.js"
  )))
);
const renderMobileDevTools = () => {
  if (typeof window !== "undefined" && !window.__RICOS_DEVTOOLS_MOBILE_HOOK__) {
    window.__RICOS_DEVTOOLS_MOBILE_HOOK__ = true;
    const cdnTag = document.createElement("script");
    cdnTag.src = "//cdn.jsdelivr.net/npm/eruda";
    cdnTag.onload = () => {
      eruda.init();
    };
    window.document.head.appendChild(cdnTag);
  }
};
const RicosEditor = (0, import_react.forwardRef)(
  (props, ref) => {
    const {
      biSettings,
      instance,
      wixClient,
      iframeSandboxDomain: explicitDomain,
      msid,
      theme,
      environment: explicitEnvironment,
      ...restProps
    } = props;
    const {
      helpers: biHelpers,
      biLogger,
      biLoadPromise
    } = (0, import_use_bi_helpers.useBiHelpers)({ instance, biSettings, msid });
    (0, import_react.useEffect)(() => {
      biLoadPromise.then((callbacks) => {
        callbacks.onOpenEditorSuccess?.(
          import_next_schema.RICOS_VERSION,
          "INLINE",
          restProps.content?.metadata?.id
        );
      });
    }, [biLoadPromise]);
    const iframeSandboxDomain = (0, import_use_iframe_sandbox_domain.useIframeSandboxDomain)(explicitDomain, instance);
    const isBmPlatform = typeof window !== "undefined" && !!window?.ModuleRegistry;
    const environment = explicitEnvironment ?? (isBmPlatform ? "businessManager" : "liveSite");
    const resolvedTheme = (0, import_react.useMemo)(
      () => (0, import_resolve_theme.resolveTheme)({ theme, environment, isMobile: restProps.isMobile }),
      [theme, environment, restProps.isMobile]
    );
    const editorPlugins = props.plugins;
    const pluginTypes = (0, import_react.useMemo)(
      () => (editorPlugins || []).filter((opt) => !(0, import_is_editor_plugin.isCommonPluginOptions)(opt) && !("tiptapExtensions" in opt)).map((opt) => opt.type),
      [editorPlugins]
    );
    const overridePluginsMap = (0, import_overrides.useOverridePluginsMap)();
    const extractedPluginConfig = (0, import_react.useMemo)(
      () => (0, import_extract_plugins_config.extractPluginsConfig)(editorPlugins),
      [editorPlugins]
    );
    const linkPreviewConfig = (0, import_react.useMemo)(() => {
      if (instance && (0, import_has_plugin.hasPlugin)(import_ricos_content.LINK_PREVIEW_TYPE, editorPlugins)) {
        return { [import_ricos_content.LINK_PREVIEW_TYPE]: { fetchData: (0, import_fetch_embed_data.fetchEmbedData)(instance) } };
      }
      return {};
    }, [instance, editorPlugins]);
    const pluginConfig = (0, import_react.useMemo)(
      () => import_lodash.default.merge({}, extractedPluginConfig.config, linkPreviewConfig),
      [extractedPluginConfig, linkPreviewConfig]
    );
    const helpers = (0, import_react.useMemo)(() => ({ ...biHelpers, getImageUrl: import_url_resolvers.getImageUrl }), [biHelpers]);
    const setRicosEditorApi = (0, import_ricos_editor_api_context.useSetRicosEditorApi)();
    const [registeredApi, setRegisteredApi] = (0, import_react.useState)(null);
    const mergedRef = (0, import_react.useCallback)(
      (node) => {
        setRegisteredApi(node);
        if (typeof ref === "function") {
          ref(node);
        } else if (ref) {
          ref.current = node;
        }
      },
      [ref]
    );
    (0, import_react.useEffect)(() => {
      setRicosEditorApi(registeredApi);
      return () => setRicosEditorApi(null);
    }, [registeredApi, setRicosEditorApi]);
    if ((restProps.debugMode?.includes("mobile") || restProps.debugMode?.includes("all")) && restProps.isMobile) {
      renderMobileDevTools();
    }
    const loadingPlaceholder = restProps.loadingPlaceholder || /* @__PURE__ */ import_react.default.createElement("div", null);
    const Wrapper = (0, import_dev_tools.shouldShowDebuggingInfo)(restProps.debugMode?.includes("all")) ? import_dev_tools.DevContextProvider : import_react.default.Fragment;
    const editorProps = {
      ...restProps,
      pluginConfig,
      helpers,
      biLogger,
      wixClient,
      instance,
      plugins: restProps.plugins,
      iframeSandboxDomain,
      theme: resolvedTheme,
      environment
    };
    return (0, import_wix_rich_content_common.isSSR)() ? /* @__PURE__ */ import_react.default.createElement("div", null) : /* @__PURE__ */ import_react.default.createElement(Wrapper, null, /* @__PURE__ */ import_react.default.createElement(import_ricos_error_boundary.RicosErrorBoundary, { onError: restProps.onError }, /* @__PURE__ */ import_react.default.createElement(import_react.Suspense, { fallback: loadingPlaceholder }, /* @__PURE__ */ import_react.default.createElement(LocaleResourceProviderLazy, { fallback: loadingPlaceholder, locale: restProps.locale }, (locale) => (t) => import_overrides.hasWixInsiderOverride ? overridePluginsMap ? /* @__PURE__ */ import_react.default.createElement(
      import_overrides.OverrideFullRicosEditorLazy,
      {
        ...editorProps,
        pluginsMap: overridePluginsMap,
        locale,
        t,
        ref: mergedRef,
        platform: (0, import_editor_common.isiOS)() ? "macOs" : "windows"
      }
    ) : loadingPlaceholder : /* @__PURE__ */ import_react.default.createElement(
      FullRicosEditorLazy,
      {
        ...editorProps,
        pluginTypes,
        locale,
        t,
        ref: mergedRef,
        platform: (0, import_editor_common.isiOS)() ? "macOs" : "windows"
      }
    )))));
  }
);
RicosEditor.displayName = "RicosEditor";
//# sourceMappingURL=ricos-editor.js.map
