import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { Component, useEntity, QueryLanguage, useMutationState, Field } from "@contember/react-binding";
import { FieldContainer, EditorCanvas } from "@contember/ui";
import { useMemo, useState, useCallback } from "react";
import { Node } from "slate";
import { Slate, Editable } from "slate-react";
import { useAccessorErrors } from "@contember/react-binding-ui";
import { RichEditor } from "../RichEditor.js";
import { useRichTextFieldNodes } from "./useRichTextFieldNodes.js";
import { createEditor } from "../editorFactory/createEditor.js";
import { paragraphElementType } from "../plugins/element/paragraphs/ParagraphElement.js";
import { HoveringToolbars } from "../toolbars/HoveringToolbars.js";
const LegacyDeprecatedEditorFormerlyKnownAsRichTextField = Component(
  (props) => {
    const entity = useEntity();
    const environment = entity.environment;
    const getParent = entity.getAccessor;
    const desugaredField = useMemo(
      () => QueryLanguage.desugarRelativeSingleField(props, environment),
      [environment, props]
    );
    const fieldAccessor = useMemo(
      () => entity.getRelativeSingleField(desugaredField),
      [entity, desugaredField]
    );
    const [contemberFieldElementCache] = useState(() => /* @__PURE__ */ new WeakMap());
    const isMutating = useMutationState();
    const [editor] = useState(() => {
      return createEditor({
        plugins: props.plugins,
        augmentEditor: props.augmentEditor,
        augmentEditorBuiltins: props.augmentEditorBuiltins,
        defaultElementType: paragraphElementType,
        addEditorBuiltins: (editor2) => editor2
      });
    });
    const valueNodes = useRichTextFieldNodes({
      editor,
      fieldAccessor,
      contemberFieldElementCache
    });
    const serialize = editor.serializeNodes;
    const onChange = useCallback(
      (value) => {
        getParent().batchUpdates((getAccessor) => {
          const fieldAccessor2 = getAccessor().getRelativeSingleField(desugaredField);
          if (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__ */ jsxDEV(
      FieldContainer,
      {
        label: props.label,
        size: props.size,
        labelDescription: props.labelDescription,
        labelPosition: props.labelPosition,
        description: props.description,
        useLabelElement: props.useLabelElement,
        errors: useAccessorErrors(fieldAccessor.errors?.errors),
        children: /* @__PURE__ */ jsxDEV(Slate, { editor, value: valueNodes, onChange, children: /* @__PURE__ */ jsxDEV(
          EditorCanvas,
          {
            underlyingComponent: Editable,
            componentProps: {
              readOnly: isMutating,
              renderElement: editor.renderElement,
              renderLeaf: editor.renderLeaf,
              onKeyDown: editor.onKeyDown,
              onFocusCapture: editor.onFocus,
              onBlurCapture: editor.onBlur,
              onDOMBeforeInput: editor.onDOMBeforeInput
            },
            children: /* @__PURE__ */ jsxDEV(
              HoveringToolbars,
              {
                blockButtons: props.blockButtons,
                inlineButtons: props.inlineButtons ?? defaultInlineButtons
              },
              void 0,
              false,
              {
                fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/legacyDeprecatedEditorFormerlyKnownAsRichTextField/LegacyDeprecatedEditorFormerlyKnownAsRichTextField.tsx",
                lineNumber: 104,
                columnNumber: 8
              },
              void 0
            )
          },
          void 0,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/legacyDeprecatedEditorFormerlyKnownAsRichTextField/LegacyDeprecatedEditorFormerlyKnownAsRichTextField.tsx",
            lineNumber: 92,
            columnNumber: 7
          },
          void 0
        ) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/legacyDeprecatedEditorFormerlyKnownAsRichTextField/LegacyDeprecatedEditorFormerlyKnownAsRichTextField.tsx",
          lineNumber: 91,
          columnNumber: 6
        }, void 0)
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/legacyDeprecatedEditorFormerlyKnownAsRichTextField/LegacyDeprecatedEditorFormerlyKnownAsRichTextField.tsx",
        lineNumber: 82,
        columnNumber: 5
      },
      void 0
    );
  },
  (props) => /* @__PURE__ */ jsxDEV(Fragment, { children: [
    /* @__PURE__ */ jsxDEV(Field, { defaultValue: props.defaultValue, field: props.field, isNonbearing: props.isNonbearing }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/legacyDeprecatedEditorFormerlyKnownAsRichTextField/LegacyDeprecatedEditorFormerlyKnownAsRichTextField.tsx",
      lineNumber: 115,
      columnNumber: 5
    }, void 0),
    props.label,
    props.labelDescription,
    props.description
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/legacyDeprecatedEditorFormerlyKnownAsRichTextField/LegacyDeprecatedEditorFormerlyKnownAsRichTextField.tsx",
    lineNumber: 114,
    columnNumber: 4
  }, void 0),
  "LegacyDeprecatedEditorFormerlyKnownAsRichTextField"
);
const RB = RichEditor.buttons;
const defaultInlineButtons = [
  [RB.bold, RB.italic, RB.underline, RB.anchor],
  [RB.strikeThrough, RB.code]
];
export {
  LegacyDeprecatedEditorFormerlyKnownAsRichTextField
};
//# sourceMappingURL=LegacyDeprecatedEditorFormerlyKnownAsRichTextField.js.map
