/** Page containing the vertical center of the canvas viewport (0-based). */
export function resolveCurrentPageInViewport(
  stackTopInViewport: number,
  viewportHeight: number,
  pageStride: number,
  pageCount: number
): number {
  if (pageCount <= 1 || pageStride <= 0 || viewportHeight <= 0) return 0
  const centerInStack = viewportHeight / 2 - stackTopInViewport
  const index = Math.floor(centerInStack / pageStride)
  return Math.min(pageCount - 1, Math.max(0, index))
}

/** @deprecated Use resolveCurrentPageInViewport — kept for tests comparing old behaviour. */
export function resolveCurrentPageAtViewportTop(
  stackTopInViewport: number,
  pageStride: number,
  pageCount: number
): number {
  if (pageCount <= 1 || pageStride <= 0) return 0
  if (stackTopInViewport >= 0) return 0
  const index = Math.floor(-stackTopInViewport / pageStride)
  return Math.min(pageCount - 1, Math.max(0, index))
}

export function computePageTopInViewport(
  stackTopInViewport: number,
  currentPage: number,
  pageStride: number
): number {
  return stackTopInViewport + currentPage * pageStride
}
