import { getMemoOptions, memo } from '../utils';
import { expandRows } from './getExpandedRowModel';
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- 保留参数供未来扩展
export function getPaginationRowModel(_opts) {
    return (table) => 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 = 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;
    }, getMemoOptions(table.options, 'debugTable', 'getPaginationRowModel'));
}
