"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const reactBinding = require("@contember/react-binding");
const ui = require("@contember/ui");
const react = require("react");
const Slate = require("slate");
const slateReact = require("slate-react");
const reactBindingUi = require("@contember/react-binding-ui");
const RichEditor = require("../RichEditor.cjs");
const useRichTextFieldNodes = require("./useRichTextFieldNodes.cjs");
const createEditor = require("../editorFactory/createEditor.cjs");
const ParagraphElement = require("../plugins/element/paragraphs/ParagraphElement.cjs");
const HoveringToolbars = require("../toolbars/HoveringToolbars.cjs");
const LegacyDeprecatedEditorFormerlyKnownAsRichTextField = reactBinding.Component(
  (props) => {
    const entity = reactBinding.useEntity();
    const environment = entity.environment;
    const getParent = entity.getAccessor;
    const desugaredField = react.useMemo(
      () => reactBinding.QueryLanguage.desugarRelativeSingleField(props, environment),
      [environment, props]
    );
    const fieldAccessor = react.useMemo(
      () => entity.getRelativeSingleField(desugaredField),
      [entity, desugaredField]
    );
    const [contemberFieldElementCache] = react.useState(() => /* @__PURE__ */ new WeakMap());
    const isMutating = reactBinding.useMutationState();
    const [editor] = react.useState(() => {
      return createEditor.createEditor({
        plugins: props.plugins,
        augmentEditor: props.augmentEditor,
        augmentEditorBuiltins: props.augmentEditorBuiltins,
        defaultElementType: ParagraphElement.paragraphElementType,
        addEditorBuiltins: (editor2) => editor2
      });
    });
    const valueNodes = useRichTextFieldNodes.useRichTextFieldNodes({
      editor,
      fieldAccessor,
      contemberFieldElementCache
    });
    const serialize = editor.serializeNodes;
    const onChange = react.useCallback(
      (value) => {
        getParent().batchUpdates((getAccessor) => {
          const fieldAccessor2 = getAccessor().getRelativeSingleField(desugaredField);
          if (Slate.Node.string({ type: "dummy", children: value }) === "") {
            fieldAccessor2.updateValue(fieldAccessor2.valueOnServer === null ? null : "");
            return;
          }
          fieldAccessor2.updateValue(serialize(value));
          contemberFieldElementCache.set(getAccessor().getRelativeSingleField(desugaredField), value);
        });
      },
      [getParent, contemberFieldElementCache, desugaredField, serialize]
    );
    return /* @__PURE__ */ jsxRuntime.jsx(
      ui.FieldContainer,
      {
        label: props.label,
        size: props.size,
        labelDescription: props.labelDescription,
        labelPosition: props.labelPosition,
        description: props.description,
        useLabelElement: props.useLabelElement,
        errors: reactBindingUi.useAccessorErrors(fieldAccessor.errors?.errors),
        children: /* @__PURE__ */ jsxRuntime.jsx(slateReact.Slate, { editor, value: valueNodes, onChange, children: /* @__PURE__ */ jsxRuntime.jsx(
          ui.EditorCanvas,
          {
            underlyingComponent: slateReact.Editable,
            componentProps: {
              readOnly: isMutating,
              renderElement: editor.renderElement,
              renderLeaf: editor.renderLeaf,
              onKeyDown: editor.onKeyDown,
              onFocusCapture: editor.onFocus,
              onBlurCapture: editor.onBlur,
              onDOMBeforeInput: editor.onDOMBeforeInput
            },
            children: /* @__PURE__ */ jsxRuntime.jsx(
              HoveringToolbars.HoveringToolbars,
              {
                blockButtons: props.blockButtons,
                inlineButtons: props.inlineButtons ?? defaultInlineButtons
              }
            )
          }
        ) })
      }
    );
  },
  (props) => /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Field, { defaultValue: props.defaultValue, field: props.field, isNonbearing: props.isNonbearing }),
    props.label,
    props.labelDescription,
    props.description
  ] }),
  "LegacyDeprecatedEditorFormerlyKnownAsRichTextField"
);
const RB = RichEditor.RichEditor.buttons;
const defaultInlineButtons = [
  [RB.bold, RB.italic, RB.underline, RB.anchor],
  [RB.strikeThrough, RB.code]
];
exports.LegacyDeprecatedEditorFormerlyKnownAsRichTextField = LegacyDeprecatedEditorFormerlyKnownAsRichTextField;
//# sourceMappingURL=LegacyDeprecatedEditorFormerlyKnownAsRichTextField.cjs.map
