import { Node_Type } from "./../ricos-types/index.js";
import { findParentNodeClosestToPos } from "./../tiptap/index.js";
import { liftTarget } from "./../tiptap/pm/transform.js";
const liftEmptyListItemWithMultipleEmptyParagraphs = (state, dispatch) => {
  if (!dispatch) {
    console.error("dispatch is not defined");
    return false;
  }
  const currListItem = findParentNodeClosestToPos(
    state.selection.$from,
    (node) => node.type.name === Node_Type.LIST_ITEM
  );
  if (!currListItem) {
    return false;
  }
  if (currListItem.node.content.childCount < 2) {
    return false;
  }
  const currParagraph = findParentNodeClosestToPos(
    state.selection.$from,
    (node) => node.type.name === Node_Type.PARAGRAPH
  );
  if (!currParagraph) {
    return false;
  }
  if (currListItem.node.firstChild?.attrs.id !== currParagraph.node.attrs.id) {
    return false;
  }
  let allEmptyParagraphs = true;
  currListItem.node.forEach((node) => {
    if (node.type.name !== Node_Type.PARAGRAPH || node.content.childCount !== 0) {
      allEmptyParagraphs = false;
    }
  });
  if (!allEmptyParagraphs) {
    return false;
  }
  const parentListNode = findParentNodeClosestToPos(
    state.selection.$from,
    (node) => node.type.name === Node_Type.BULLETED_LIST || node.type.name === Node_Type.CHECKBOX_LIST || node.type.name === Node_Type.ORDERED_LIST
  );
  if (!parentListNode) {
    return false;
  }
  if (parentListNode.node.lastChild?.attrs.id !== currListItem.node.attrs.id) {
    return false;
  }
  const tr = state.tr;
  tr.delete(currListItem.pos + 3, currListItem.pos + currListItem.node.nodeSize - 1);
  const { $cursor } = tr.selection;
  if (!$cursor || $cursor.parent.content.size) {
    return false;
  }
  const range = $cursor?.blockRange();
  if (!range || range.parent.type.name !== Node_Type.LIST_ITEM) {
    return false;
  }
  const target = liftTarget(range);
  if (target === null) {
    return false;
  }
  dispatch(tr.lift(range, target).scrollIntoView());
  return true;
};
export {
  liftEmptyListItemWithMultipleEmptyParagraphs
};
//# sourceMappingURL=lift-list-item-with-multiple-empty-paragraphs.js.map
