"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RowPagination = void 0;
const utils_1 = require("../utils");
//
const defaultPageIndex = 0;
const defaultPageSize = 10;
const getDefaultPaginationState = () => ({
    pageIndex: defaultPageIndex,
    pageSize: defaultPageSize,
});
exports.RowPagination = {
    getInitialState: (state) => {
        return {
            ...state,
            pagination: {
                ...getDefaultPaginationState(),
                ...state === null || state === void 0 ? void 0 : state.pagination,
            },
        };
    },
    getDefaultOptions: (table) => {
        return {
            onPaginationChange: (0, utils_1.makeStateUpdater)('pagination', table),
        };
    },
    createTable: (table) => {
        let registered = false;
        let queued = false;
        table._autoResetPageIndex = () => {
            var _a, _b;
            if (!registered) {
                table._queue(() => {
                    registered = true;
                });
                return;
            }
            if ((_b = (_a = table.options.autoResetAll) !== null && _a !== void 0 ? _a : table.options.autoResetPageIndex) !== null && _b !== void 0 ? _b : !table.options.manualPagination) {
                if (queued)
                    return;
                queued = true;
                table._queue(() => {
                    table.resetPageIndex();
                    queued = false;
                });
            }
        };
        table.setPagination = (updater) => {
            var _a, _b;
            const safeUpdater = (old) => {
                const newState = (0, utils_1.functionalUpdate)(updater, old);
                return newState;
            };
            return (_b = (_a = table.options).onPaginationChange) === null || _b === void 0 ? void 0 : _b.call(_a, safeUpdater);
        };
        table.resetPagination = (defaultState) => {
            var _a;
            table.setPagination(defaultState
                ? getDefaultPaginationState()
                : ((_a = table.initialState.pagination) !== null && _a !== void 0 ? _a : getDefaultPaginationState()));
        };
        table.setPageIndex = (updater) => {
            table.setPagination((old) => {
                let pageIndex = (0, utils_1.functionalUpdate)(updater, old.pageIndex);
                const maxPageIndex = typeof table.options.pageCount === 'undefined' || table.options.pageCount === -1
                    ? Number.MAX_SAFE_INTEGER
                    : table.options.pageCount - 1;
                pageIndex = Math.max(0, Math.min(pageIndex, maxPageIndex));
                return {
                    ...old,
                    pageIndex,
                };
            });
        };
        table.resetPageIndex = (defaultState) => {
            var _a, _b, _c;
            table.setPageIndex(defaultState
                ? defaultPageIndex
                : ((_c = (_b = (_a = table.initialState) === null || _a === void 0 ? void 0 : _a.pagination) === null || _b === void 0 ? void 0 : _b.pageIndex) !== null && _c !== void 0 ? _c : defaultPageIndex));
        };
        table.resetPageSize = (defaultState) => {
            var _a, _b, _c;
            table.setPageSize(defaultState
                ? defaultPageSize
                : ((_c = (_b = (_a = table.initialState) === null || _a === void 0 ? void 0 : _a.pagination) === null || _b === void 0 ? void 0 : _b.pageSize) !== null && _c !== void 0 ? _c : defaultPageSize));
        };
        table.setPageSize = (updater) => {
            table.setPagination((old) => {
                const pageSize = Math.max(1, (0, utils_1.functionalUpdate)(updater, old.pageSize));
                const topRowIndex = old.pageSize * old.pageIndex;
                const pageIndex = Math.floor(topRowIndex / pageSize);
                return {
                    ...old,
                    pageIndex,
                    pageSize,
                };
            });
        };
        // deprecated
        table.setPageCount = (updater) => table.setPagination((old) => {
            var _a;
            let newPageCount = (0, utils_1.functionalUpdate)(updater, (_a = table.options.pageCount) !== null && _a !== void 0 ? _a : -1);
            if (typeof newPageCount === 'number') {
                newPageCount = Math.max(-1, newPageCount);
            }
            return {
                ...old,
                pageCount: newPageCount,
            };
        });
        table.getPageOptions = (0, utils_1.memo)(() => [table.getPageCount()], (pageCount) => {
            let pageOptions = [];
            if (pageCount && pageCount > 0) {
                pageOptions = [...new Array(pageCount)].fill(null).map((_, i) => i);
            }
            return pageOptions;
        }, (0, utils_1.getMemoOptions)(table.options, 'debugTable', 'getPageOptions'));
        table.getCanPreviousPage = () => table.getState().pagination.pageIndex > 0;
        table.getCanNextPage = () => {
            const { pageIndex } = table.getState().pagination;
            const pageCount = table.getPageCount();
            if (pageCount === -1) {
                return true;
            }
            if (pageCount === 0) {
                return false;
            }
            return pageIndex < pageCount - 1;
        };
        table.previousPage = () => {
            return table.setPageIndex((old) => old - 1);
        };
        table.nextPage = () => {
            return table.setPageIndex((old) => {
                return old + 1;
            });
        };
        table.firstPage = () => {
            return table.setPageIndex(0);
        };
        table.lastPage = () => {
            return table.setPageIndex(table.getPageCount() - 1);
        };
        table.getPrePaginationRowModel = () => table.getExpandedRowModel();
        table.getPaginationRowModel = () => {
            if (!table._getPaginationRowModel && table.options.getPaginationRowModel) {
                table._getPaginationRowModel = table.options.getPaginationRowModel(table);
            }
            if (table.options.manualPagination || !table._getPaginationRowModel) {
                return table.getPrePaginationRowModel();
            }
            return table._getPaginationRowModel();
        };
        table.getPageCount = () => {
            var _a;
            return ((_a = table.options.pageCount) !== null && _a !== void 0 ? _a : Math.ceil(table.getRowCount() / table.getState().pagination.pageSize));
        };
        table.getRowCount = () => {
            var _a;
            return (_a = table.options.rowCount) !== null && _a !== void 0 ? _a : table.getPrePaginationRowModel().rows.length;
        };
    },
};
