import { Element, Editor, Transforms, Text } from "slate";
import { ContemberEditor } from "../../../../ContemberEditor/index.js";
import { listItemElementType } from "../ListItemElement.js";
import { isListElement } from "../ListElement.js";
const toggleListElement = (editor, elementType, suchThat, otherKindOfList) => {
  if (editor.isElementActive(elementType, suchThat)) {
    const closestListEntry = ContemberEditor.closest(editor, {
      match: (node) => Element.isElement(node) && node.type === elementType
    });
    if (!closestListEntry) {
      return;
    }
    return Editor.withoutNormalizing(editor, () => {
      const [closestList, closestListPath] = closestListEntry;
      for (let i = closestList.children.length - 1; i >= 0; i--) {
        const currentItemPath = [...closestListPath, i];
        if (Editor.hasInlines(editor, closestList.children[i])) {
          Transforms.wrapNodes(editor, editor.createDefaultElement([]), {
            at: currentItemPath
          });
          Transforms.unwrapNodes(editor, {
            at: [...currentItemPath, 0]
          });
        } else {
          Transforms.unwrapNodes(editor, { at: currentItemPath });
        }
      }
      Transforms.unwrapNodes(editor, { at: closestListPath });
    });
  }
  if (editor.isElementActive(elementType) || editor.isElementActive(otherKindOfList)) {
    const closestListEntry = ContemberEditor.closest(editor, {
      match: (node) => isListElement(node)
    });
    if (!closestListEntry) {
      return;
    }
    return Editor.withoutNormalizing(editor, () => {
      const [, closestListPath] = closestListEntry;
      ContemberEditor.ejectElement(editor, closestListPath);
      Transforms.setNodes(editor, { ...suchThat, type: elementType }, { at: closestListPath });
    });
  }
  const selection = editor.selection;
  if (!selection) {
    return;
  }
  const closestViableParentEntry = ContemberEditor.closestViableBlockContainerEntry(editor);
  if (!closestViableParentEntry) {
    return;
  }
  Editor.withoutNormalizing(editor, () => {
    const [targetParent, targetParentPath] = closestViableParentEntry;
    if (Element.isElement(targetParent) && Editor.hasInlines(editor, targetParent)) {
      const listItemPath = [...targetParentPath, 0];
      Transforms.insertNodes(editor, { type: listItemElementType, children: [] }, { at: listItemPath });
      Transforms.moveNodes(editor, {
        to: [...listItemPath, 0],
        match: (node) => Text.isText(node) || Editor.isInline(editor, node),
        at: {
          anchor: Editor.start(editor, [...targetParentPath, 1]),
          focus: Editor.end(editor, [...targetParentPath, targetParent.children.length])
        }
      });
      Transforms.wrapNodes(editor, { type: elementType, children: [] }, { at: listItemPath });
    } else {
      const [selectionStart, selectionEnd] = Editor.edges(editor, selection);
      const relativeStartIndex = selectionStart.path[targetParentPath.length];
      const relativeEndIndex = selectionEnd.path[targetParentPath.length];
      for (let i = relativeStartIndex; i <= relativeEndIndex; i++) {
        Transforms.wrapNodes(editor, { type: listItemElementType, children: [] }, { at: [...targetParentPath, i] });
      }
      const emptyList = {
        ...suchThat,
        type: elementType,
        children: []
      };
      const listPath = [...targetParentPath, relativeStartIndex];
      Transforms.insertNodes(editor, emptyList, { at: listPath });
      for (let i = relativeStartIndex; i <= relativeEndIndex; i++) {
        Transforms.moveNodes(editor, {
          to: [...listPath, i - relativeStartIndex],
          at: [...targetParentPath, relativeStartIndex + 1]
        });
      }
    }
  });
};
export { toggleListElement };
//# sourceMappingURL=toggleListElement.js.map
