import { Element, Editor, Node, Transforms, Range, Path } from "slate";
import { AnchorModifications } from "./AnchorModifications.js";
import { AnchorRenderer } from "./AnchorRenderer.js";
const anchorElementType = "anchor";
const isAnchorElement = (element) => Element.isElement(element) && element.type === anchorElementType;
const isAnchorElementActive = (editor) => {
  const [link] = Editor.nodes(editor, { match: isAnchorElement });
  return !!link;
};
const anchorElementPlugin = {
  type: anchorElementType,
  isInline: true,
  render: AnchorRenderer,
  isActive: ({ editor }) => isAnchorElementActive(editor),
  toggleElement: ({ editor, suchThat }) => {
    if (isAnchorElementActive(editor)) {
      AnchorModifications.unwrapAnchor(editor);
    } else {
      const href = suchThat?.href ?? prompt("Insert the URL:");
      if (!href) {
        return;
      }
      AnchorModifications.wrapAnchor(editor, href);
    }
  },
  normalizeNode: ({ element, path, editor, preventDefault }) => {
    if (Node.string(element) === "") {
      const selection = editor.selection;
      Transforms.removeNodes(editor, {
        at: path
      });
      if (selection && Range.isCollapsed(selection) && Path.isCommon(path, selection.focus.path)) {
        Transforms.select(editor, Path.parent(path));
      }
      return preventDefault();
    }
  }
};
export {
  anchorElementPlugin,
  anchorElementType,
  isAnchorElement,
  isAnchorElementActive
};
//# sourceMappingURL=AnchorElement.js.map
