"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 full_ricos_editor_exports = {};
__export(full_ricos_editor_exports, {
  FullRicosEditor: () => FullRicosEditor,
  default: () => full_ricos_editor_default
});
module.exports = __toCommonJS(full_ricos_editor_exports);
var import_lodash = __toESM(require("lodash"));
var import_react = __toESM(require("react"));
var import_context = require("./../../pm-converters/context.js");
var import_ricos_content_query = require("./../../ricos-content-query/index.js");
var import_ricos_context = require("./../../ricos-context/index.js");
var import_orchestrator_context = require("./../../ricos-context/orchestrator-context.js");
var import_pm_content_context = require("./../../ricos-context/pm-content-context.js");
var import_selection_context = require("./../../ricos-context/selection-context.js");
var import_use_shortcuts = require("./../shortcuts/use-shortcuts.js");
var import_ricos_modals = require("./../../ricos-modals/index.js");
var import_modal_service_provider = require("./../../ricos-modals/modal-service-provider.js");
var import_ricos_orchestrator = require("./../../ricos-orchestrator/index.js");
var import_plugin_global_components = require("./../../ricos-plugins/plugin-global-components.js");
var import_plugin_providers = require("./../../ricos-plugins/plugin-providers.js");
var import_ricos_styles = require("./../../ricos-styles/index.js");
var import_theme_provider = require("./../../ricos-styles/theme-provider.js");
var import_bi_context_provider = require("./../../_shared/bi-context-provider/bi-context-provider.js");
var import_bi_helpers_context = require("./../../_shared/bi-helpers-context/bi-helpers-context.js");
var import_editor_base_url_context = require("./../../_shared/editor-base-url/editor-base-url-context.js");
var import_request_service_provider = require("./../../_shared/request-service-provider/request-service-provider.js");
var import_wix_instance_context = require("./../../_shared/request-service-provider/wix-instance-context.js");
var import_site_pages_fetch_context = require("./../../_shared/site-pages/site-pages-fetch-context.js");
var import_some_editor_props_context = require("./../../_shared/some-editor-props-context.js");
var import_use_user_interaction = require("./../../_shared/use-user-interaction.js");
var import_wix_rich_content_common = require("./../../wix-rich-content-common/index.js");
var import_editor_common = require("./../../editor-common/index.js");
var import_toolbars_v3 = require("./../../toolbars-v3/index.js");
var import_toolbar_boundary = require("./../../toolbars-v3/toolbar-boundary.js");
var import_dynamic_plugins_map = require("../../_shared/dynamic-plugins-map.js");
var import_normalize_content = require("../normalize-content.js");
var import_active_uploads = require("./active-uploads.js");
var import_error_notifier = require("./error-notifier.js");
var import_ricos_editor = require("./ricos-editor.js");
var import_ricos_error_boundary = require("./ricos-error-boundary.js");
var import_ricos_toolbars = require("./ricos-toolbars.js");
var import_styled_editor_container = require("./styled-editor-container.js");
const InjectedContentContext = import_react.default.createContext(void 0);
const PlaceholderContext = import_react.default.createContext(void 0);
const RicosEditorFallback = ({ Editor }) => /* @__PURE__ */ import_react.default.createElement(Editor, null);
const ShortcutsSetup = () => {
  (0, import_use_shortcuts.useShortcuts)();
  return null;
};
const InnerFullRicosEditor = import_react.default.forwardRef((props, ref) => {
  const modalService = (0, import_modal_service_provider.useModalService)();
  const theme = (0, import_theme_provider.useTheme)();
  const devService = (0, import_ricos_context.useDevService)();
  const wixInstance = (0, import_wix_instance_context.useWixInstanceService)();
  const makeRequestService = (0, import_request_service_provider.useRequestService)();
  const [orchestrator] = import_react.default.useState(
    () => new import_ricos_orchestrator.RicosOrchestrator({
      editorProps: props,
      t: props.t,
      platform: props.platform,
      modalService,
      devService,
      wixInstance,
      makeRequestService,
      pluginsMap: props.pluginsMap
    })
  );
  (0, import_modal_service_provider.useModalStateContext)({
    onSelectorContextValue: (selectorContextValue) => {
      return orchestrator.modalStateSelectorContextValue = selectorContextValue;
    }
  });
  const [portalRef, setPortalRef] = import_react.default.useState(null);
  const editorRef = import_react.default.useRef(null);
  const [errorNotifierRef, setErrorNotifierRef] = import_react.default.useState(null);
  const [inputRef, setInputRef] = import_react.default.useState(null);
  (0, import_ricos_context.useTrackRender)("FullRicosEditor");
  const { lastInteraction } = (0, import_use_user_interaction.useUserInteraction)();
  import_react.default.useEffect(() => {
    window.ricosEditor = orchestrator.getServices().editor;
    return () => {
      delete window.ricosEditor;
    };
  }, []);
  import_react.default.useEffect(() => {
    if (errorNotifierRef && inputRef) {
      orchestrator.setUpdateServiceDom(
        () => errorNotifierRef,
        () => inputRef
      );
    }
  }, [orchestrator, errorNotifierRef, inputRef]);
  const Editor = import_react.default.useMemo(
    () => import_react.default.memo(() => {
      const injectedContent = import_react.default.useContext(InjectedContentContext);
      const placeholder = import_react.default.useContext(PlaceholderContext);
      const [isBusy, setIsBusy] = import_react.default.useState(false);
      const onBusyChange = import_react.default.useCallback(
        (pmContent) => {
          const { onBusyChange: onBusyChange2 } = props;
          const isCurrentlyBusy = (0, import_active_uploads.hasActiveUploads)(pmContent);
          if (isBusy !== isCurrentlyBusy) {
            setIsBusy(isCurrentlyBusy);
            onBusyChange2?.(isCurrentlyBusy);
          }
        },
        [props.onBusyChange, isBusy]
      );
      const onChange = import_react.default.useCallback(
        (content2, meta) => {
          props.onChange?.(content2, meta);
        },
        [props.onChange]
      );
      return /* @__PURE__ */ import_react.default.createElement(
        import_ricos_editor.RicosEditorWithForwardRef,
        {
          ...props,
          injectedContent,
          placeholder,
          onChange,
          onInternalBusyChange: onBusyChange,
          ref
        }
      );
    }),
    []
  );
  const Children = props.children || RicosEditorFallback;
  const {
    plugins,
    shortcuts,
    content,
    uploadService,
    updateService,
    editor,
    styles,
    pluginsEvents,
    schema,
    zIndexService,
    getEditorQuery,
    context,
    converters
  } = orchestrator.getServices();
  const pluginExports = orchestrator.getPluginExports();
  const documentStyle = schema.getContent().documentStyle || {};
  const languageDir = (0, import_wix_rich_content_common.getLangDir)(context.locale);
  (0, import_react.useEffect)(() => {
    if (portalRef) {
      orchestrator.setPortalRef(portalRef);
    }
  }, [portalRef]);
  orchestrator.biHelpersRef.current = props.helpers || {};
  return /* @__PURE__ */ import_react.default.createElement(import_orchestrator_context.OrchestratorContext.Provider, { value: orchestrator }, /* @__PURE__ */ import_react.default.createElement(import_ricos_error_boundary.RicosErrorBoundary, { onError: props.onError, editor }, /* @__PURE__ */ import_react.default.createElement(import_bi_context_provider.BiContextProvider, { biLogger: props.biLogger }, /* @__PURE__ */ import_react.default.createElement(import_bi_helpers_context.BiHelpersProvider, { helpers: props.helpers || {} }, /* @__PURE__ */ import_react.default.createElement(import_editor_base_url_context.EditorBaseUrlContextProvider, { baseUrl: props.baseUrl }, /* @__PURE__ */ import_react.default.createElement(import_site_pages_fetch_context.SitePagesFetchContextProvider, null, /* @__PURE__ */ import_react.default.createElement(import_context.ConvertersContext.Provider, { value: converters }, /* @__PURE__ */ import_react.default.createElement(
    import_toolbar_boundary.ToolbarBoundaryProvider,
    {
      editorContainer: editorRef.current,
      externalContainer: props.container
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_ricos_styles.StylesContextProvider,
      {
        styles,
        documentStyle,
        theme,
        environment: props.environment ?? "liveSite",
        isMobile: context.isMobile
      },
      /* @__PURE__ */ import_react.default.createElement(import_styled_editor_container.StyledEditorContainer, { ref: editorRef }, /* @__PURE__ */ import_react.default.createElement(import_ricos_context.ZIndexContextProvider, { zIndexService }, /* @__PURE__ */ import_react.default.createElement(import_ricos_context.PluginsContextProvider, { plugins }, /* @__PURE__ */ import_react.default.createElement(import_plugin_providers.PluginProviders, { plugins }, /* @__PURE__ */ import_react.default.createElement(
        import_editor_common.RicosPortal,
        {
          languageDir,
          ref: setPortalRef,
          environment: props.environment
        }
      ), portalRef && /* @__PURE__ */ import_react.default.createElement(
        import_ricos_context.RicosContextProvider,
        {
          t: context.t,
          isMobile: context.isMobile,
          experiments: context.experiments,
          locale: context.locale,
          localeContent: context.localeContent,
          languageDir: context.languageDir,
          theme,
          portal: portalRef,
          debugMode: context.debugMode,
          jsdomMode: context.jsdomMode,
          platform: context.platform,
          content,
          environment: context.environment,
          tabIndex: context.tabIndex,
          lastInteraction,
          tabFocusBehavior: context.tabFocusBehavior,
          colorManager: props.colorManager
        },
        /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_ricos_context.EditorContextProvider, { editor }, /* @__PURE__ */ import_react.default.createElement(import_ricos_context.EditorQueryContextProvider, { editorQuery: getEditorQuery() }, /* @__PURE__ */ import_react.default.createElement(import_ricos_context.ShortcutsContextProvider, { shortcuts }, /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
          import_ricos_context.PluginsEventsContextProvider,
          {
            pluginsEvents
          },
          /* @__PURE__ */ import_react.default.createElement(import_ricos_context.PluginExportsContextProvider, { value: pluginExports }, /* @__PURE__ */ import_react.default.createElement(import_ricos_context.SchemaContextProvider, { schema }, /* @__PURE__ */ import_react.default.createElement(
            import_ricos_context.UploadContextProvider,
            {
              uploadService,
              updateService
            },
            /* @__PURE__ */ import_react.default.createElement(
              import_ricos_context.ToolbarContextProvider,
              {
                toolbarSettings: props.toolbarSettings,
                onSelectorContextValue: (selectorContextValue) => orchestrator.toolbarStateSelectorContextValue = selectorContextValue
              },
              /* @__PURE__ */ import_react.default.createElement(
                import_ricos_content_query.ContentQueryProvider,
                {
                  editor: editor.adapter.tiptapEditor
                },
                /* @__PURE__ */ import_react.default.createElement(
                  import_selection_context.SelectionContextProvider,
                  {
                    onSelectorContextValue: (selectorContextValue) => orchestrator.selectionSelectorContextValue = selectorContextValue
                  },
                  /* @__PURE__ */ import_react.default.createElement(
                    import_pm_content_context.PmContentContextProvider,
                    {
                      onSelectorContextValue: (selectorContextValue) => orchestrator.pmContentSelectorContextValue = selectorContextValue
                    },
                    /* @__PURE__ */ import_react.default.createElement(import_toolbars_v3.MobileToolbar, null),
                    /* @__PURE__ */ import_react.default.createElement(
                      InjectedContentContext.Provider,
                      {
                        value: props.injectedContent
                      },
                      /* @__PURE__ */ import_react.default.createElement(
                        PlaceholderContext.Provider,
                        {
                          value: props.placeholder
                        },
                        /* @__PURE__ */ import_react.default.createElement(Children, { Editor }),
                        /* @__PURE__ */ import_react.default.createElement(
                          import_plugin_global_components.PluginGlobalComponents,
                          {
                            plugins
                          }
                        )
                      )
                    ),
                    /* @__PURE__ */ import_react.default.createElement(import_ricos_toolbars.RicosToolbars, null)
                  )
                ),
                /* @__PURE__ */ import_react.default.createElement(import_ricos_modals.ModalRenderer, null)
              )
            )
          )))
        ), /* @__PURE__ */ import_react.default.createElement(ShortcutsSetup, null))))), /* @__PURE__ */ import_react.default.createElement(
          import_error_notifier.ErrorNotifier,
          {
            ref: setErrorNotifierRef,
            isMobile: props.isMobile,
            languageDir
          }
        ), /* @__PURE__ */ import_react.default.createElement(
          "input",
          {
            ref: setInputRef,
            id: "ricos-file-upload-input",
            key: "ricosUploadInput",
            type: "file",
            style: { display: "none" },
            tabIndex: -1,
            multiple: true
          }
        ))
      )))))
    )
  ))))))));
});
const FullRicosEditor = import_react.default.forwardRef(
  ({ content, injectedContent, pluginTypes, pluginsMap: pluginsMapProp, ...restProps }, ref) => {
    if (!(import_lodash.default.isPlainObject(content) || content === null || content === void 0)) {
      console.error("Invalid content prop provided to RicosEditor:", content);
      restProps.onError?.("Invalid content prop provided to RicosEditor");
      throw Error("Invalid content prop provided to RicosEdidtor");
    }
    if (!(import_lodash.default.isPlainObject(injectedContent) || injectedContent === null || injectedContent === void 0)) {
      console.error("Invalid injectedContent prop provided to RicosEditor:", injectedContent);
      restProps.onError?.("Invalid injectedContent prop provided to RicosEditor");
      throw Error("Invalid injectedContent prop provided to RicosEditor");
    }
    const normalizedContent = import_react.default.useMemo(
      () => content ? (0, import_normalize_content.normalizeContent)(content) : content,
      [content]
    );
    const normalizedInjectedContent = import_react.default.useMemo(
      () => injectedContent ? (0, import_normalize_content.normalizeContent)(injectedContent) : injectedContent,
      [injectedContent]
    );
    const [resolvedPluginsMap, setResolvedPluginsMap] = import_react.default.useState(pluginsMapProp ?? null);
    import_react.default.useEffect(() => {
      if (pluginsMapProp) {
        return;
      }
      let cancelled = false;
      (0, import_dynamic_plugins_map.resolvePlugins)(pluginTypes ?? []).then((map) => {
        if (!cancelled) {
          setResolvedPluginsMap(map);
        }
      });
      return () => {
        cancelled = true;
      };
    }, [pluginTypes, pluginsMapProp]);
    if (!resolvedPluginsMap) {
      return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, restProps.loadingPlaceholder || /* @__PURE__ */ import_react.default.createElement("div", null));
    }
    return /* @__PURE__ */ import_react.default.createElement(import_modal_service_provider.ModalProvider, { modalSettings: restProps.modalSettings }, /* @__PURE__ */ import_react.default.createElement(import_theme_provider.ThemeProvider, { theme: restProps.theme }, /* @__PURE__ */ import_react.default.createElement(
      import_some_editor_props_context.SomeEditorPropsContextProvider,
      {
        visitorUploads: restProps.visitorUploads,
        fileUploadSizeLimit: restProps.fileUploadSizeLimit,
        biSettings: restProps.biSettings,
        pickMedia: restProps.pickMedia,
        generalLinkSettings: restProps.generalLinkSettings,
        historySettings: restProps.historySettings,
        helpers: restProps.helpers,
        iframeSandboxDomain: restProps.iframeSandboxDomain,
        instance: restProps.instance
      },
      /* @__PURE__ */ import_react.default.createElement(import_request_service_provider.RequestServiceProvider, { wixClient: restProps.wixClient, instance: restProps.instance }, /* @__PURE__ */ import_react.default.createElement(import_wix_instance_context.WixInstanceContextProvider, { instance: restProps.instance }, /* @__PURE__ */ import_react.default.createElement(
        InnerFullRicosEditor,
        {
          ...restProps,
          pluginsMap: resolvedPluginsMap,
          content: normalizedContent,
          injectedContent: normalizedInjectedContent,
          ref
        }
      )))
    )));
  }
);
var full_ricos_editor_default = FullRicosEditor;
//# sourceMappingURL=full-ricos-editor.js.map
