"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"));
var Pagination = function (_a) {
    var total = _a.total, perPage = _a.perPage, currentPage = _a.currentPage, onChange = _a.onChange, activeTextColor = _a.activeTextColor, activeBgColor = _a.activeBgColor, textColor = _a.textColor;
    var pages = [];
    var totalPages = parseInt(String(total / perPage + 0.99), 10);
    if (total === perPage)
        totalPages = 1;
    var start = currentPage - 2;
    var end = currentPage + 1;
    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)
            onChange(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-testid": "pagination", activeTextColor: activeTextColor, activeBgColor: activeBgColor, textColor: textColor },
        react_1.default.createElement("div", { className: "pagination" },
            currentPage > 1 && (react_1.default.createElement("div", { onClick: function () { return onChange(currentPage - 1); }, className: "page ".concat(last && 'hidden'), "data-testid": "page-back" }, '<')),
            pages.map(function (page) {
                return (react_1.default.createElement("div", { className: "page ".concat(page.number === currentPage && ' active ').concat(page.disabled && ' disabled '), onClick: function () { return setPage(page); }, key: page.id, "data-testid": "page-".concat(page.id) }, page.number));
            }),
            currentPage !== totalPages && (react_1.default.createElement("div", { onClick: function () { return onChange(currentPage + 1); }, className: "page ".concat(next && 'hidden'), "data-testid": "page-next" }, '>')))))));
};
exports.Pagination = Pagination;
//# sourceMappingURL=Pagination.js.map