import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useMemo } from 'react';
import { useAppSelector } from 'src/app/redux/store';
import css from './Paginator.module.scss';
import { _cx } from 'src/utility/class';
import ArrowIcon from '../icon/ArrowIcon';
var cx = _cx(css);
export default function Paginator(_a) {
    var pageCount = _a.pageCount, currentPageIndex = _a.currentPageIndex, _b = _a.showHeader, showHeader = _b === void 0 ? true : _b, setIndex = _a.setIndex;
    var _c = useAppSelector(function (state) { return state.config.config; }), language = _c.language, paginationLimit = _c.paginationLimit;
    var t = useAppSelector(function (state) { return state.config.config.languageData[language]; });
    var pages = Array.from({ length: pageCount }, function (_, i) { return i + 1; });
    var lastIndex = pages.length - 1;
    var pageIndex = currentPageIndex > lastIndex ? lastIndex : currentPageIndex;
    var divider = useMemo(function () { return Math.floor(paginationLimit / 2); }, [paginationLimit]);
    var isEven = paginationLimit % 2 === 0;
    var closerToStartThanLimit = pageIndex - divider < 0 || pageCount < paginationLimit;
    var closerToEndThanLimit = isEven
        ? pageIndex + divider - 1 > lastIndex
        : pageIndex + divider > lastIndex;
    var countToEnd = lastIndex - pageIndex + 1;
    var countBackwards = closerToStartThanLimit
        ? pageIndex
        : closerToEndThanLimit
            ? paginationLimit - countToEnd
            : divider;
    var countForwards = isEven ? divider : divider + 1;
    var firstPos = pageIndex - countBackwards;
    var lastPos = closerToStartThanLimit ? paginationLimit : pageIndex + countForwards;
    var pagesToShow = pages.slice(firstPos, lastPos);
    var notFirstIndex = pageIndex > 0;
    var notLastIndex = pageIndex < lastIndex;
    var setNewIndex = useCallback(function (i) {
        setIndex(i, !showHeader);
    }, []);
    return (_jsxs("div", { className: cx('container'), children: [showHeader && (_jsx("span", { children: "".concat(pageCount, " ").concat(pageCount > 1 ? t.result_page_plural : t.result_page_single) })), _jsxs("div", { className: cx('pagination'), children: [_jsx("div", { className: cx('leftCol isNotFirst'), onClick: function () { return (notFirstIndex ? setNewIndex(pageIndex - 1) : ''); }, children: _jsx(ArrowIcon, { direction: "left", disabled: !notFirstIndex }) }), pageCount > 1 && (_jsxs("div", { className: cx('pages'), children: [pagesToShow.map(function (page) {
                                var index = page - 1;
                                return (_jsx("div", { className: cx('page', {
                                        selected: index === pageIndex,
                                        notSelected: index !== pageIndex,
                                    }), style: {
                                        marginLeft: "".concat(index !== pageIndex || (index === pageIndex && pageIndex !== 0) ? 2 : 0, "px"),
                                    }, onClick: function () { return setNewIndex(page - 1); }, children: page }, "page-".concat(page, "-").concat(showHeader ? "top" : "bottom")));
                            }), notLastIndex && (_jsx("div", { className: cx('page'), style: { marginRight: '0px', marginLeft: '0px' }, children: "..." }))] })), _jsx("div", { className: cx('rightCol'), onClick: function () { return (notLastIndex ? setNewIndex(pageIndex + 1) : ''); }, children: _jsx(ArrowIcon, { direction: "right", disabled: !notLastIndex }) })] })] }));
}
