"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_tiptap_editor_exports = {};
__export(ricos_tiptap_editor_exports, {
  RicosTiptapEditor: () => RicosTiptapEditor,
  editorFocusDescriptionId: () => editorFocusDescriptionId
});
module.exports = __toCommonJS(ricos_tiptap_editor_exports);
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../../../ricos-content/index.js");
var import_ricos_context = require("./../../../ricos-context/index.js");
var import_toolbar_context = require("./../../../ricos-context/toolbar-context.js");
var import_ricos_styles = require("./../../../ricos-styles/index.js");
var import_ricos_types = require("./../../../ricos-types/index.js");
var import_get_plugin_id_by_insert_plugin = require("./../../../_shared/get-plugin-id-by-insert-plugin.js");
var import_react2 = require("./../../../tiptap/react/index.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_use_is_toolbar_empty = require("./../../../toolbars-v3/ricos-toolbars/use-is-toolbar-empty.js");
var import_context = require("../../context.js");
var import_tiptap_editor_styles_scss = __toESM(require("../../statics/styles/tiptap-editor-styles-scss.js"));
var import_focus_reducer = require("./focus-reducer.js");
var import_use_editor_side_effects = require("./use-editor-side-effects.js");
var import_use_jsdom_width_fix = require("./use-jsdom-width-fix.js");
var import_use_keydown_listener = require("./use-keydown-listener.js");
const editorFocusDescriptionId = "editor-container-tooltip";
const RicosTiptapEditor = (props) => {
  const { editor, onUpdate, onSelectionUpdate, editorStyleClasses, htmlAttributes, ...context } = props;
  const { localeContent, lastInteraction, t, tabFocusBehavior } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const { stylePreset } = (0, import_react.useContext)(import_ricos_styles.StylesContext);
  const [ref, setRef] = (0, import_react.useState)(null);
  const [state, dispatch] = (0, import_react.useReducer)(import_focus_reducer.focusReducer, "not-focused");
  (0, import_ricos_context.useTrackRender)("RicosTiptapEditor");
  (0, import_use_jsdom_width_fix.useJsdomWidthFix)(ref, editor);
  (0, import_use_editor_side_effects.useEditorSideEffects)({ editor, onUpdate, onSelectionUpdate });
  const handleKeyDown = (event) => {
    if (event.key !== "Escape") {
      dispatch({ type: "keydown", event });
      return;
    }
    const currentSelection = editor.state.selection;
    setTimeout(() => {
      if (!currentSelection.eq(editor.state.selection)) {
        return;
      }
      dispatch({ type: "keydown", event });
    }, 0);
  };
  const handleMouseDown = () => {
    dispatch({ type: "pointer-focus" });
  };
  (0, import_use_keydown_listener.useKeydownListener)(ref, handleKeyDown);
  const ricosTiptapContextValue = (0, import_react.useMemo)(() => {
    return {
      context: {
        t: context.t,
        theme: context.theme,
        onLoad: context.onLoad,
        devToolsElement: context.devToolsElement
      }
    };
  }, [context.t, context.theme, context.onLoad, context.devToolsElement]);
  return /* @__PURE__ */ import_react.default.createElement(import_context.RicosTiptapContext.Provider, { value: ricosTiptapContextValue }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      "data-hook": import_editor_common.RICOS_TIPTAP_EDITOR_DATA_HOOK,
      dir: (0, import_wix_rich_content_common.getLangDir)(localeContent),
      className: (0, import_classnames.default)(
        editorStyleClasses?.containerClassName,
        import_tiptap_editor_styles_scss.default.ricosTiptapEditor,
        {
          [import_tiptap_editor_styles_scss.default.focusRing]: state === "editor-focused-outlined" || state === "container-focused" || state === "awaiting-keyboard-input"
        },
        stylePreset
      ),
      style: state === "container-focused" || state === "awaiting-keyboard-input" ? { caretColor: "transparent" } : {},
      onMouseDown: handleMouseDown,
      onBlur: () => {
        dispatch({ type: "blur" });
      },
      onFocus: () => {
        const getKeyboardFocusType = () => {
          if (tabFocusBehavior === "container") {
            return "keyboard-focus-awaiting";
          }
          return "keyboard-focus";
        };
        dispatch({
          type: lastInteraction === "keyboard" ? getKeyboardFocusType() : "pointer-focus"
        });
      },
      ...htmlAttributes,
      tabIndex: -1,
      ref: setRef
    },
    /* @__PURE__ */ import_react.default.createElement(FloatingPluginMenu, { isMobile: context.isMobile }),
    /* @__PURE__ */ import_react.default.createElement(import_react2.EditorContent, { editor, className: editorStyleClasses?.editorClassName }),
    props.devToolsElement
  ), (state === "container-focused" || state === "awaiting-keyboard-input") && /* @__PURE__ */ import_react.default.createElement("span", { "aria-live": "assertive", role: "alert", className: import_tiptap_editor_styles_scss.default.srOnly }, t("Editor_Blur_Description")), /* @__PURE__ */ import_react.default.createElement("span", { id: editorFocusDescriptionId, className: import_tiptap_editor_styles_scss.default.srOnly }, t("Editor_Focus_Description")));
};
const FloatingPluginMenu = ({ isMobile }) => {
  const editorQuery = (0, import_react.useContext)(import_ricos_context.EditorQueryContext);
  const { isMultipleSelection, isSingleRootTextBlock } = editorQuery.query;
  const shouldShowPlusButton = (0, import_ricos_content.or)([isMultipleSelection, isSingleRootTextBlock]);
  const sideToolbarSettings = (0, import_toolbar_context.useToolbarSettings)((0, import_toolbar_context.getToolbarSettings)(import_ricos_types.ToolbarType.SIDE));
  const insertPluginToolbarSettings = (0, import_toolbar_context.useToolbarSettings)(
    (0, import_toolbar_context.getToolbarSettings)(import_ricos_types.ToolbarType.INSERT_PLUGIN)
  );
  const supportedButtonIds = insertPluginToolbarSettings?.getButtons?.()?.desktop?.map(import_get_plugin_id_by_insert_plugin.getPluginIdByInsertPlugin);
  const isToolbarEmpty = (0, import_use_is_toolbar_empty.useIsToolbarEmpty)(import_ricos_types.ToolbarType.SIDE);
  if (isMobile || isToolbarEmpty) {
    return null;
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_toolbars_v3.FloatingAddPluginMenu,
    {
      onClick: sideToolbarSettings?.onClick,
      isVisible: shouldShowPlusButton,
      supportedButtonIds
    }
  );
};
//# sourceMappingURL=ricos-tiptap-editor.js.map
