/** Shared list pagination defaults (page size matches the ref-frontend grids). */
export const DEFAULT_PAGE_SIZE = 10;
export const MAX_PAGE_SIZE = 100;

/**
 * Resolves a `{ limit, offset }` for a list query from raw `limit`/`page` query
 * values. Falls back to {@link DEFAULT_PAGE_SIZE} and caps at
 * {@link MAX_PAGE_SIZE}. `page` is zero-based.
 */
export function resolvePagination(
  rawLimit?: unknown,
  rawPage?: unknown,
): { limit: number; offset: number } {
  const parsedLimit = Number(rawLimit);
  const limit =
    Number.isFinite(parsedLimit) && parsedLimit > 0
      ? Math.min(Math.floor(parsedLimit), MAX_PAGE_SIZE)
      : DEFAULT_PAGE_SIZE;

  const parsedPage = Number(rawPage);
  const page =
    Number.isFinite(parsedPage) && parsedPage > 0 ? Math.floor(parsedPage) : 0;

  return { limit, offset: page * limit };
}
