"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const ReactDOM = require("react-dom");
const getStringLength = require("../../../_util/getStringLength.js");
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var GHOST_ITEM_KEY = "__rc_ghost_item__";
function getLocationItem(scrollPtg, total) {
  var itemIndex = Math.floor(scrollPtg * total);
  var itemTopPtg = itemIndex / total;
  var offsetPtg = (scrollPtg - itemTopPtg) / (1 / total);
  return {
    index: itemIndex,
    offsetPtg: Number.isNaN(offsetPtg) ? 0 : offsetPtg
  };
}
function getNodeHeight(node) {
  var element = ReactDOM.findDOMNode(node);
  return element ? element.offsetHeight : 0;
}
function getValidScrollTop(scrollTop, scrollRange) {
  return scrollTop < 0 ? 0 : scrollTop > scrollRange ? scrollRange : scrollTop;
}
function getScrollPercentage(_a) {
  var scrollTop = _a.scrollTop, scrollHeight = _a.scrollHeight, clientHeight = _a.clientHeight;
  var scrollRange = scrollHeight - clientHeight;
  return scrollRange <= 0 ? 0 : getValidScrollTop(scrollTop, scrollRange) / scrollRange;
}
function getRangeIndex(scrollPtg, itemCount, visibleCount) {
  var _a = getLocationItem(scrollPtg, itemCount), index = _a.index, offsetPtg = _a.offsetPtg;
  var beforeCount = Math.ceil(scrollPtg * visibleCount);
  var afterCount = Math.ceil((1 - scrollPtg) * visibleCount);
  return {
    itemIndex: index,
    itemOffsetPtg: offsetPtg,
    startIndex: Math.max(0, index - beforeCount),
    endIndex: Math.min(itemCount - 1, index + afterCount)
  };
}
function getItemRelativeTop(_a) {
  var itemHeight = _a.itemHeight, itemOffsetPtg = _a.itemOffsetPtg, scrollPtg = _a.scrollPtg, clientHeight = _a.clientHeight;
  return Math.floor(clientHeight * scrollPtg - itemHeight * itemOffsetPtg);
}
function getItemAbsoluteTop(_a) {
  var scrollTop = _a.scrollTop, rest = __rest(_a, ["scrollTop"]);
  return scrollTop + getItemRelativeTop(rest);
}
function getCompareItemRelativeTop(_a) {
  var locatedItemRelativeTop = _a.locatedItemRelativeTop, locatedItemIndex = _a.locatedItemIndex, compareItemIndex = _a.compareItemIndex, startIndex = _a.startIndex, endIndex = _a.endIndex, getItemKey = _a.getItemKey, itemElementHeights = _a.itemElementHeights, itemHeight = _a.itemHeight;
  var compareItemTop = locatedItemRelativeTop;
  var compareItemKey = getItemKey(compareItemIndex);
  if (compareItemIndex <= locatedItemIndex) {
    for (var index = locatedItemIndex; index >= startIndex; index -= 1) {
      var key = getItemKey(index);
      if (key === compareItemKey) {
        break;
      }
      var prevItemKey = getItemKey(index - 1);
      compareItemTop -= itemElementHeights[prevItemKey] || itemHeight;
    }
  } else {
    for (var index = locatedItemIndex; index <= endIndex; index += 1) {
      var key = getItemKey(index);
      if (key === compareItemKey) {
        break;
      }
      compareItemTop += itemElementHeights[key] || itemHeight;
    }
  }
  return compareItemTop;
}
function getLongestItemIndex(data) {
  var result = -1;
  var length = 0;
  data.forEach(function(item, index) {
    var _a;
    item = typeof item === "string" ? item : (_a = item.props) === null || _a === void 0 ? void 0 : _a.children;
    if (typeof item === "string") {
      var _length = getStringLength(item);
      if (_length > length) {
        length = _length;
        result = index;
      }
    }
  });
  return result;
}
exports.GHOST_ITEM_KEY = GHOST_ITEM_KEY;
exports.getCompareItemRelativeTop = getCompareItemRelativeTop;
exports.getItemAbsoluteTop = getItemAbsoluteTop;
exports.getItemRelativeTop = getItemRelativeTop;
exports.getLocationItem = getLocationItem;
exports.getLongestItemIndex = getLongestItemIndex;
exports.getNodeHeight = getNodeHeight;
exports.getRangeIndex = getRangeIndex;
exports.getScrollPercentage = getScrollPercentage;
exports.getValidScrollTop = getValidScrollTop;
//# sourceMappingURL=itemUtil.js.map
