"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, {
  RicosEditorWithForwardRef: () => RicosEditorWithForwardRef
});
module.exports = __toCommonJS(ricos_editor_exports);
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../../ricos-content/index.js");
var import_is_rich_content_equal = require("./../../ricos-content/lib/is-rich-content-equal.js");
var import_ricos_context = require("./../../ricos-context/index.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_editor_events_context = require("./../../editor-common/editor-events-context.js");
var import_ricos_tiptap_editor = require("./../../tiptap-editor/components/RicosTiptapEditor/ricos-tiptap-editor.js");
var import_tiptap_editor = require("./../../tiptap-editor/index.js");
var import_invoke_atomic_block_focus = require("../../_shared/invoke-atomic-block-focus.js");
var import_placeholder = require("../../tiptap-extensions/placeholder.js");
var import_editor_styles_scss = __toESM(require("../statics/styles/editor-styles-scss.js"));
var import_publish = require("../utils/bi/publish.js");
var import_create_editor_style_classes = require("../utils/create-editor-style-classes.js");
const PUBLISH_DEPRECATION_WARNING_v9 = `Please provide the postId via RicosEditor biSettings prop and use editorEvents.publish() APIs for publishing.
The getContent(postId, isPublishing) API is deprecated and will be removed in ricos v9.0.0`;
class RicosEditor extends import_react.default.Component {
  constructor(props) {
    super(props);
    this.state = {
      htmlAttributes: {}
    };
    this.isLastChangeEdit = false;
    this.firstEdit = false;
    this.focus = (focusPosition) => {
      this.tiptapAdapter.focus(focusPosition);
    };
    this.blur = () => {
      this.tiptapAdapter.blur();
    };
    this.undo = () => {
      this.tiptapAdapter.tiptapEditor.chain().focus().undo().run();
    };
    this.redo = () => {
      this.tiptapAdapter.tiptapEditor.chain().focus().redo().run();
    };
    this.setContent = (content) => {
      this.props.schema.setContent(content);
    };
    this.openModal = (options) => {
      this.tiptapAdapter.openModal(options);
    };
    this.getContent = async (postId, forPublish) => {
      const content = Promise.resolve(this.props.schema.getContent());
      if (postId && forPublish) {
        console.warn(PUBLISH_DEPRECATION_WARNING_v9);
        const onPublish = this.props.helpers?.onPublish;
        (0, import_publish.publishBI)(content, onPublish, postId);
      }
      return content;
    };
    this.getContentPromise = ({ publishId } = {}) => this.getContent(publishId, !!publishId);
    this.getContentTraits = () => {
      return {
        isEmpty: this.props.schema.isEmpty(),
        isContentChanged: this.props.schema.isContentChanged(),
        isLastChangeEdit: this.isLastChangeEdit
      };
    };
    this.getToolbarProps = (type) => this.tiptapAdapter.getToolbarProps(type);
    this.useToolbarProps = (type) => this.tiptapAdapter.useToolbarProps(type);
    this.useToolbarWixelProps = () => this.tiptapAdapter.useToolbarWixelProps();
    this.useInlineAiState = (invocationId) => this.tiptapAdapter.useInlineAiState(invocationId);
    this.useUndoState = () => this.tiptapAdapter.useUndoState();
    this.getEditorCommands = () => {
      return this.tiptapAdapter.getEditorCommands();
    };
    this.getEditorContainer = () => {
      return this.tiptapAdapter.tiptapEditor.view.dom;
    };
    this.onSelectionUpdate = ({ selectedNodes, content, pmContent }) => {
      const { onAtomicBlockFocus, onChange, onInternalBusyChange } = this.props;
      this.isLastChangeEdit = false;
      (0, import_invoke_atomic_block_focus.invokeAtomicBlockFocus)({
        selectedNodes,
        onAtomicBlockFocus
      });
      if (onChange) {
        onChange(content, { source: "selection" });
      }
      if (onInternalBusyChange) {
        onInternalBusyChange(pmContent);
      }
    };
    this.onUpdate = ({ content, pmContent }) => {
      if (!this.firstEdit) {
        this.firstEdit = true;
        this.props.editor.publishFirstEdit();
      }
      this.isLastChangeEdit = true;
      this.props.onChange?.(content, { source: "edit" });
      this.props.onInternalBusyChange?.(pmContent);
    };
    this.onPublish = async () => {
      const content = Promise.resolve(this.props.schema.getContent());
      const onPublish = this.props.helpers?.onPublish;
      (0, import_publish.publishBI)(content, onPublish);
      this.props.editor.publishContentSaved();
      console.log("editor publish callback");
      return {
        type: "EDITOR_PUBLISH",
        data: await content
      };
    };
    this.prePublish = async () => {
      if (this.props.editorEvents) {
        await this.props.editorEvents.dispatch(import_editor_events_context.EditorEvents.PUBLISH);
      }
      const content = this.props.schema.getContent();
      const onPublish = this.props.helpers?.onPublish;
      (0, import_publish.publishBI)(Promise.resolve(content), onPublish);
      this.props.editor.publishContentSaved();
      return content;
    };
    const { isMobile, experiments, theme, ricosContext } = props;
    this.editorStyleClasses = (0, import_create_editor_style_classes.createEditorStyleClasses)({
      isMobile,
      experiments,
      editorCss: import_editor_styles_scss.default,
      theme
    });
    this.tiptapAdapter = props.editor.adapter;
    const editorDomElement = this.tiptapAdapter.tiptapEditor.view.dom;
    editorDomElement.setAttribute("role", "textbox");
    editorDomElement.setAttribute("aria-multiline", "true");
    editorDomElement.setAttribute(
      "aria-label",
      this.props.htmlAttributes?.ariaLabel || ricosContext.t("Editor_Main_AriaLabel")
    );
    editorDomElement.setAttribute("aria-describedby", import_ricos_tiptap_editor.editorFocusDescriptionId);
  }
  componentWillReceiveProps(newProps) {
    if (newProps.injectedContent) {
      if (!(0, import_is_rich_content_equal.isRichContentEqual)(
        this.props.injectedContent,
        newProps.injectedContent
      )) {
        const content = newProps.injectedContent;
        setTimeout(() => {
          this.props.schema?.setContent(content);
        }, 0);
      }
    }
    if (this.props.placeholder !== newProps.placeholder) {
      (0, import_placeholder.updatePlaceholder)(this.tiptapAdapter.tiptapEditor, newProps.placeholder || "");
    }
  }
  getHtmlAttributes() {
    return {
      autoCapitalize: this.props.htmlAttributes?.autoCapitalize || "off",
      spellCheck: this.props.htmlAttributes?.spellCheck === false ? "false" : "true",
      autoComplete: this.props.htmlAttributes?.autoComplete || "off",
      autoCorrect: this.props.htmlAttributes?.autoCorrect || "off",
      tabIndex: this.props.htmlAttributes?.tabIndex || 0
    };
  }
  componentDidMount() {
    const htmlAttributes = this.getHtmlAttributes();
    this.setState({
      htmlAttributes
    });
    this.props.editorEvents?.subscribe(import_editor_events_context.EditorEvents.RICOS_PUBLISH, this.onPublish);
    (0, import_wix_rich_content_common.addDevToolsClickListener)(
      this.tiptapAdapter.tiptapEditor.view.dom,
      () => this.setState({ debugInvokedWithMouse: true })
    );
    this.props.editor.publishLoaded();
  }
  componentWillUnmount() {
    this.props.editorEvents?.unsubscribe(import_editor_events_context.EditorEvents.RICOS_PUBLISH, this.onPublish);
  }
  render() {
    const { ricosContext, onLoad, schema } = this.props;
    const { htmlAttributes } = this.state;
    const initialContent = schema.getTiptapContent();
    if (!initialContent) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement(
      import_tiptap_editor.RicosTiptapEditor,
      {
        onLoad,
        editor: this.tiptapAdapter.tiptapEditor,
        content: initialContent,
        t: ricosContext.t,
        onUpdate: this.onUpdate,
        onSelectionUpdate: this.onSelectionUpdate,
        htmlAttributes,
        editorStyleClasses: this.editorStyleClasses,
        devToolsElement: this.state.debugInvokedWithMouse || (0, import_dev_tools.shouldShowDebuggingInfo)(ricosContext.debugMode?.includes("all")) ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Suspense, null, /* @__PURE__ */ import_react.default.createElement(
          import_dev_tools.RicosDevToolsLazy,
          {
            version: import_ricos_content.Version.currentVersion,
            mode: "editor",
            contentState: schema.getContent(),
            ricosProps: this.props,
            autoOpen: this.state.debugInvokedWithMouse ?? false,
            editorView: this.tiptapAdapter.tiptapEditor.view
          }
        )) : void 0
      }
    );
  }
}
const RicosEditorWithForwardRef = (0, import_react.forwardRef)(
  (props, ref) => /* @__PURE__ */ import_react.default.createElement(import_ricos_context.RicosContextConsumer, null, (ricosContext) => /* @__PURE__ */ import_react.default.createElement(import_ricos_context.SchemaContextConsumer, null, (schema) => /* @__PURE__ */ import_react.default.createElement(import_ricos_context.EditorContextConsumer, null, (editor) => /* @__PURE__ */ import_react.default.createElement(import_editor_events_context.EditorEventsContext.Consumer, null, (editorEvents) => /* @__PURE__ */ import_react.default.createElement(
    RicosEditor,
    {
      ...props,
      ref,
      ricosContext,
      editor,
      schema,
      editorEvents
    }
  )))))
);
//# sourceMappingURL=ricos-editor.js.map
