function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, 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 normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import constant from "./constant";

// 计算可见区域元素
function getVisibleItems(_ref) {
  var prependHeight = _ref.prependHeight,
    items = _ref.items,
    itemHeights = _ref.itemHeights,
    scrollTop = _ref.scrollTop,
    containerHeight = _ref.containerHeight;
  if (!Array.isArray(items) || !items.length) return [];
  // 计算每一项的 top 值和高度
  var top = 0;
  var _iterator = _createForOfIteratorHelper(items.entries()),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var _item$virtualData;
      var _step$value = _slicedToArray(_step.value, 2),
        index = _step$value[0],
        item = _step$value[1];
      var itemHeight = itemHeights[index];
      top = index === 0 ? 0 : items[index - 1].virtualData.top + items[index - 1].virtualData.height;
      item.virtualData = {
        type: ((_item$virtualData = item.virtualData) === null || _item$virtualData === void 0 ? void 0 : _item$virtualData.type) || undefined,
        height: itemHeight,
        top: top,
        index: index
      };
    }

    // 计算可见区域的起始索引
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  var startIndex = 0;
  while (items[startIndex] && items[startIndex].virtualData.top < scrollTop - prependHeight - constant.startBuffer) {
    startIndex++;
  }

  // 计算可见区域的结束索引
  var endIndex = startIndex;
  while (items[endIndex] && items[endIndex].virtualData.top < scrollTop + containerHeight + constant.endBuffer) {
    endIndex++;
  }

  // 渲染可见区域的元素
  var visibleItems = items.slice(startIndex, endIndex);
  return visibleItems;
}
export default getVisibleItems;