export const VIRTUAL_THREAD_ROW_HEIGHT = 48;
export const VIRTUAL_THREAD_OVERSCAN = 8;
export const VIRTUAL_THREAD_THRESHOLD = 80;

export function shouldVirtualizeThreadRows(count) {
  return count > VIRTUAL_THREAD_THRESHOLD;
}

export function virtualThreadRange({
  count,
  scrollTop = 0,
  viewportHeight = 0,
  rowHeight = VIRTUAL_THREAD_ROW_HEIGHT,
  overscan = VIRTUAL_THREAD_OVERSCAN
}) {
  const safeCount = Math.max(0, Math.floor(count));
  const safeRowHeight = Math.max(1, rowHeight);
  const safeViewportHeight = Math.max(0, viewportHeight);
  const totalHeight = safeCount * safeRowHeight;
  const maxScrollTop = Math.max(0, totalHeight - safeViewportHeight);
  const boundedScrollTop = Math.min(Math.max(0, scrollTop), maxScrollTop);
  const firstVisible = Math.floor(boundedScrollTop / safeRowHeight);
  const visibleCount = Math.max(1, Math.ceil(safeViewportHeight / safeRowHeight));
  const start = Math.max(0, firstVisible - overscan);
  const end = Math.min(safeCount, firstVisible + visibleCount + overscan);

  return { start, end, offsetTop: start * safeRowHeight, totalHeight };
}
