"use strict";
"use client";
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 editable_text_v2_exports = {};
__export(editable_text_v2_exports, {
  EditableTextV2: () => EditableTextV2,
  default: () => editable_text_v2_default
});
module.exports = __toCommonJS(editable_text_v2_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_slate = require("slate");
var import_is_hotkey = __toESM(require("is-hotkey"));
var import_slate_react = require("slate-react");
var import_rich_text_v2 = require("../../../../../controls/rich-text-v2");
var import__ = require("../../..");
var import_builder_edit_mode = require("../../../../../state/modules/builder-edit-mode");
var import_poll_box_model = require("../../../poll-box-model");
var import_slate2 = require("../../../../../slate");
var import_useSyncDOMSelection = require("./useSyncDOMSelection");
var import_render_element = require("./render-element");
var import_render_leaf = require("./render-leaf");
var import_useRemoteChanges = require("./useRemoteChanges");
var import_usePresetValue = require("./usePresetValue");
function EditableTextV2({ text, definition, control }) {
  const plugins = (0, import_react.useMemo)(() => definition.config.plugins, [definition]);
  const [editor] = (0, import_react.useState)(
    () => plugins.reduceRight(
      (editor2, plugin) => plugin?.withPlugin?.(editor2) ?? editor2,
      (0, import_slate2.withLocalChanges)((0, import_slate2.withBuilder)((0, import_slate_react.withReact)((0, import_slate.createEditor)())))
    )
  );
  (0, import_react.useEffect)(() => {
    if (control == null)
      return;
    const element = import_slate_react.ReactEditor.toDOMNode(editor, editor);
    return (0, import_poll_box_model.pollBoxModel)({
      element,
      onBoxModelChange: (boxModel) => control.changeBoxModel(boxModel)
    });
  }, [editor, control]);
  const isPreservingFocus = (0, import_react.useRef)(false);
  (0, import_useSyncDOMSelection.useSyncDOMSelection)(editor, isPreservingFocus);
  const editMode = (0, import__.useBuilderEditMode)();
  (0, import_react.useEffect)(() => {
    if (editMode !== import_builder_edit_mode.BuilderEditMode.CONTENT) {
      isPreservingFocus.current = false;
      import_slate_react.ReactEditor.deselect(editor);
    }
  }, [editMode]);
  (0, import_useRemoteChanges.useSyncRemoteChanges)(editor, text);
  const presetValue = (0, import_usePresetValue.usePresetValue)(definition);
  const initialValue = (0, import_react.useMemo)(
    () => (text && import_rich_text_v2.RichText.dataToNodes(text)) ?? presetValue,
    [text, presetValue]
  );
  (0, import_react.useEffect)(() => {
    control?.setEditor(editor);
    control?.setDefaultValue(import_usePresetValue.defaultValue);
  }, [control, editor]);
  (0, import_react.useEffect)(() => {
    if (initialValue === presetValue) {
      control?.onLocalUserChange();
    }
  }, [control, initialValue, presetValue]);
  const renderElement = (0, import_react.useCallback)(
    (props) => {
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_render_element.RichTextV2Element, { ...props, definition, plugins });
    },
    [plugins, definition]
  );
  const renderLeaf = (0, import_react.useCallback)(
    (props) => {
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_render_leaf.RichTextV2Leaf, { ...props, definition, plugins });
    },
    [plugins, definition]
  );
  const handleFocus = (0, import_react.useCallback)(() => {
    isPreservingFocus.current = true;
    control?.select();
  }, [control]);
  const handleKeyDown = (0, import_react.useCallback)(
    (e) => {
      if ((0, import_is_hotkey.default)("mod+shift+z", e))
        return control?.redo();
      if ((0, import_is_hotkey.default)("mod+z", e))
        return control?.undo();
      if ((0, import_is_hotkey.default)("escape")(e)) {
        isPreservingFocus.current = false;
        import_slate_react.ReactEditor.blur(editor);
        control?.switchToBuildMode();
      }
      if (editMode === import_builder_edit_mode.BuilderEditMode.CONTENT) {
        e.stopPropagation();
      }
      plugins.forEach((plugin) => plugin?.onKeyDown?.(e, editor));
    },
    [control, plugins, editor, editMode]
  );
  const handleKeyUp = (0, import_react.useCallback)(
    (e) => {
      if (editMode === import_builder_edit_mode.BuilderEditMode.CONTENT) {
        e.stopPropagation();
        e.preventDefault();
      }
    },
    [control, editor, editMode]
  );
  const handleClick = (0, import_react.useCallback)(
    (e) => {
      if (editMode === import_builder_edit_mode.BuilderEditMode.CONTENT) {
        e.stopPropagation();
        e.preventDefault();
      }
    },
    [editMode]
  );
  const handleBlur = (0, import_react.useCallback)((e) => {
    if (e.relatedTarget == null)
      return;
    if (e.relatedTarget?.getAttribute("contenteditable") === "true")
      isPreservingFocus.current = false;
  }, []);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_slate_react.Slate, { editor, value: initialValue, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_slate_react.Editable,
    {
      renderLeaf,
      renderElement,
      onFocus: handleFocus,
      onKeyDown: handleKeyDown,
      onKeyUp: handleKeyUp,
      onClick: handleClick,
      onBlur: handleBlur,
      readOnly: editMode !== import_builder_edit_mode.BuilderEditMode.CONTENT,
      placeholder: "Write some text..."
    }
  ) });
}
var editable_text_v2_default = EditableTextV2;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  EditableTextV2
});
//# sourceMappingURL=editable-text-v2.js.map