(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "@greenatom/icons", "../typography/P1", "../hooks/useForkRef", "./hooks/usePagination", "./style"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var icons_1 = require("@greenatom/icons");
    var P1_1 = (0, tslib_1.__importDefault)(require("../typography/P1"));
    var useForkRef_1 = (0, tslib_1.__importDefault)(require("../hooks/useForkRef"));
    var usePagination_1 = (0, tslib_1.__importDefault)(require("./hooks/usePagination"));
    var style_1 = require("./style");
    var Pagination = (0, react_1.forwardRef)(function (_a, ref) {
        var _b = _a.iconNext, iconNext = _b === void 0 ? react_1.default.createElement(icons_1.IconBrokenArrowRight3, null) : _b, _c = _a.iconPrev, iconPrev = _c === void 0 ? react_1.default.createElement(icons_1.IconBrokenArrowLeft2, null) : _c, generalPages = _a.generalPages, className = _a.className, switcherTextPrev = _a.switcherTextPrev, switcherTextNext = _a.switcherTextNext, isShowSwitchers = _a.isShowSwitchers, _d = _a.currentPage, currentPage = _d === void 0 ? 1 : _d, isMinimized = _a.isMinimized, onPageChange = _a.onPageChange, width = _a.width, isDisabled = _a.isDisabled, isFill = _a.isFill;
        var containerRef = (0, react_1.useRef)(null);
        var forkRef = (0, useForkRef_1.default)([ref, containerRef]);
        var hasMinified = (0, react_1.useMemo)(function () { return isMinimized || generalPages === 1; }, [isMinimized, generalPages]);
        var _e = (0, usePagination_1.default)({
            contentPerPage: 1,
            count: generalPages,
            currentPage: currentPage,
            isMinimized: hasMinified,
            ref: containerRef
        }), nextPage = _e.nextPage, prevPage = _e.prevPage, currPage = _e.currPage, gaps = _e.gaps, setPage = _e.setPage, totalPages = _e.totalPages;
        var changePage = function (page) {
            setPage(page);
            onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange(page);
        };
        var handleClickNextPage = function () {
            nextPage();
            onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange(currPage + 1);
        };
        var handleClickPrevPage = function () {
            prevPage();
            onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange(currPage - 1);
        };
        var renderPrev = (0, react_1.useMemo)(function () {
            if (isShowSwitchers) {
                return (react_1.default.createElement(style_1.StyledIcons, { viewType: "ghost", size: "s", onClick: handleClickPrevPage, isDisabled: !currentPage || currPage === 1 || isDisabled }, switcherTextPrev ? switcherTextPrev : iconPrev));
            }
            return null;
        }, [isShowSwitchers, iconPrev, switcherTextPrev, currPage, isDisabled]);
        var renderNext = (0, react_1.useMemo)(function () {
            if (isShowSwitchers) {
                return (react_1.default.createElement(style_1.StyledIcons, { viewType: "ghost", size: "s", onClick: handleClickNextPage, isDisabled: currPage === totalPages || isDisabled }, switcherTextNext ? switcherTextNext : iconNext));
            }
            return null;
        }, [isShowSwitchers, iconNext, switcherTextNext, currPage, isDisabled]);
        if (totalPages) {
            return (react_1.default.createElement(style_1.PaginationContainer, { isFill: isFill, isDisabled: isDisabled, className: className, ref: forkRef, width: width },
                renderPrev,
                react_1.default.createElement(style_1.Elems, { isMinimized: hasMinified || generalPages < 6 },
                    hasMinified ? null : (react_1.default.createElement(style_1.StyledPages, { forwardedAs: "li", size: "s", isDisabled: isDisabled, viewType: "ghost", onClick: function () { return changePage(1); }, isActive: currPage === 1 },
                        react_1.default.createElement(P1_1.default, { type: "phoenix" }, "1"))),
                    !hasMinified && gaps.before ? (react_1.default.createElement(style_1.StyledEllipses, { isDisabled: isDisabled },
                        react_1.default.createElement(P1_1.default, { type: "phoenix" }, "..."))) : null,
                    gaps.paginationGroup.map(function (el) { return (react_1.default.createElement(style_1.StyledPages, { forwardedAs: "li", viewType: "ghost", size: "s", isDisabled: isDisabled, onClick: function () { return changePage(el); }, isActive: currPage === el, key: el },
                        react_1.default.createElement(P1_1.default, { type: "phoenix" }, el))); }),
                    !hasMinified && gaps.after && (react_1.default.createElement(style_1.StyledEllipses, { isDisabled: isDisabled },
                        react_1.default.createElement(P1_1.default, { type: "phoenix" }, "..."))),
                    hasMinified ? null : (react_1.default.createElement(style_1.StyledPages, { forwardedAs: "li", size: "s", isDisabled: isDisabled, viewType: "ghost", isActive: currPage === totalPages, onClick: function () { return changePage(totalPages); } },
                        react_1.default.createElement(P1_1.default, { type: "phoenix" }, totalPages)))),
                renderNext));
        }
        return null;
    });
    Pagination.displayName = "Pagination";
    exports.default = Pagination;
});
