"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.moveListItems = void 0;
var _slate = require("slate");
var _core = require("../../../core");
var _queries = require("../queries");
var _moveListItemDown = require("./move-list-item-down");
var _moveListItemUp = require("./move-list-item-up");
var _removeFirstListItem = require("./remove-first-list-item");
var _elementTypes = require("../../../constants/element-types");
const moveListItems = function (editor) {
  let {
    increase = true,
    at = editor.selection,
    enableResetOnShiftTab
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  const _nodes = (0, _core.getNodeEntries)(editor, {
    at,
    match: {
      type: [_elementTypes.PARAGRAPH]
    }
  });
  const lics = Array.from(_nodes);
  if (!lics.length) return;
  const highestLicPaths = [];
  const highestLicPathRefs = [];
  lics.forEach(lic => {
    const licPath = lic[1];
    const liPath = _slate.Path.parent(licPath);
    const isAncestor = highestLicPaths.some(path => {
      const highestLiPath = _slate.Path.parent(path);
      return _slate.Path.isAncestor(highestLiPath, liPath);
    });
    if (!isAncestor) {
      highestLicPaths.push(licPath);
      highestLicPathRefs.push(_slate.Editor.pathRef(editor, licPath));
    }
  });
  const licPathRefsToMove = increase ? highestLicPathRefs : highestLicPathRefs.reverse();
  let moved = false;
  licPathRefsToMove.forEach(licPathRef => {
    const licPath = licPathRef.unref();
    if (!licPath) return;
    const listItem = _slate.Editor.parent(editor, licPath);
    if (!listItem) return;
    const parentList = _slate.Editor.parent(editor, listItem[1]);
    if (!parentList) return;
    let _moved = false;
    if (increase) {
      _moved = (0, _moveListItemDown.movedListItemDown)(editor, {
        list: parentList,
        listItem: listItem
      });
    } else if ((0, _queries.isListNested)(editor, parentList[1])) {
      _moved = (0, _moveListItemUp.movedListItemUp)(editor, {
        list: parentList,
        listItem: listItem
      });
      return _moved;
    } else if (enableResetOnShiftTab) {
      _moved = (0, _removeFirstListItem.removeFirstListItem)(editor, {
        list: parentList,
        listItem: listItem
      });
    }
    moved = _moved || moved;
  });
  return moved;
};
exports.moveListItems = moveListItems;