"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.normalizeListItem = void 0;
var _slate = require("slate");
var _core = require("../../../core");
var _moveListItemUp = require("./move-list-item-up");
var _constant = require("../constant");
var _elementTypes = require("../../../constants/element-types");
const normalizeListItem = (editor, _ref) => {
  let {
    listItem
  } = _ref;
  let changed = false;

  // patch old version
  const validLiChildrenTypes = [..._constant.LIST_TYPES, _elementTypes.PARAGRAPH, _elementTypes.CODE_BLOCK, _elementTypes.BLOCKQUOTE];
  const [, liPath] = listItem;
  const liChildren = (0, _core.getChildren)(listItem);
  const invalidLiChildrenPathRefs = liChildren.filter(_ref2 => {
    let [child] = _ref2;
    return !validLiChildrenTypes.includes(child.type);
  }).map(_ref3 => {
    let [, childPath] = _ref3;
    return _slate.Editor.pathRef(editor, childPath);
  });
  const firstLiChild = liChildren[0];
  const [firstLiChildNode, firstLiChildPath] = firstLiChild !== null && firstLiChild !== void 0 ? firstLiChild : [];
  if (!firstLiChild || !_slate.Editor.isBlock(editor, firstLiChildNode)) {
    const emptyLic = (0, _core.generateElement)(_elementTypes.PARAGRAPH);
    _slate.Transforms.insertNodes(editor, emptyLic, {
      at: liPath.concat([0])
    });
    return true;
  }
  if (_slate.Editor.isBlock(editor, firstLiChildNode) && !(0, _core.match)(firstLiChildNode, [], {
    type: [_elementTypes.PARAGRAPH]
  })) {
    if ((0, _core.match)(firstLiChildNode, [], {
      type: _constant.LIST_TYPES
    })) {
      const parent = _slate.Editor.parent(editor, listItem[1]);
      const subList = firstLiChild;
      const children = (0, _core.getChildren)(firstLiChild).reverse();
      children.forEach(c => {
        (0, _moveListItemUp.movedListItemUp)(editor, {
          list: subList,
          listItem: c
        });
      });
      _slate.Transforms.removeNodes(editor, {
        at: [...parent[1], 0]
      });
      return true;
    }
    if (validLiChildrenTypes.includes(firstLiChildNode.type)) {
      return true;
    }
    _slate.Transforms.setNodes(editor, {
      type: _elementTypes.PARAGRAPH
    }, {
      at: firstLiChildPath
    });
    changed = true;
  }
  const licChildren = (0, _core.getChildren)(firstLiChild);
  if (licChildren.length) {
    var _licChildren;
    const blockPathRefs = [];
    const inlineChildren = [];
    for (const licChild of licChildren) {
      if (!(_slate.Element.isElement(licChild[0]) && _slate.Editor.isBlock(editor, licChild[0]))) {
        break;
      }
      blockPathRefs.push(_slate.Editor.pathRef(editor, licChild[1]));
      inlineChildren.push(...(0, _core.getDeepInlineChildren)(editor, {
        children: (0, _core.getChildren)(licChild)
      }));
    }
    const to = _slate.Path.next((_licChildren = licChildren[licChildren.length - 1]) === null || _licChildren === void 0 ? void 0 : _licChildren[1]);
    inlineChildren.reverse().forEach(_ref4 => {
      let [, path] = _ref4;
      _slate.Transforms.moveNodes(editor, {
        at: path,
        to
      });
    });
    blockPathRefs.forEach(pathRef => {
      const path = pathRef.unref();
      path && _slate.Transforms.removeNodes(editor, {
        at: path
      });
    });
    if (blockPathRefs.length) {
      changed = true;
    }
  }
  if (changed) return true;
  invalidLiChildrenPathRefs.reverse().forEach(ref => {
    const path = ref.unref();
    path && _slate.Transforms.moveNodes(editor, {
      at: path,
      to: firstLiChildPath.concat([0])
    });
  });
  return !!invalidLiChildrenPathRefs.length;
};
exports.normalizeListItem = normalizeListItem;