"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const DataGridContext_1 = require("../../DataGrid/DataGridContext");
const constants_1 = require("../constants");
const useLogic = ({ totalCount, rowsPerPage = constants_1.DEFAULT_ROWS_PER_PAGE, page, onSetCountPerPage, }) => {
    const { isLoading } = (0, react_1.useContext)(DataGridContext_1.DataGridContext);
    const prevTotalCountRef = (0, react_1.useRef)(0);
    (0, react_1.useEffect)(() => {
        if (!isLoading) {
            prevTotalCountRef.current = totalCount;
        }
    }, [totalCount, isLoading]);
    const actualTotalCount = isLoading ? prevTotalCountRef.current : totalCount;
    const pageCount = Math.ceil(actualTotalCount / rowsPerPage);
    const rangeStart = (page - 1) * rowsPerPage + 1;
    const rangeEnd = () => {
        const end = page * rowsPerPage;
        return end < actualTotalCount ? end : actualTotalCount;
    };
    const formattedRange = () => {
        return `${rangeStart} — ${rangeEnd()} из ${actualTotalCount} записей`;
    };
    const isVisiblePagination = !(!onSetCountPerPage && actualTotalCount <= rowsPerPage);
    return {
        isVisiblePagination,
        pageCount,
        formattedRange,
        actualTotalCount,
    };
};
exports.useLogic = useLogic;
