import { ScrollTargetRenderer } from "./ScrollTargetRenderer.js";
import { Element, Editor, Transforms, Range } from "slate";
const scrollTargetElementType = "scrollTarget";
const isScrollTargetElement = (element) => Element.isElement(element) && element.type === scrollTargetElementType;
const isScrollTargetElementActive = (editor) => {
  const [link] = Editor.nodes(editor, { match: isScrollTargetElement });
  return !!link;
};
const scrollTargetElementPlugin = {
  type: scrollTargetElementType,
  render: ScrollTargetRenderer,
  isActive: ({ editor }) => isScrollTargetElementActive(editor),
  isInline: true,
  isVoid: true,
  toggleElement: ({ editor, suchThat }) => {
    if (isScrollTargetElementActive(editor)) {
      unwrapScrollTarget(editor);
    } else {
      let identifier = suchThat?.identifier ?? prompt("Insert the identifier:");
      if (!identifier) {
        return;
      }
      wrapScrollTarget(editor, identifier);
    }
  }
};
const unwrapScrollTarget = (editor) => {
  Transforms.removeNodes(editor, { match: isScrollTargetElement });
};
const wrapScrollTarget = (editor, identifier) => {
  if (isScrollTargetElementActive(editor)) {
    unwrapScrollTarget(editor);
  }
  const selection = editor.selection;
  const isCollapsed = selection ? Range.isCollapsed(selection) : false;
  const ScrollTarget = {
    type: scrollTargetElementType,
    identifier,
    children: [{ text: "" }]
  };
  if (!isCollapsed) {
    Transforms.collapse(editor, { edge: "start" });
  }
  Transforms.insertNodes(editor, ScrollTarget);
};
export { isScrollTargetElement, isScrollTargetElementActive, scrollTargetElementPlugin, scrollTargetElementType };
//# sourceMappingURL=ScrollTargetElement.js.map
