import { jsx } from "react/jsx-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__ */ jsx(ErrorBoundary, { fallback: /* @__PURE__ */ jsx("span", { style: { background: "red", color: "white" }, children: "Invalid element" }), children: /* @__PURE__ */ jsx(ReferenceElementWrapper, { element: props.element, children }) });
    }
    if (path.length === 1) {
      return /* @__PURE__ */ jsx(SortableBlock, { element: props.element, children });
    }
    return children;
  };
};
const SortableBlock = ({ children, element }) => {
  const editor = useSlateStatic();
  const sortedBlocks = useContext(SortedBlocksContext);
  const [index] = ReactEditor.findPath(editor, element);
  return /* @__PURE__ */ jsx(SortableRepeaterItem, { index, children: /* @__PURE__ */ jsx(EditorBlock, { dragHandle: /* @__PURE__ */ jsx(SortableRepeaterItemHandle, { children: /* @__PURE__ */ jsx(DragHandle, {}) }), children }) }, sortedBlocks[index]?.id);
};
export {
  overrideRenderElement
};
//# sourceMappingURL=overrideRenderElement.js.map
