"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get applyPaginationToQueryFromPlugin () {
        return applyPaginationToQueryFromPlugin;
    },
    get applyPaginationToQueryParameters () {
        return applyPaginationToQueryParameters;
    },
    get getPagedRowHeaderIndex () {
        return getPagedRowHeaderIndex;
    },
    get handleAfterPageChangeExternalPagination () {
        return handleAfterPageChangeExternalPagination;
    },
    get handleAfterPageSizeChangeExternalPagination () {
        return handleAfterPageSizeChangeExternalPagination;
    },
    get normalizeExternalPaginationPageSize () {
        return normalizeExternalPaginationPageSize;
    }
});
const _constants = require("../constants");
const PAGINATION_PLUGIN_KEY = 'pagination';
function applyPaginationToQueryParameters(paginationPlugin, queryParameters) {
    if (!paginationPlugin?.enabled) {
        return;
    }
    const currentPageSize = typeof paginationPlugin.getCurrentPageSize === 'function' ? paginationPlugin.getCurrentPageSize() : undefined;
    const settingsPageSize = paginationPlugin.getSetting('pageSize');
    const initialPage = paginationPlugin.getSetting('initialPage');
    const currentPage = typeof paginationPlugin.getCurrentPage === 'function' ? paginationPlugin.getCurrentPage() : undefined;
    if (typeof currentPageSize === 'number') {
        queryParameters.pageSize = currentPageSize;
    } else if (typeof settingsPageSize === 'number') {
        queryParameters.pageSize = settingsPageSize;
    }
    if (typeof currentPage === 'number' && currentPage >= 1) {
        queryParameters.page = currentPage;
    } else if (typeof initialPage === 'number' && initialPage >= 1) {
        queryParameters.page = initialPage;
    }
}
function normalizeExternalPaginationPageSize(newPageSize, fallbackPageSize) {
    if (newPageSize === 'auto' || typeof newPageSize !== 'number' || !Number.isFinite(newPageSize) || newPageSize < 1) {
        return fallbackPageSize;
    }
    return newPageSize;
}
function applyPaginationToQueryFromPlugin(hot, queryParameters) {
    applyPaginationToQueryParameters(hot.getPlugin(PAGINATION_PLUGIN_KEY), queryParameters);
}
function getPagedRowHeaderIndex(queryParameters, visualRowIndex, fallbackPageSize = _constants.DEFAULT_PAGE_SIZE) {
    const { page, pageSize } = queryParameters;
    const ps = typeof pageSize === 'number' && pageSize >= 1 ? pageSize : fallbackPageSize;
    return (page - 1) * ps + visualRowIndex;
}
function handleAfterPageChangeExternalPagination(ctx, oldPage, newPage) {
    const { hot, getQueryPage, goToPage } = ctx;
    const paginationPlugin = hot.getPlugin(PAGINATION_PLUGIN_KEY);
    if (!paginationPlugin?.enabled) {
        return;
    }
    if (newPage === getQueryPage()) {
        return;
    }
    goToPage(newPage).catch(()=>{
        const p = hot.getPlugin(PAGINATION_PLUGIN_KEY);
        if (p?.enabled) {
            p.revertPageTo(oldPage);
        }
    });
}
function handleAfterPageSizeChangeExternalPagination(ctx, oldPageSize, newPageSize) {
    const { hot, getQueryPage, getQueryPageSize, setPageSize } = ctx;
    const paginationPlugin = hot.getPlugin(PAGINATION_PLUGIN_KEY);
    if (!paginationPlugin?.enabled) {
        return;
    }
    const ps = normalizeExternalPaginationPageSize(newPageSize, _constants.DEFAULT_PAGE_SIZE);
    if (ps === getQueryPageSize() && getQueryPage() === 1) {
        return;
    }
    setPageSize(ps).catch(()=>{
        const p = hot.getPlugin(PAGINATION_PLUGIN_KEY);
        if (p?.enabled) {
            p.revertPageSizeTo(oldPageSize);
        }
    });
}
