"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pagination = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var v_eris_1 = require("v-eris");
require("./types.pagination.scss");
var Pagination = function (props) {
    var className = props.className, onChange = props.onChange, rest = tslib_1.__rest(props, ["className", "onChange"]);
    var currentLimit = v_eris_1.Common.uint(props.limit);
    var currentOffset = Math.floor(v_eris_1.Common.uint(props.offset) / currentLimit) * currentLimit;
    //const currentLength = Common.uint( props.length );
    var currentLength = v_eris_1.Common.uint(props.length);
    var maxPages = Math.floor(currentLength / currentLimit);
    if (currentOffset >= currentLength)
        currentOffset = v_eris_1.Common.uint(maxPages * currentLimit);
    var currentPage = (currentOffset / currentLimit);
    if (!onChange)
        onChange = function () { };
    var tryNext = function (offset, limit, length) {
        var current = offset + limit;
        if (current >= length)
            return -1;
        return current;
    };
    var tryPrev = function (offset, limit, length) {
        var current = offset - limit;
        if (current < 0)
            return -1;
        return current;
    };
    var tryPage = function (page, limit, length) {
        var current = page - limit;
        if (current < 0)
            return -1;
        return current;
    };
    var next = function () {
        var current = tryNext(currentOffset, currentLimit, currentLength);
        if (current < 0)
            return;
        onChange({ offset: current });
    };
    var prev = function () {
        var current = tryPrev(currentOffset, currentLimit, currentLength);
        if (current < 0)
            return;
        onChange({ offset: current });
    };
    var setPage = function (value) {
        var current = Math.floor(v_eris_1.Common.uint(value * currentLimit) / currentLimit) * currentLimit;
        if (current < 0)
            current = 0;
        if (current >= currentLength)
            current = v_eris_1.Common.uint(maxPages * currentLimit);
        onChange({ offset: current });
    };
    var pages = (0, react_1.useMemo)(function () {
        var pages = [];
        var power = 3;
        var powerLeft = 0;
        var block = { left: false, right: false };
        var min = currentPage - power;
        var max = currentPage + power;
        if (min < 0) {
            powerLeft += min * -1;
            min = 0;
            block.left = true;
        }
        ;
        if (max > maxPages) {
            powerLeft += max - maxPages;
            max = maxPages;
            block.right = true;
        }
        ;
        if (powerLeft && !block.right) {
            max += powerLeft;
        }
        ;
        if (powerLeft && !block.left) {
            min -= powerLeft;
        }
        ;
        min = v_eris_1.VMath.max(0, min);
        max = v_eris_1.VMath.min(maxPages, max);
        max += 1;
        for (var n = min; n < max; n++) {
            pages.push(n);
        }
        ;
        pages[0] = 0;
        if (pages.length > 1)
            pages[pages.length - 1] = maxPages;
        if ((pages.length > 2) && (pages[1] < currentPage) && ((pages[1] - pages[0]) > 1))
            pages[1] = -1;
        if ((pages.length > 2) && (pages[pages.length - 2] > currentPage) && ((pages[pages.length - 1] - pages[pages.length - 2]) > 1))
            pages[pages.length - 2] = -1;
        return pages;
    }, [currentOffset, currentLimit, currentLength, maxPages]);
    return (react_1.default.createElement("div", { className: v_eris_1.Props.className("eris-pagination", { hidden: props.autoHide && maxPages < 1 }) },
        react_1.default.createElement(v_eris_1.Button, { onClick: prev, disabled: tryPrev(currentOffset, currentLimit, currentLength) < 0 }, "Prev"),
        pages.map(function (value, index) {
            return react_1.default.createElement(v_eris_1.Button, { key: index, warning: value == currentPage, disabled: value < 0, onClick: function () { return setPage(value); } }, (value < 0 ? "..." : v_eris_1.Common.string(value + 1)));
        }),
        react_1.default.createElement(v_eris_1.Button, { onClick: next, disabled: tryNext(currentOffset, currentLimit, currentLength) < 0 }, "Next"),
        react_1.default.createElement("div", { key: "carret", className: v_eris_1.Props.className("eris-pagination-input") },
            "Page:",
            react_1.default.createElement(v_eris_1.Input, { onKeyDown: function (e) {
                    if (e.event.which != 13)
                        return;
                    e.event.target.blur();
                }, onBlur: function (e) {
                    setPage(v_eris_1.Common.uint(e.value) - 1);
                } }, currentPage + 1))));
};
exports.Pagination = Pagination;
//# sourceMappingURL=types.pagination.js.map