"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ScrollTargetRenderer = require("./ScrollTargetRenderer.cjs");
const Slate = require("slate");
const scrollTargetElementType = "scrollTarget";
const isScrollTargetElement = (element) => Slate.Element.isElement(element) && element.type === scrollTargetElementType;
const isScrollTargetElementActive = (editor) => {
  const [link] = Slate.Editor.nodes(editor, { match: isScrollTargetElement });
  return !!link;
};
const scrollTargetElementPlugin = {
  type: scrollTargetElementType,
  render: ScrollTargetRenderer.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) => {
  Slate.Transforms.removeNodes(editor, { match: isScrollTargetElement });
};
const wrapScrollTarget = (editor, identifier) => {
  if (isScrollTargetElementActive(editor)) {
    unwrapScrollTarget(editor);
  }
  const selection = editor.selection;
  const isCollapsed = selection ? Slate.Range.isCollapsed(selection) : false;
  const ScrollTarget = {
    type: scrollTargetElementType,
    identifier,
    children: [{ text: "" }]
  };
  if (!isCollapsed) {
    Slate.Transforms.collapse(editor, { edge: "start" });
  }
  Slate.Transforms.insertNodes(editor, ScrollTarget);
};
exports.isScrollTargetElement = isScrollTargetElement;
exports.isScrollTargetElementActive = isScrollTargetElementActive;
exports.scrollTargetElementPlugin = scrollTargetElementPlugin;
exports.scrollTargetElementType = scrollTargetElementType;
//# sourceMappingURL=ScrollTargetElement.cjs.map
