"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 OrderedListElement = require("./OrderedListElement.cjs");
const UnorderedListElement = require("./UnorderedListElement.cjs");
const ListElement = require("./ListElement.cjs");
const ListHtmlDeserializer = require("./ListHtmlDeserializer.cjs");
const dedentListItem = require("./transforms/dedentListItem.cjs");
const indentListItem = require("./transforms/indentListItem.cjs");
const withLists = (editor) => {
  const {
    insertBreak,
    onKeyDown
  } = editor;
  editor.registerElement(ListItemElement.listItemElementPlugin);
  editor.registerElement(OrderedListElement.orderedListElementPlugin);
  editor.registerElement(UnorderedListElement.unorderedListElementPlugin);
  editor.htmlDeserializer.registerPlugin(ListHtmlDeserializer.listHtmlDeserializerFactory());
  Object.assign(editor, {
    insertBreak: () => {
      const { selection } = editor;
      if (!selection || !Slate.Range.isCollapsed(selection)) {
        return insertBreak();
      }
      const closestBlockEntry = index.ContemberEditor.closestBlockEntry(editor);
      if (closestBlockEntry === void 0) {
        return insertBreak();
      }
      const [closestBlock, closestBlockPath] = closestBlockEntry;
      let containingListItem;
      let containingListItemPath;
      if (!ListItemElement.isListItemElement(closestBlock)) {
        if (closestBlockPath.length < 2) {
          return insertBreak();
        }
        containingListItemPath = Slate.Path.parent(closestBlockPath);
        const closestListItem = Slate.Node.get(editor, containingListItemPath);
        if (!ListItemElement.isListItemElement(closestListItem)) {
          return insertBreak();
        }
        containingListItem = closestListItem;
      } else {
        containingListItem = closestBlock;
        containingListItemPath = closestBlockPath;
      }
      if (Slate.Node.string(containingListItem) !== "") {
        return Slate.Transforms.splitNodes(editor, {
          always: true,
          at: selection.focus,
          match: (node) => ListItemElement.isListItemElement(node)
        });
      }
      const containingListPath = Slate.Path.parent(containingListItemPath);
      const containingList = Slate.Node.get(editor, containingListPath);
      if (!ListElement.isListElement(containingList)) {
        return insertBreak();
      }
      const followingListItemPath = Slate.Path.next(containingListItemPath);
      const hasFollowingListItem = Slate.Node.has(editor, followingListItemPath);
      if (hasFollowingListItem) {
        const followingListItemNode = Slate.Node.get(editor, followingListItemPath);
        if (!ListItemElement.isListItemElement(followingListItemNode)) {
          return insertBreak();
        }
        if (containingListItemPath[containingListItemPath.length - 1] === 0) {
          Slate.Editor.withoutNormalizing(editor, () => {
            Slate.Transforms.removeNodes(editor, { at: containingListItemPath });
            Slate.Transforms.insertNodes(editor, editor.createDefaultElement([{ text: "" }]), { at: containingListPath, select: true });
          });
        } else {
          Slate.Editor.withoutNormalizing(editor, () => {
            Slate.Transforms.removeNodes(editor, { at: containingListItemPath });
            Slate.Transforms.splitNodes(editor, { at: containingListItemPath });
            const afterListParent = Slate.Path.next(containingListPath);
            Slate.Transforms.insertNodes(editor, editor.createDefaultElement([{ text: "" }]), { at: afterListParent, select: true });
          });
        }
      } else {
        Slate.Editor.withoutNormalizing(editor, () => {
          const afterListParent = Slate.Path.next(containingListPath);
          Slate.Transforms.removeNodes(editor, { at: containingListItemPath });
          Slate.Transforms.insertNodes(editor, editor.createDefaultElement([{ text: "" }]), { at: afterListParent, select: true });
        });
      }
    },
    onKeyDown: (event) => {
      if (event.key !== "Tab" && event.key !== "Enter" || !editor.selection || !Slate.Range.isCollapsed(editor.selection)) {
        return onKeyDown(event);
      }
      const selection = editor.selection;
      const closestBlockEntry = index.ContemberEditor.closestBlockEntry(editor, { at: selection.focus });
      if (closestBlockEntry === void 0) {
        return onKeyDown(event);
      }
      let [closestBlockElement, closestBlockPath] = closestBlockEntry;
      if (!Slate.Element.isElement(closestBlockElement)) {
        return onKeyDown(event);
      }
      if (event.key === "Tab") {
        if (editor.isDefaultElement(closestBlockElement)) {
          [closestBlockElement, closestBlockPath] = Slate.Editor.parent(editor, closestBlockPath);
        }
        if (!ListItemElement.isListItemElement(closestBlockElement)) {
          return onKeyDown(event);
        }
        const succeeded = event.shiftKey ? dedentListItem.dedentListItem(editor, closestBlockElement, closestBlockPath) : indentListItem.indentListItem(editor, closestBlockElement, closestBlockPath);
        if (succeeded) {
          return event.preventDefault();
        }
      } else if (event.key === "Enter" && event.shiftKey) {
        if (editor.isDefaultElement(closestBlockElement)) {
          const [listItem] = Slate.Editor.parent(editor, closestBlockPath);
          if (!ListItemElement.isListItemElement(listItem)) {
            return onKeyDown(event);
          }
          event.preventDefault();
          return Slate.Transforms.splitNodes(editor, {
            always: true,
            at: selection.focus,
            match: (node) => Slate.Editor.isBlock(editor, node) && editor.isDefaultElement(node)
          });
        } else if (ListItemElement.isListItemElement(closestBlockElement)) {
          const [listItemStart, listItemEnd] = Slate.Editor.edges(editor, closestBlockPath);
          event.preventDefault();
          return Slate.Editor.withoutNormalizing(editor, () => {
            Slate.Transforms.wrapNodes(editor, editor.createDefaultElement([]), {
              match: (node) => Slate.Text.isText(node) || Slate.Element.isElement(node) && editor.isInline(node),
              at: {
                anchor: listItemStart,
                focus: listItemEnd
              }
            });
            const relative = Slate.Path.relative(selection.focus.path, closestBlockPath);
            Slate.Transforms.splitNodes(editor, {
              // The zero should be the newly created default element.
              at: {
                path: [...closestBlockPath, 0, ...relative],
                offset: selection.focus.offset
              },
              always: true
            });
          });
        }
      }
      return onKeyDown(event);
    }
  });
  return editor;
};
exports.withLists = withLists;
//# sourceMappingURL=withLists.cjs.map
