import type { PaginatedResult } from "./types";

export const DEFAULT_PAGE_SIZE = 20;

/**
 * Builds a PaginatedResult from rows fetched with limit+1 strategy.
 * Caller should fetch `limit + 1` rows — the extra row determines hasNextPage.
 */
export function buildPaginatedResult<T>(
  rows: T[],
  limit: number,
  total?: number,
): PaginatedResult<T> {
  const hasNextPage = rows.length > limit;
  const items = hasNextPage ? rows.slice(0, limit) : rows;

  const result: PaginatedResult<T> = { items, hasNextPage };
  if (total !== undefined) {
    result.total = total;
  }
  return result;
}
