"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePagination = void 0;
const react_1 = __importDefault(require("react"));
const hooks_1 = require("../hooks");
const utility_1 = require("../utility");
const usePagination = (input) => {
    const { track } = (0, hooks_1.useAnalytics)();
    const buildPagePath = react_1.default.useCallback((page) => {
        const currentPath = typeof window !== "undefined" ? window.location.pathname : "";
        return currentPath.includes("page=")
            ? currentPath.replace(/page=\d+/g, `page=${page}`)
            : `${currentPath}${currentPath.includes("?") ? "&" : "?"}page=${page}`;
    }, []);
    const pagesToShow = react_1.default.useMemo(() => getPages(input.pages, input.totalPages, input.currentPage), [input.pages, input.totalPages, input.currentPage]);
    // default input.currentPage is zero-indexed, actualCurrentPage is one-indexed
    const actualCurrentPage = react_1.default.useMemo(() => input.currentPage + 1, [input.currentPage]);
    const hasNextPage = react_1.default.useMemo(() => actualCurrentPage < input.totalPages, [actualCurrentPage, input.totalPages]);
    const hasPreviousPage = react_1.default.useMemo(() => actualCurrentPage > 1, [actualCurrentPage]);
    const hasPage = react_1.default.useCallback((pageNumber) => {
        return pageNumber >= 0 && pageNumber < input.totalPages;
    }, [input.totalPages]);
    const handlePageChange = react_1.default.useCallback((pageNumber, event) => {
        utility_1.debug.log("usePagination", "handlePageChange[pageNumber]", pageNumber);
        event === null || event === void 0 ? void 0 : event.preventDefault();
        if (!hasPage(pageNumber)) {
            return;
        }
        input.setPage(pageNumber);
        track({
            eventName: "paginationChange",
            payload: {
                page: pageNumber,
                source: window.location.pathname.includes("/search")
                    ? "search"
                    : "collection",
            },
        });
    }, [input.setPage, track]);
    const handleNextPage = react_1.default.useCallback((event) => {
        utility_1.debug.log("usePagination", "handleNextPage", input.currentPage + 1);
        handlePageChange(input.currentPage + 1, event);
    }, [hasNextPage, input.currentPage, handlePageChange]);
    const handlePreviousPage = react_1.default.useCallback((event) => {
        utility_1.debug.log("usePagination", "handlePreviousPage", input.currentPage - 1);
        handlePageChange(input.currentPage - 1, event);
    }, [hasPreviousPage, input.currentPage, handlePageChange]);
    return react_1.default.useMemo(() => ({
        currentPage: input.currentPage,
        totalPages: input.totalPages,
        pagesToShow,
        hasNextPage,
        buildPagePath,
        hasPreviousPage,
        actualCurrentPage,
        handleNextPage,
        handlePreviousPage,
        handlePageChange,
    }), [
        input.currentPage,
        input.totalPages,
        pagesToShow,
        hasNextPage,
        buildPagePath,
        hasPreviousPage,
        actualCurrentPage,
        handleNextPage,
        handlePreviousPage,
        handlePageChange,
    ]);
};
exports.usePagination = usePagination;
// get a list of pages to display buttons for
// @todo this is super hacky and needs to be cleaned up
const getPages = (pages, totalPages, currentPage) => {
    const pagesToShow = pages > totalPages ? totalPages : pages;
    const buffer = Math.floor(pagesToShow / 2);
    let list = [];
    let start = 0;
    if (totalPages <= 5 || currentPage < buffer + 1) {
        start = 0;
    }
    else {
        if (totalPages - buffer <= currentPage) {
            start =
                currentPage + 1 === totalPages
                    ? currentPage - (pagesToShow - 1)
                    : currentPage - (buffer + buffer / 2);
        }
        else {
            start = currentPage - buffer;
        }
    }
    Array.apply(null, Array(pagesToShow)).map((item, index) => {
        list.push(start + index);
    });
    return list;
};
//# sourceMappingURL=usePagination.js.map