import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
import { Fragment } from '@atlaskit/editor-prosemirror/model';
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
var listDepth = 3;
export var selectionCoversAllListItems = function selectionCoversAllListItems($from, $to) {
  // Block level lists
  var listParents = ['bulletList', 'orderedList'];
  if ($from.depth >= listDepth && $to.depth >= listDepth && $from.depth === $to.depth) {
    var _greatGrandparentFrom, _greatGrandparentFrom2;
    // Get grandparents (from)
    var grandparentFrom = $from.node($from.depth - 1);
    var greatGrandparentFrom = $from.node($from.depth - 2);
    // Get grandparents (to)
    var grandparentTo = $to.node($from.depth - 1);
    var greatGrandparentTo = $to.node($from.depth - 2);
    if (greatGrandparentTo.eq(greatGrandparentFrom) && listParents.includes(greatGrandparentFrom.type.name) && // Selection covers entire list
    (_greatGrandparentFrom = greatGrandparentFrom.firstChild) !== null && _greatGrandparentFrom !== void 0 && _greatGrandparentFrom.eq(grandparentFrom) && (_greatGrandparentFrom2 = greatGrandparentFrom.lastChild) !== null && _greatGrandparentFrom2 !== void 0 && _greatGrandparentFrom2.eq(grandparentTo)) {
      return true;
    }
  }
  return false;
};

/**
 * Get the slice of the document corresponding to the selection.
 * This is similar to the prosemirror `selection.content()` - but
 * does not include the parents (unless the result is inline)
 *
 * @param selection The selection to get the slice for.
 * @returns The slice of the document corresponding to the selection.
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var getSliceFromSelection = function getSliceFromSelection(selection) {
  var from = selection.from,
    to = selection.to;
  if (from === to) {
    return Fragment.empty;
  }
  var frag = Fragment.empty;
  var sortedRanges = _toConsumableArray(selection.ranges.slice()).sort(function (a, b) {
    return a.$from.pos - b.$from.pos;
  });
  var _iterator = _createForOfIteratorHelper(sortedRanges),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var range = _step.value;
      var $from = range.$from,
        $to = range.$to;
      var _to = $to.pos;
      var depth =
      // If we're in a text selection, and share the parent node across the anchor->head
      // make the depth the parent node
      selection instanceof TextSelection && $from.parent.eq($to.parent) ? Math.max(0, $from.sharedDepth(_to) - 1) : $from.sharedDepth(_to);
      var finalDepth = depth;
      // For block-level lists (non-nested) specifically use the selection
      if (selectionCoversAllListItems($from, $to)) {
        finalDepth = $from.depth - listDepth;
      }
      var start = $from.start(finalDepth);
      var node = $from.node(finalDepth);
      var content = node.content.cut($from.pos - start, $to.pos - start);
      frag = frag.append(content);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return frag;
};
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { getFragmentsFromSelection } from './getFragmentsFromSelection';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { getLocalIdsFromSelection } from './getLocalIdsFromSelection';