import { Editor, Transforms, Element } from "slate";
import { ContemberEditor } from "../../../ContemberEditor/index.js";
import { ListItemRenderer } from "./ListItemRenderer.js";
import { isListElement } from "./ListElement.js";
const listItemElementType = "listItem";
const isListItemElement = (element, suchThat) => ContemberEditor.isElementType(element, listItemElementType, suchThat);
const listItemElementPlugin = {
  type: listItemElementType,
  render: ListItemRenderer,
  canContainAnyBlocks: true,
  normalizeNode: ({ editor, path, element, preventDefault }) => {
    const parentEntry = Editor.above(editor, { at: path });
    if (parentEntry === void 0 || !isListElement(parentEntry[0])) {
      Editor.withoutNormalizing(editor, () => {
        const defaultElement = editor.createDefaultElement([{ text: "" }]);
        Transforms.wrapNodes(editor, defaultElement, {
          at: path
        });
        Transforms.unwrapNodes(editor, {
          at: [...path, 0]
        });
      });
      return preventDefault();
    }
    if (element.children.every((it) => isListItemElement(it))) {
      Transforms.unwrapNodes(editor, {
        at: path
      });
      return preventDefault();
    }
    if (element.children.length === 1) {
      const onlyChild = element.children[0];
      if (Element.isElement(onlyChild) && editor.isDefaultElement(onlyChild)) {
        Transforms.unwrapNodes(editor, {
          at: [...path, 0]
        });
        return preventDefault();
      }
    }
    const firstChild = element.children[0];
    if (Element.isElement(firstChild) && isListElement(firstChild)) {
      Transforms.insertNodes(editor, editor.createDefaultElement([{ text: "" }]), {
        at: [...path, 0]
      });
      return preventDefault();
    }
  }
};
export {
  isListItemElement,
  listItemElementPlugin,
  listItemElementType
};
//# sourceMappingURL=ListItemElement.js.map
