"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ReferenceElementWrapper = require("../references/ReferenceElementWrapper.cjs");
const reactErrorBoundary = require("react-error-boundary");
const slateReact = require("slate-react");
const ui = require("@contember/ui");
const react = require("react");
const SortedBlocksContext = require("../state/SortedBlocksContext.cjs");
const SortableRepeaterItem = require("../../../collections/Repeater/SortableRepeaterItem.cjs");
const SortableRepeaterItemHandle = require("../../../collections/Repeater/SortableRepeaterItemHandle.cjs");
const ElementWithReference = require("../elements/ElementWithReference.cjs");
const DragHandle = require("../../../../ui/DragHandle.cjs");
const overrideRenderElement = (editor) => {
  const { renderElement } = editor;
  editor.renderElement = (props) => {
    let children = renderElement(props);
    const path = slateReact.ReactEditor.findPath(editor, props.element);
    if (ElementWithReference.isElementWithReference(props.element)) {
      children = /* @__PURE__ */ jsxRuntime.jsx(reactErrorBoundary.ErrorBoundary, { fallback: /* @__PURE__ */ jsxRuntime.jsx("span", { style: { background: "red", color: "white" }, children: "Invalid element" }), children: /* @__PURE__ */ jsxRuntime.jsx(ReferenceElementWrapper.ReferenceElementWrapper, { element: props.element, children }) });
    }
    if (path.length === 1) {
      return /* @__PURE__ */ jsxRuntime.jsx(SortableBlock, { element: props.element, children });
    }
    return children;
  };
};
const SortableBlock = ({ children, element }) => {
  const editor = slateReact.useSlateStatic();
  const sortedBlocks = react.useContext(SortedBlocksContext.SortedBlocksContext);
  const [index] = slateReact.ReactEditor.findPath(editor, element);
  return /* @__PURE__ */ jsxRuntime.jsx(SortableRepeaterItem.SortableRepeaterItem, { index, children: /* @__PURE__ */ jsxRuntime.jsx(ui.EditorBlock, { dragHandle: /* @__PURE__ */ jsxRuntime.jsx(SortableRepeaterItemHandle.SortableRepeaterItemHandle, { children: /* @__PURE__ */ jsxRuntime.jsx(DragHandle.DragHandle, {}) }), children }) }, sortedBlocks[index]?.id);
};
exports.overrideRenderElement = overrideRenderElement;
//# sourceMappingURL=overrideRenderElement.cjs.map
