"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 initialize_tiptap_adapter_exports = {};
__export(initialize_tiptap_adapter_exports, {
  initializeTiptapAdapter: () => initializeTiptapAdapter
});
module.exports = __toCommonJS(initialize_tiptap_adapter_exports);
var import_get_selected_nodes = require("./../_shared/get-selected-nodes.js");
var import_core = require("./../tiptap/core/index.js");
var import_invoke_atomic_block_focus = require("../_shared/invoke-atomic-block-focus.js");
var import_rich_content_adapter = require("./components/rich-content-adapter.js");
var import_apply_dev_tools = require("./components/RicosTiptapEditor/apply-dev-tools.js");
var import_core_configs = require("./components/RicosTiptapEditor/core-configs.js");
var import_create_tiptap_extensions = require("./models/create-tiptap-extensions.js");
var import_transform_pasted_html = require("./transform-pasted-html.js");
const extractExtensionProps = (props) => {
  const {
    placeholder,
    textAlignment,
    iframeSandboxDomain,
    textWrap,
    maxTextLength,
    linkSettings,
    generalLinkSettings,
    theme
  } = props;
  let { anchorTarget, relValue, rel } = linkSettings || {};
  if (generalLinkSettings?.initialLinkOptions) {
    const { target, ...rest } = generalLinkSettings.initialLinkOptions;
    anchorTarget = target;
    rel = rest;
  }
  return {
    placeholder,
    textAlignment,
    iframeSandboxDomain,
    isTextWrap: textWrap,
    maxTextLength,
    anchorTarget,
    rel,
    relValue,
    breakoutRule: theme?.breakout?.rule
  };
};
const initializeTiptapAdapter = (ricosEditorProps, services, isDebugMode = false, getToolbarStateSelectorContextValue, getSelectionSelectorContextValue, getModalStateSelectorContextValue, pmContentSelectorContextValue, getInlineAiStateSelectorContextValue) => {
  const hasCollabPlugin = services.plugins.asArray().some((plugin) => plugin.plugin.name === "collab");
  const tiptapContent = hasCollabPlugin ? null : services.schema.getTiptapContent();
  const extensionProps = extractExtensionProps(ricosEditorProps);
  const extensions = [...services.plugins.getTiptapExtensions(), ...import_core_configs.coreConfigs];
  const tiptapExtensions = (0, import_create_tiptap_extensions.createTiptapExtensions)(extensions, extensionProps, services);
  const { autofocus, onLoad, onAtomicBlockFocus } = ricosEditorProps;
  const editor = new import_core.Editor({
    extensions: tiptapExtensions,
    content: tiptapContent,
    injectCSS: true,
    editorProps: {
      transformPastedHTML: import_transform_pasted_html.transformPastedHTML
    },
    onCreate({ editor: editor2 }) {
      if (onLoad) {
        onLoad({ editor: editor2 });
        if (autofocus) {
          (0, import_invoke_atomic_block_focus.invokeAtomicBlockFocus)({
            selectedNodes: (0, import_get_selected_nodes.getSelectedNodes)(editor2),
            onAtomicBlockFocus
          });
        }
      }
    },
    ...autofocus && { autofocus }
  });
  (0, import_apply_dev_tools.applyDevTools)(editor, isDebugMode);
  return new import_rich_content_adapter.RichContentAdapter(
    editor,
    services,
    ricosEditorProps.toolbarSettings,
    getToolbarStateSelectorContextValue,
    getSelectionSelectorContextValue,
    getModalStateSelectorContextValue,
    pmContentSelectorContextValue,
    getInlineAiStateSelectorContextValue
  );
};
//# sourceMappingURL=initialize-tiptap-adapter.js.map
