export const DEFAULT_PAGE_SIZE = 20;
/**
 * Paginates an array of results.
 * @param results Full array of results
 * @param page Requests page number (1-indexed)
 * @param limit Items per page (default: 20)
 * @returns Sliced results and metadata
 */
export function paginateResults(results, page, limit = DEFAULT_PAGE_SIZE) {
    const total = results.length;
    // Calculate total pages properly
    // Edge case: if total is 0, totalPages should probably be 0 or 1 depending on convention.
    // Test expectation was: empty results -> totalPages: 0
    const totalPages = total === 0 ? 0 : Math.ceil(total / limit);
    // Sanitize page number
    const currentPage = Math.max(1, page);
    // Calculate start and end indices
    const startIndex = (currentPage - 1) * limit;
    // If startIndex is beyond array, slice returns empty, which is correct
    const slice = results.slice(startIndex, startIndex + limit);
    return {
        results: slice,
        meta: {
            total,
            page: currentPage,
            totalPages: totalPages
        }
    };
}
