const GHOST_ITEM_KEY = "__vc_ghost_item__";
function alignScrollTop(scrollTop, scrollRange) {
  if (scrollTop < 0)
    return 0;
  if (scrollTop >= scrollRange)
    return scrollRange;
  return scrollTop;
}
function getNodeHeight(node) {
  return node ? node.offsetHeight : 0;
}
function getItemAbsoluteTop({ scrollTop, ...rest }) {
  return scrollTop + getItemRelativeTop(rest);
}
function getItemRelativeTop({
  itemIndex,
  itemOffsetPtg,
  itemElementHeights,
  scrollPtg,
  clientHeight,
  getItemKey
}) {
  const locatedItemHeight = itemElementHeights[getItemKey(itemIndex)] || 0;
  const locatedItemTop = scrollPtg * clientHeight;
  const locatedItemOffset = itemOffsetPtg * locatedItemHeight;
  return Math.floor(locatedItemTop - locatedItemOffset);
}
function getCompareItemRelativeTop({
  locatedItemRelativeTop,
  locatedItemIndex,
  compareItemIndex,
  startIndex,
  endIndex,
  getItemKey,
  itemElementHeights
}) {
  let originCompareItemTop = locatedItemRelativeTop;
  const compareItemKey = getItemKey(compareItemIndex);
  if (compareItemIndex <= locatedItemIndex) {
    for (let index = locatedItemIndex; index >= startIndex; index -= 1) {
      const key = getItemKey(index);
      if (key === compareItemKey)
        break;
      const prevItemKey = getItemKey(index - 1);
      originCompareItemTop -= itemElementHeights[prevItemKey] || 0;
    }
  } else {
    for (let index = locatedItemIndex; index <= endIndex; index += 1) {
      const key = getItemKey(index);
      if (key === compareItemKey)
        break;
      originCompareItemTop += itemElementHeights[key] || 0;
    }
  }
  return originCompareItemTop;
}
function getScrollPercentage({ scrollTop, scrollHeight, clientHeight }) {
  if (scrollHeight <= clientHeight)
    return 0;
  const scrollRange = scrollHeight - clientHeight;
  const alignedScrollTop = alignScrollTop(scrollTop, scrollRange);
  const scrollTopPtg = alignedScrollTop / scrollRange;
  return scrollTopPtg;
}
function getElementScrollPercentage(element) {
  if (!element)
    return 0;
  return getScrollPercentage(element);
}
function getLocationItem(scrollPtg, total) {
  const itemIndex = Math.floor(scrollPtg * total);
  const itemTopPtg = itemIndex / total;
  const itemBottomPtg = (itemIndex + 1) / total;
  const itemOffsetPtg = (scrollPtg - itemTopPtg) / (itemBottomPtg - itemTopPtg);
  return {
    index: itemIndex,
    offsetPtg: itemOffsetPtg
  };
}
function getRangeIndex(scrollPtg, itemCount, visibleCount) {
  const { index, offsetPtg } = getLocationItem(scrollPtg, itemCount);
  const beforeCount = Math.ceil(scrollPtg * visibleCount);
  const 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 requireVirtual(height, itemHeight, count, virtual) {
  return virtual !== false && typeof height === "number" && count * itemHeight > height;
}

export { GHOST_ITEM_KEY, alignScrollTop, getCompareItemRelativeTop, getElementScrollPercentage, getItemAbsoluteTop, getItemRelativeTop, getNodeHeight, getRangeIndex, getScrollPercentage, requireVirtual };
//# sourceMappingURL=itemUtil.mjs.map
