"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MAX_NESTED_LIST_INDENTATION = void 0;
exports.indentList = indentList;

var _indentation = require("../utils/indentation");

var _indentListItemsSelected = require("../actions/indent-list-items-selected");

var _find = require("../utils/find");

var _selection = require("../utils/selection");

// import { Command } from '../../../types';
var MAX_NESTED_LIST_INDENTATION = 6;
exports.MAX_NESTED_LIST_INDENTATION = MAX_NESTED_LIST_INDENTATION;

function indentList() {
  return function (state, dispatch) {
    var tr = state.tr,
        $from = state.selection.$from; // don't indent if selection is not inside a list

    if (!(0, _selection.isInsideListItem)(state)) {
      return false;
    }

    var firstListItemSelectedAttributes = (0, _selection.getListItemAttributes)($from);
    var parentListNode = (0, _find.findFirstParentListNode)($from);

    if (!parentListNode || firstListItemSelectedAttributes && firstListItemSelectedAttributes.indentLevel === 0 && firstListItemSelectedAttributes.itemIndex === 0) {
      if ((0, _selection.isInsideTableCell)(state)) {
        // dont consume tab, as table-keymap should move cursor to next cell
        return false;
      } else {
        // Even though this is a non-operation, we don't want to send this event to the browser. Because if we return false, the browser will move the focus to another place
        return true;
      }
    }

    (0, _indentListItemsSelected.indentListItemsSelected)(tr);
    var maximimunNestedLevelReached = !(0, _indentation.hasValidListIndentationLevel)({
      tr: tr,
      maxIndentation: MAX_NESTED_LIST_INDENTATION
    });

    if (maximimunNestedLevelReached || !tr.docChanged) {
      // Even though this is a non-operation, we don't want to send this event to the browser. Because if we return false, the browser will move the focus to another place
      return true;
    }

    if (dispatch) {
      dispatch(tr);
    }

    return true;
  };
}