(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", "../../utils/getTextWidth", "../style"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = require("react");
    var getTextWidth_1 = (0, tslib_1.__importDefault)(require("../../utils/getTextWidth"));
    var style_1 = require("../style");
    var usePagination = function (_a) {
        var contentPerPage = _a.contentPerPage, count = _a.count, currentPage = _a.currentPage, isMinimized = _a.isMinimized, ref = _a.ref;
        var _b = (0, tslib_1.__read)((0, react_1.useState)(Math.max(currentPage < count ? currentPage : count, 1)), 2), currPage = _b[0], setCurrPage = _b[1];
        (0, react_1.useEffect)(function () {
            setCurrPage(Math.max(currentPage < count ? currentPage : count, 1));
        }, [currentPage]);
        var _c = (0, tslib_1.__read)((0, react_1.useState)(0), 2), widthContainer = _c[0], setWidthContainer = _c[1];
        // number of pages in total (total items / content on each page)
        var pageCount = Math.ceil(count / contentPerPage);
        // useResizeObserved([ref], handleResize);
        var handlerResizeWindow = function () {
            if (ref.current) {
                setWidthContainer(ref.current.clientWidth);
            }
        };
        (0, react_1.useEffect)(function () {
            var resizeObserver = new ResizeObserver(handlerResizeWindow);
            ref.current && resizeObserver.observe(ref.current);
            window.addEventListener("resize", handlerResizeWindow);
            return function () {
                resizeObserver.disconnect();
                window.removeEventListener("resize", handlerResizeWindow);
            };
        }, []);
        var countItems = (0, react_1.useMemo)(function () {
            if (ref.current && widthContainer) {
                var accessibleWidth_1 = widthContainer;
                var icons = ref.current.querySelectorAll("" + style_1.StyledIcons);
                icons.forEach(function (icon) {
                    accessibleWidth_1 -= icon.clientWidth;
                });
                var containerItemsPages = ref.current.querySelector("" + style_1.Elems);
                if (containerItemsPages) {
                    var children = containerItemsPages.children;
                    var itemFirst = children.item(0);
                    var itemLast = children.item(children.length - 1);
                    if (itemFirst && itemLast) {
                        var elementPadding = Number.parseInt(getComputedStyle(itemFirst).paddingLeft, 10) +
                            Number.parseInt(getComputedStyle(itemFirst).paddingRight, 10);
                        var currentPageWidth = (0, getTextWidth_1.default)(String(currPage), getComputedStyle(itemFirst).font);
                        accessibleWidth_1 -= itemFirst.clientWidth + itemLast.clientWidth;
                        var averageWidthItem = (itemLast.clientWidth + currentPageWidth + elementPadding) / 2;
                        return Math.ceil(accessibleWidth_1 / averageWidthItem) - 2;
                    }
                }
            }
            return 0;
        }, [widthContainer, currPage, pageCount]);
        var _d = (0, tslib_1.__read)((0, react_1.useState)({
            before: false,
            paginationGroup: [],
            after: true
        }), 2), gaps = _d[0], setGaps = _d[1];
        var _e = (0, tslib_1.__read)((0, react_1.useState)([]), 2), pagesInBetween = _e[0], setPagesInBetween = _e[1];
        (0, react_1.useEffect)(function () {
            if (pageCount > 2) {
                if (isMinimized) {
                    var temp = new Array(pageCount).fill(1).map(function (_, i) { return i + 1; });
                    setPagesInBetween(temp);
                }
                else {
                    var temp = new Array(pageCount - 2).fill(1).map(function (_, i) { return i + 2; });
                    setPagesInBetween(temp);
                }
            }
            else {
                setPagesInBetween([]);
            }
        }, [pageCount]);
        (0, react_1.useEffect)(function () {
            if (currPage > pageCount) {
                setCurrPage(pageCount - (pageCount - 1));
            }
        }, [currPage]);
        (0, react_1.useEffect)(function () {
            var currentLocation = pagesInBetween.indexOf(currPage);
            var paginationGroup = [];
            var before = false;
            var after = false;
            if (currPage === 1) {
                if (isMinimized) {
                    paginationGroup = pagesInBetween.slice(0, 1);
                }
                else {
                    paginationGroup = pagesInBetween.slice(0, countItems);
                }
            }
            else if (currPage === pageCount) {
                paginationGroup = pagesInBetween.slice(pageCount - countItems - 2, pageCount);
            }
            else if (currPage >= pageCount - countItems + 2) {
                paginationGroup = pagesInBetween.slice(-countItems, pageCount);
            }
            else if (currPage < countItems) {
                paginationGroup = pagesInBetween.slice(0, countItems);
            }
            else {
                var centralCountItem = Math.ceil(countItems / 2);
                paginationGroup = pagesInBetween.slice(currentLocation - centralCountItem + 1, currentLocation + centralCountItem);
            }
            if (pageCount <= countItems - 1) {
                before = false;
                after = false;
            }
            else {
                before = false;
                after = false;
                if (paginationGroup[0] > 2) {
                    before = true;
                }
                if (paginationGroup[0] < pageCount - countItems) {
                    after = true;
                }
            }
            if (isMinimized || countItems <= 1) {
                paginationGroup = [currPage];
            }
            setGaps({ paginationGroup: paginationGroup, before: before, after: after });
        }, [
            currPage,
            pagesInBetween,
            pageCount,
            isMinimized,
            widthContainer,
            countItems
        ]);
        var changePage = function (direction) {
            setCurrPage(function (state) {
                if (direction) {
                    if (state === pageCount) {
                        return state;
                    }
                    return state + 1;
                }
                else {
                    if (state === 1) {
                        return state;
                    }
                    return state - 1;
                }
            });
        };
        var setPageSAFE = function (num) {
            if (num > pageCount) {
                setCurrPage(pageCount);
            }
            else if (num < 1) {
                setCurrPage(1);
            }
            else {
                setCurrPage(num);
            }
        };
        return {
            totalPages: pageCount,
            nextPage: function () { return changePage(true); },
            prevPage: function () { return changePage(false); },
            setPage: setPageSAFE,
            currPage: currPage,
            gaps: gaps
        };
    };
    exports.default = usePagination;
});
