"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Slate = require("slate");
const index = require("../../ContemberEditor/index.cjs");
const ReferenceElement = require("../elements/ReferenceElement.cjs");
const prepareElementForInsertion = (editor, node) => {
  const selection = editor.selection;
  let targetLocation;
  if (selection) {
    targetLocation = selection;
  } else if (editor.children.length) {
    targetLocation = Slate.Editor.end(editor, []);
  } else {
    targetLocation = { path: [], offset: 0 };
  }
  if (Slate.Range.isRange(targetLocation)) {
    if (Slate.Range.isExpanded(targetLocation)) {
      const [, end2] = Slate.Range.edges(targetLocation);
      const pointRef = Slate.Editor.pointRef(editor, end2);
      Slate.Transforms.delete(editor, { at: targetLocation });
      targetLocation = pointRef.unref();
    } else {
      targetLocation = targetLocation.focus;
    }
  }
  const targetPoint = targetLocation;
  if (!ReferenceElement.isReferenceElement(node)) {
    if (targetPoint.offset === 0) {
      return targetPoint.path;
    }
    Slate.Transforms.splitNodes(editor, {
      at: targetPoint
    });
    return Slate.Path.next(targetPoint.path);
  }
  const [closestBlockElement, closestBlockPath] = index.ContemberEditor.closestBlockEntry(editor, {
    at: targetPoint
  });
  if (editor.canContainAnyBlocks(closestBlockElement)) {
    return targetPoint.path;
  }
  if (ReferenceElement.isReferenceElement(closestBlockElement)) {
    const newPath = Slate.Path.next(closestBlockPath);
    Promise.resolve().then(() => {
      Slate.Transforms.select(editor, newPath);
    });
    return newPath;
  }
  if (Slate.Node.string(closestBlockElement) === "") {
    Slate.Transforms.removeNodes(editor, {
      at: closestBlockPath
    });
    return closestBlockPath;
  }
  const [start, end] = Slate.Editor.edges(editor, closestBlockPath);
  if (Slate.Point.equals(start, targetPoint)) {
    return closestBlockPath;
  } else if (Slate.Point.equals(end, targetPoint)) {
    return Slate.Path.next(closestBlockPath);
  } else {
    Slate.Transforms.splitNodes(editor, {
      at: targetPoint
    });
    return Slate.Path.next(Slate.Path.parent(targetPoint.path));
  }
};
exports.prepareElementForInsertion = prepareElementForInsertion;
//# sourceMappingURL=prepareElementForInsertion.cjs.map
