"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pagination = void 0;
var react_1 = __importDefault(require("react"));
var Pagination_style_1 = __importDefault(require("./Pagination.style"));
exports.Pagination = function (_a) {
    var total = _a.total, perPage = _a.perPage, currentPage = _a.currentPage, changePage = _a.changePage;
    var pages = [];
    var totalPages = parseInt(String(total / perPage + 0.99), 10);
    if (total === perPage)
        totalPages = 1;
    var start = currentPage - 3;
    var end = currentPage + 2;
    if (currentPage <= 4) {
        start = 0;
        end = 5;
    }
    else if (totalPages - currentPage <= 3) {
        start = totalPages - 5;
        end = totalPages;
    }
    if (start < 0) {
        start = 0;
    }
    if (end > totalPages) {
        end = totalPages;
    }
    if (start > 1) {
        pages.push({
            id: 1,
            number: 1,
        });
        pages.push({
            id: 'back_p',
            disabled: true,
            number: '...',
        });
    }
    for (var i = start; i < end; i += 1) {
        pages.push({
            id: i + 1,
            active: i + 1 === currentPage,
            number: i + 1,
        });
    }
    if (totalPages - end >= 2) {
        pages.push({
            id: 'last_p',
            disabled: true,
            number: '...',
        });
        pages.push({
            id: totalPages,
            number: totalPages,
        });
    }
    var last = currentPage !== 1;
    var next = currentPage !== totalPages;
    function setPage(page) {
        if (!page.disabled)
            changePage(page.number);
    }
    return (react_1.default.createElement(react_1.default.Fragment, null, pages && pages.length > 1 && (react_1.default.createElement(Pagination_style_1.default, { "data-cy": "pagination" },
        react_1.default.createElement("div", { className: "pagination" },
            currentPage > 1 && (react_1.default.createElement("div", { onClick: function () { return changePage(currentPage - 1); }, className: "page " + (last && 'hidden'), "data-cy": "page-back", "data-testid": "page-back" }, '<')),
            pages.map(function (page) {
                return (react_1.default.createElement("div", { className: "page " + (page.number === currentPage && ' active ') + (page.disabled && ' disabled '), onClick: function () { return setPage(page); }, key: page.id, "data-cy": "page-" + page.id, "data-testid": "page-" + page.id }, page.number));
            }),
            currentPage !== totalPages && (react_1.default.createElement("div", { onClick: function () { return changePage(currentPage + 1); }, className: "page " + (next && 'hidden'), "data-cy": "page-next", "data-testid": "page-next" }, '>')))))));
};
//# sourceMappingURL=Pagination.js.map