"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.movedListItemUp = void 0;
var _slate = require("slate");
var _core = require("../../../core");
var _queries = require("../queries");
var _moveListItemsToList = require("./move-list-items-to-list");
var _unwrapList = require("./unwrap-list");
var _elementTypes = require("../../../constants/element-types");
const movedListItemUp = (editor, _ref) => {
  let {
    list,
    listItem
  } = _ref;
  const move = () => {
    const [listNode, listPath] = list;
    const [liNode, liPath] = listItem;

    // Get above node of list item
    const liParent = (0, _core.getAboveNode)(editor, {
      at: listPath,
      match: {
        type: [_elementTypes.LIST_ITEM]
      }
    });

    // Current list is the top hierarchy list, list[ol] > 'li', since there is no parent.
    if (!liParent) {
      let toListPath = null;
      try {
        toListPath = _slate.Path.next(listPath);
      } catch (err) {
        return;
      }
      const isHasLiChild = (0, _queries.hasListChild)(liNode);
      const isNotLastChild = !(0, _core.isLastChild)(list, liPath);
      if (isHasLiChild || isNotLastChild) {
        // Create a new sibling node
        const list = (0, _core.generateElement)(listNode.type, {
          childrenOrText: []
        });
        _slate.Transforms.insertNodes(editor, list, {
          at: toListPath
        });
      }

      // If contains child nodes, move the child nodes to the newly created node
      if (isHasLiChild) {
        const toListNode = (0, _core.getNode)(editor, toListPath);
        if (!toListNode) return;
        (0, _moveListItemsToList.moveListItemsToList)(editor, {
          formListItem: listItem,
          toList: [toListNode, toListPath]
        });
      }

      // If not last child, move the next siblings to the newly created node
      if (isNotLastChild) {
        const toListNode = (0, _core.getNode)(editor, toListPath);
        if (!toListNode) return;
        (0, _moveListItemsToList.moveListItemsToList)(editor, {
          fromList: list,
          fromStartIndex: liPath[liPath.length - 1] + 1,
          toList: [toListNode, toListPath],
          deleteFromList: false
        });
      }

      // unwrap the list
      (0, _unwrapList.unwrapList)(editor, {
        at: liPath.concat(0)
      });
      return true;
    }

    // If parent exists, the current list is a child list, li > list[ol] > 'li'.
    const [, liParentPath] = liParent;
    // is last: false
    // li > list[ol] > li
    //               > li
    const toListPath = liPath.concat([1]);
    if (!(0, _core.isLastChild)(list, liPath)) {
      // Here is a trick.
      // First, move the sibling node of the current node to the child node of the current node "ol".
      // Second, move the child node of the current node to the sibling node of the current node.
      if (!(0, _queries.hasListChild)(liNode)) {
        const list = (0, _core.generateElement)(listNode.type, {
          childrenOrText: []
        });
        _slate.Transforms.insertNodes(editor, list, {
          at: toListPath
        });
      }
      const toListNode = (0, _core.getNode)(editor, toListPath);
      if (!toListNode) return;

      // Move next siblings to li sublist
      (0, _moveListItemsToList.moveListItemsToList)(editor, {
        fromListItem: liParent,
        toList: [toListNode, toListPath],
        fromStartIndex: liPath[liPath.length - 1] + 1,
        deleteFromList: false
      });
    }

    // is last: true
    // li > list[ol] > li
    const movedUpLiPath = _slate.Path.next(liParentPath);
    _slate.Transforms.moveNodes(editor, {
      at: liPath,
      to: movedUpLiPath
    });
    return true;
  };
  let moved = false;
  _slate.Editor.withoutNormalizing(editor, () => {
    moved = move();
  });
  return moved;
};
exports.movedListItemUp = movedListItemUp;