import { jsxDEV } from "react/jsx-dev-runtime";
import { ReferenceElementWrapper } from "../references/ReferenceElementWrapper.js";
import { ErrorBoundary } from "react-error-boundary";
import { ReactEditor, useSlateStatic } from "slate-react";
import { EditorBlock } from "@contember/ui";
import { useContext } from "react";
import { SortedBlocksContext } from "../state/SortedBlocksContext.js";
import { SortableRepeaterItem } from "../../../collections/Repeater/SortableRepeaterItem.js";
import { SortableRepeaterItemHandle } from "../../../collections/Repeater/SortableRepeaterItemHandle.js";
import { isElementWithReference } from "../elements/ElementWithReference.js";
import { DragHandle } from "../../../../ui/DragHandle.js";
const overrideRenderElement = (editor) => {
  const { renderElement } = editor;
  editor.renderElement = (props) => {
    let children = renderElement(props);
    const path = ReactEditor.findPath(editor, props.element);
    if (isElementWithReference(props.element)) {
      children = /* @__PURE__ */ jsxDEV(ErrorBoundary, { fallback: /* @__PURE__ */ jsxDEV("span", { style: { background: "red", color: "white" }, children: "Invalid element" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/editor/overrideRenderElement.tsx",
        lineNumber: 21,
        columnNumber: 30
      }, void 0), children: /* @__PURE__ */ jsxDEV(ReferenceElementWrapper, { element: props.element, children }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/editor/overrideRenderElement.tsx",
        lineNumber: 22,
        columnNumber: 6
      }, void 0) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/editor/overrideRenderElement.tsx",
        lineNumber: 21,
        columnNumber: 5
      }, void 0);
    }
    if (path.length === 1) {
      return /* @__PURE__ */ jsxDEV(SortableBlock, { element: props.element, children }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/editor/overrideRenderElement.tsx",
        lineNumber: 27,
        columnNumber: 11
      }, void 0);
    }
    return children;
  };
};
const SortableBlock = ({ children, element }) => {
  const editor = useSlateStatic();
  const sortedBlocks = useContext(SortedBlocksContext);
  const [index] = ReactEditor.findPath(editor, element);
  return /* @__PURE__ */ jsxDEV(SortableRepeaterItem, { index, children: /* @__PURE__ */ jsxDEV(EditorBlock, { dragHandle: /* @__PURE__ */ jsxDEV(SortableRepeaterItemHandle, { children: /* @__PURE__ */ jsxDEV(DragHandle, {}, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/editor/overrideRenderElement.tsx",
    lineNumber: 41,
    columnNumber: 57
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/editor/overrideRenderElement.tsx",
    lineNumber: 41,
    columnNumber: 29
  }, void 0), children }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/editor/overrideRenderElement.tsx",
    lineNumber: 41,
    columnNumber: 4
  }, void 0) }, sortedBlocks[index]?.id, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/editor/overrideRenderElement.tsx",
    lineNumber: 40,
    columnNumber: 3
  }, void 0);
};
export {
  overrideRenderElement
};
//# sourceMappingURL=overrideRenderElement.js.map
