"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPaginationRowModel = getPaginationRowModel;
const utils_1 = require("../utils");
const getExpandedRowModel_1 = require("./getExpandedRowModel");
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- 保留参数供未来扩展
function getPaginationRowModel(_opts) {
    return (table) => (0, utils_1.memo)(() => [
        table.getState().pagination,
        table.getPrePaginationRowModel(),
        table.options.paginateExpandedRows ? undefined : table.getState().expanded,
    ], (pagination, rowModel) => {
        if (!rowModel.rows.length) {
            return rowModel;
        }
        const { pageSize, pageIndex } = pagination;
        const { flatRows, rowsById } = rowModel;
        let { rows } = rowModel;
        const pageStart = pageSize * pageIndex;
        const pageEnd = pageStart + pageSize;
        rows = rows.slice(pageStart, pageEnd);
        let paginatedRowModel;
        if (!table.options.paginateExpandedRows) {
            paginatedRowModel = (0, getExpandedRowModel_1.expandRows)({
                rows,
                flatRows,
                rowsById,
            });
        }
        else {
            paginatedRowModel = {
                rows,
                flatRows,
                rowsById,
            };
        }
        paginatedRowModel.flatRows = [];
        const handleRow = (row) => {
            paginatedRowModel.flatRows.push(row);
            if (row.subRows.length) {
                row.subRows.forEach(handleRow);
            }
        };
        paginatedRowModel.rows.forEach(handleRow);
        return paginatedRowModel;
    }, (0, utils_1.getMemoOptions)(table.options, 'debugTable', 'getPaginationRowModel'));
}
