"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.unwrapList = void 0;
var _slate = require("slate");
var _core = require("../../../core");
var _constant = require("../constant");
var _elementTypes = require("../../../constants/element-types");
const unwrapList = function (editor) {
  let {
    at
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  const ancestorListTypeCheck = () => {
    if ((0, _core.getAboveNode)(editor, {
      match: {
        type: _constant.LIST_TYPES
      }
    })) {
      return true;
    }

    // The selection's common node might be a list type
    if (!at && editor.selection) {
      const commonNode = _slate.Node.common(editor, editor.selection.anchor.path, editor.selection.focus.path);
      if (_slate.Element.isElement(commonNode[0]) && _constant.LIST_TYPES.includes(commonNode[0].type)) {
        return true;
      }
    }
    return false;
  };
  _slate.Editor.withoutNormalizing(editor, () => {
    do {
      _slate.Transforms.unwrapNodes(editor, {
        at,
        match: n => (0, _core.getNodeType)(n) === _elementTypes.LIST_ITEM,
        split: true
      });
      _slate.Transforms.unwrapNodes(editor, {
        at,
        match: n => _constant.LIST_TYPES.includes((0, _core.getNodeType)(n)),
        split: true
      });
    } while (ancestorListTypeCheck());
  });
};
exports.unwrapList = unwrapList;