"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Slate = require("slate");
const index = require("../../../../ContemberEditor/index.cjs");
const ListItemElement = require("../ListItemElement.cjs");
const ListElement = require("../ListElement.cjs");
const toggleListElement = (editor, elementType, suchThat, otherKindOfList) => {
  if (editor.isElementActive(elementType, suchThat)) {
    const closestListEntry = index.ContemberEditor.closest(editor, {
      match: (node) => Slate.Element.isElement(node) && node.type === elementType
    });
    if (!closestListEntry) {
      return;
    }
    return Slate.Editor.withoutNormalizing(editor, () => {
      const [closestList, closestListPath] = closestListEntry;
      for (let i = closestList.children.length - 1; i >= 0; i--) {
        const currentItemPath = [...closestListPath, i];
        if (Slate.Editor.hasInlines(editor, closestList.children[i])) {
          Slate.Transforms.wrapNodes(editor, editor.createDefaultElement([]), {
            at: currentItemPath
          });
          Slate.Transforms.unwrapNodes(editor, {
            at: [...currentItemPath, 0]
          });
        } else {
          Slate.Transforms.unwrapNodes(editor, { at: currentItemPath });
        }
      }
      Slate.Transforms.unwrapNodes(editor, { at: closestListPath });
    });
  }
  if (editor.isElementActive(elementType) || editor.isElementActive(otherKindOfList)) {
    const closestListEntry = index.ContemberEditor.closest(editor, {
      match: (node) => ListElement.isListElement(node)
    });
    if (!closestListEntry) {
      return;
    }
    return Slate.Editor.withoutNormalizing(editor, () => {
      const [, closestListPath] = closestListEntry;
      index.ContemberEditor.ejectElement(editor, closestListPath);
      Slate.Transforms.setNodes(editor, { ...suchThat, type: elementType }, { at: closestListPath });
    });
  }
  const selection = editor.selection;
  if (!selection) {
    return;
  }
  const closestViableParentEntry = index.ContemberEditor.closestViableBlockContainerEntry(editor);
  if (!closestViableParentEntry) {
    return;
  }
  Slate.Editor.withoutNormalizing(editor, () => {
    const [targetParent, targetParentPath] = closestViableParentEntry;
    if (Slate.Element.isElement(targetParent) && Slate.Editor.hasInlines(editor, targetParent)) {
      const listItemPath = [...targetParentPath, 0];
      Slate.Transforms.insertNodes(editor, { type: ListItemElement.listItemElementType, children: [] }, { at: listItemPath });
      Slate.Transforms.moveNodes(editor, {
        to: [...listItemPath, 0],
        match: (node) => Slate.Text.isText(node) || Slate.Editor.isInline(editor, node),
        at: {
          anchor: Slate.Editor.start(editor, [...targetParentPath, 1]),
          focus: Slate.Editor.end(editor, [...targetParentPath, targetParent.children.length])
        }
      });
      Slate.Transforms.wrapNodes(editor, { type: elementType, children: [] }, { at: listItemPath });
    } else {
      const [selectionStart, selectionEnd] = Slate.Editor.edges(editor, selection);
      const relativeStartIndex = selectionStart.path[targetParentPath.length];
      const relativeEndIndex = selectionEnd.path[targetParentPath.length];
      for (let i = relativeStartIndex; i <= relativeEndIndex; i++) {
        Slate.Transforms.wrapNodes(editor, { type: ListItemElement.listItemElementType, children: [] }, { at: [...targetParentPath, i] });
      }
      const emptyList = {
        ...suchThat,
        type: elementType,
        children: []
      };
      const listPath = [...targetParentPath, relativeStartIndex];
      Slate.Transforms.insertNodes(editor, emptyList, { at: listPath });
      for (let i = relativeStartIndex; i <= relativeEndIndex; i++) {
        Slate.Transforms.moveNodes(editor, {
          to: [...listPath, i - relativeStartIndex],
          // The path doesn't depend on i because we keep moving the siblings away.
          at: [...targetParentPath, relativeStartIndex + 1]
        });
      }
    }
  });
};
exports.toggleListElement = toggleListElement;
//# sourceMappingURL=toggleListElement.cjs.map
