"use client";
const require_use_uncontrolled = require("../use-uncontrolled/use-uncontrolled.cjs");
let react = require("react");
//#region packages/@mantine/hooks/src/use-pagination/use-pagination.ts
function range(start, end) {
	const length = end - start + 1;
	return Array.from({ length }, (_, index) => index + start);
}
const DOTS = "dots";
function usePagination({ total, siblings = 1, boundaries = 1, page, initialPage, onChange, startValue = 1 }) {
	const _startValue = Math.max(Math.trunc(startValue), 1);
	const _endValue = Math.max(Math.trunc(total), _startValue);
	const _total = _endValue - _startValue + 1;
	const _initialPage = initialPage ?? _startValue;
	const [activePage, setActivePage] = require_use_uncontrolled.useUncontrolled({
		value: page,
		onChange,
		defaultValue: _initialPage,
		finalValue: _initialPage
	});
	const setPage = (0, react.useCallback)((pageNumber) => {
		if (pageNumber < _startValue) setActivePage(_startValue);
		else if (pageNumber > _endValue) setActivePage(_endValue);
		else setActivePage(pageNumber);
	}, [
		_startValue,
		_endValue,
		setActivePage
	]);
	const next = (0, react.useCallback)(() => setPage(activePage + 1), [activePage, setPage]);
	const previous = (0, react.useCallback)(() => setPage(activePage - 1), [activePage, setPage]);
	const first = (0, react.useCallback)(() => setPage(_startValue), [setPage, _startValue]);
	const last = (0, react.useCallback)(() => setPage(_endValue), [_endValue, setPage]);
	return {
		range: (0, react.useMemo)(() => {
			if (siblings * 2 + 3 + boundaries * 2 >= _total) return range(_startValue, _endValue);
			const leftSiblingIndex = Math.max(activePage - siblings, _startValue + boundaries - 1);
			const rightSiblingIndex = Math.min(activePage + siblings, _endValue - boundaries);
			const shouldShowLeftDots = leftSiblingIndex > _startValue + boundaries + 1;
			const shouldShowRightDots = rightSiblingIndex < _endValue - boundaries;
			if (!shouldShowLeftDots && shouldShowRightDots) return [
				...range(_startValue, _startValue + (siblings * 2 + boundaries + 2) - 1),
				DOTS,
				...range(_endValue - (boundaries - 1), _endValue)
			];
			if (shouldShowLeftDots && !shouldShowRightDots) {
				const rightItemCount = boundaries + 1 + 2 * siblings;
				return [
					...range(_startValue, _startValue + boundaries - 1),
					DOTS,
					...range(_endValue - rightItemCount, _endValue)
				];
			}
			return [
				...range(_startValue, _startValue + boundaries - 1),
				DOTS,
				...range(leftSiblingIndex, rightSiblingIndex),
				DOTS,
				...range(_endValue - boundaries + 1, _endValue)
			];
		}, [
			_total,
			siblings,
			activePage,
			_startValue,
			_endValue,
			boundaries
		]),
		active: activePage,
		setPage,
		next,
		previous,
		first,
		last
	};
}
//#endregion
exports.usePagination = usePagination;

//# sourceMappingURL=use-pagination.cjs.map