"use strict";
exports.__esModule = true;
exports.Pagination = exports.cnPagination = exports.paginationDefaultType = exports.paginationTypes = exports.paginationDefaultSize = exports.paginationSizes = void 0;
var tslib_1 = require("tslib");
// @ts-nocheck
require("./Pagination.css");
var react_1 = (0, tslib_1.__importDefault)(require("react"));
var IconChevronArrowLeft_1 = require("../../icons/IconChevronArrowLeft/IconChevronArrowLeft");
var IconChevronArrowRight_1 = require("../../icons/IconChevronArrowRight/IconChevronArrowRight");
var bem_1 = require("../../utils/bem");
var Button_1 = require("../Button/Button");
var Typography_1 = require("../Typography");
var helpers_1 = require("./helpers");
exports.paginationSizes = ['xs', 's', 'm', 'l'];
exports.paginationDefaultSize = 'm';
exports.paginationTypes = ['default'];
exports.paginationDefaultType = 'default';
var getDefaultLabel = function (totalPages) { return "".concat(totalPages); };
exports.cnPagination = (0, bem_1.cn)('Pagination');
// eslint-disable-next-line @typescript-eslint/no-redeclare
exports.Pagination = react_1["default"].forwardRef(function (props, ref) {
    var _a = props.currentPage, currentPage = _a === void 0 ? 1 : _a, _b = props.totalPages, totalPages = _b === void 0 ? 0 : _b, onChange = props.onChange, _c = props.getLabel, getLabel = _c === void 0 ? getDefaultLabel(totalPages) : _c, _d = props.size, size = _d === void 0 ? exports.paginationDefaultSize : _d, 
    // type = paginationDefaultType,
    _e = props.minified, 
    // type = paginationDefaultType,
    minified = _e === void 0 ? false : _e, hideControls = props.hideControls, _f = props.hotkeys, hotkeys = _f === void 0 ? {
        nextPage: {
            values: ['Alt', 'ArrowRight']
        },
        prevPage: {
            values: ['Alt', 'ArrowLeft']
        }
    } : _f, _g = props.containerEventListener, containerEventListener = _g === void 0 ? window : _g, className = props.className, otherProps = (0, tslib_1.__rest)(props, ["currentPage", "totalPages", "onChange", "getLabel", "size", "minified", "hideControls", "hotkeys", "containerEventListener", "className"]);
    if (!totalPages)
        return null;
    var currPage = currentPage;
    var _h = (0, helpers_1.getPaginationInfo)(currPage, totalPages), prevPage = _h.prevPage, nextPage = _h.nextPage, isEmpty = _h.isEmpty;
    // eslint-disable-next-line react-hooks/rules-of-hooks
    var _j = (0, tslib_1.__read)(react_1["default"].useState([]), 2), keys = _j[0], setKeys = _j[1];
    var changePage = function (page) {
        if (page && page !== currPage && onChange) {
            onChange(page);
        }
    };
    var handleClick = function (page) { return function (e) {
        e.preventDefault();
        changePage(page);
    }; };
    var keyUpListener = function (event) {
        var key = event.key;
        setKeys(function (prevState) { return prevState.filter(function (hotKey) { return hotKey !== key; }); });
    };
    var keyDownListener = function (event) {
        var key = event.key;
        if (hotkeys.nextPage.values.includes(key) || hotkeys.prevPage.values.includes(key)) {
            var newKeys_1 = (0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([], (0, tslib_1.__read)(keys), false), [key], false);
            if (hotkeys.nextPage.values.every(function (hotKey) { return newKeys_1.includes(hotKey); })) {
                changePage(nextPage);
            }
            else if (hotkeys.prevPage.values.every(function (hotKey) { return newKeys_1.includes(hotKey); })) {
                changePage(prevPage);
            }
            setKeys(newKeys_1);
        }
    };
    // eslint-disable-next-line react-hooks/rules-of-hooks
    react_1["default"].useEffect(function () {
        containerEventListener.addEventListener('keydown', keyDownListener);
        containerEventListener.addEventListener('keyup', keyUpListener);
        return function () {
            containerEventListener.removeEventListener('keydown', keyDownListener);
            containerEventListener.removeEventListener('keyup', keyUpListener);
        };
    });
    return ((!isEmpty && (react_1["default"].createElement("nav", (0, tslib_1.__assign)({ className: (0, exports.cnPagination)({
            size: size
        }, [className]), ref: ref }, otherProps),
        react_1["default"].createElement("div", { className: (0, exports.cnPagination)('Pages') },
            react_1["default"].createElement(Typography_1.Typography.Text, { size: size, weight: "bold", transform: "uppercase", className: (0, exports.cnPagination)('Current') }, currPage.toString()),
            react_1["default"].createElement(Typography_1.Typography.Text, { size: size, className: (0, exports.cnPagination)('Total'), weight: "bold", transform: "uppercase" }, getLabel)),
        !hideControls && (react_1["default"].createElement("div", { className: (0, exports.cnPagination)('Controls') },
            (currPage > 1 || minified) && (react_1["default"].createElement("div", { className: (0, exports.cnPagination)('PrevPage') },
                react_1["default"].createElement(Button_1.Button, { className: (0, exports.cnPagination)('ItemLeft', {
                        minified: minified,
                        show: currPage > 1
                    }), iconLeft: IconChevronArrowLeft_1.IconChevronArrowLeft, view: "ghost", size: size, iconSize: "xs", onClick: handleClick(prevPage) }))),
            (currPage < totalPages || minified) && (react_1["default"].createElement("div", { className: (0, exports.cnPagination)('NextPage') },
                react_1["default"].createElement(Button_1.Button, { className: (0, exports.cnPagination)('ItemRight', {
                        minified: minified,
                        show: currPage < totalPages
                    }), iconRight: IconChevronArrowRight_1.IconChevronArrowRight, view: "ghost", size: size, onClick: handleClick(nextPage) })))))))) ||
        null);
});
