"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.normalizeList = void 0;
var _slate = require("slate");
var _transforms = require("../transforms");
var _elementTypes = require("../../../constants/element-types");
var _core = require("../../../core");
var _constant = require("../constant");
const normalizeList = editor => {
  const {
    normalizeNode
  } = editor;
  return _ref => {
    let [node, path] = _ref;
    if (!_slate.Element.isElement(node)) {
      return normalizeNode([node, path]);
    }

    // root
    if (_constant.LIST_TYPES.includes(node.type)) {
      const children = (0, _core.getChildren)([node, path]);
      const nonLiChild = children.find(_ref2 => {
        let [child] = _ref2;
        return child.type !== _elementTypes.LIST_ITEM;
      });
      if (nonLiChild) {
        const listItem = (0, _core.generateElement)(_elementTypes.LIST_ITEM, {
          childrenOrText: []
        });
        _slate.Transforms.wrapNodes(editor, listItem, {
          at: nonLiChild[1]
        });
        return;
      }
    }
    if ((0, _core.match)(node, [], {
      type: _constant.LIST_TYPES
    })) {
      if (!node.children.length || !node.children.find(item => item.type === _elementTypes.LIST_ITEM)) {
        _slate.Transforms.removeNodes(editor, {
          at: path
        });
        return;
      }
      const nextPath = _slate.Path.next(path);
      const nextNode = (0, _core.getNode)(editor, nextPath);
      if ((nextNode === null || nextNode === void 0 ? void 0 : nextNode.type) === node.type) {
        (0, _transforms.moveListItemsToList)(editor, {
          fromList: [nextNode, nextPath],
          toList: [node, path],
          deleteFromList: true
        });
      }
      const prevPath = (0, _core.getPreviousPath)(path);
      const prevNode = (0, _core.getNode)(editor, prevPath);
      if ((prevNode === null || prevNode === void 0 ? void 0 : prevNode.type) === node.type) {
        editor.normalizeNode([prevNode, prevPath]);
        return;
      }
      if ((0, _transforms.normalizeNestedList)(editor, {
        nestedListItem: [node, path]
      })) {
        return;
      }
    }
    if (node.type === _elementTypes.LIST_ITEM) {
      if ((0, _transforms.normalizeListItem)(editor, {
        listItem: [node, path]
      })) {
        return;
      }
    }
    normalizeNode([node, path]);
  };
};
exports.normalizeList = normalizeList;