import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__ from "../utils/utils.mjs";
const PAGINATION_DOTS = null;
/**
 * A React hook for creating pagination logic.
 *
 * This hook calculates the pagination range based on total count of items, page size, sibling count, and the current page.
 * It also takes care of scenarios where 'dots' should appear in the pagination component to indicate skipped page numbers.
 *
 * @param {Object} config - The configuration object for pagination.
 * @param {number} config.totalCount - The total count of items to be paginated.
 * @param {number} config.pageSize - The number of items per page.
 * @param {number} [config.siblingCount=1] - The number of sibling pages to show adjacent to the current page.
 * @param {number} config.currentPage - The current active page.
 *
 * @returns {{pages: Array<number|null>, totalPageCount: number}}
 * An object containing the pagination range (pages) and the total number of pages (totalPageCount).
 * The 'pages' array contains numbers for page indexes and nulls where 'dots' should appear in the UI.
 *
 * @example
 * // Example usage:
 * const { pages, totalPageCount } = usePagination({
 *   totalCount: 100,
 *   pageSize: 10,
 *   siblingCount: 1,
 *   currentPage: 5
 * });
 *
 * // This will return:
 * // {
 * //   pages: [1, null, 4, 5, 6, null, 10],
 * //   totalPageCount: 10
 * // }
 */ const usePagination = ({ totalCount, pageSize, siblingCount = 1, currentPage })=>{
    const totalPageCount = Math.ceil(totalCount / pageSize);
    const paginationRange = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        // Pages count is determined as siblingCount + firstPage + lastPage + currentPage + 2 * DOTS
        const totalPageNumbers = siblingCount + 5;
        /*
      If the number of pages is less than the page numbers we want to show in our
      paginationComponent, we return the range [1..totalPageCount]
    */ if (totalPageNumbers >= totalPageCount) return (0, __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__.range)(1, totalPageCount);
        const leftSiblingIndex = Math.max(currentPage - siblingCount, 1);
        const rightSiblingIndex = Math.min(currentPage + siblingCount, totalPageCount);
        /*
      We do not want to show dots if there is only one position left
      after/before the left/right page count as that would lead to a change if our Pagination
      component size which we do not want
    */ const shouldShowLeftDots = leftSiblingIndex > 2;
        const shouldShowRightDots = rightSiblingIndex < totalPageCount - 2;
        const firstPageIndex = 1;
        const lastPageIndex = totalPageCount;
        if (!shouldShowLeftDots && shouldShowRightDots) {
            let leftItemCount = 3 + 2 * siblingCount;
            let leftRange = (0, __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__.range)(1, leftItemCount);
            return [
                ...leftRange,
                PAGINATION_DOTS,
                totalPageCount
            ];
        }
        if (shouldShowLeftDots && !shouldShowRightDots) {
            let rightItemCount = 3 + 2 * siblingCount;
            let rightRange = (0, __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__.range)(totalPageCount - rightItemCount + 1, totalPageCount);
            return [
                firstPageIndex,
                PAGINATION_DOTS,
                ...rightRange
            ];
        }
        if (shouldShowLeftDots && shouldShowRightDots) {
            let middleRange = (0, __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__.range)(leftSiblingIndex, rightSiblingIndex);
            return [
                firstPageIndex,
                PAGINATION_DOTS,
                ...middleRange,
                PAGINATION_DOTS,
                lastPageIndex
            ];
        }
        return [];
    }, [
        totalCount,
        pageSize,
        siblingCount,
        currentPage
    ]);
    return {
        pages: paginationRange,
        totalPageCount
    };
};
export { usePagination };
