import { useCallback, useRef } from "react";
import { debounce } from "lodash";
//#region src/components/PisellScrollView/hooks/useScrollEvents.ts
const useScrollEvents = ({ containerRef, onScroll, onScrollStart, onScrollEnd, onReachTop, onReachBottom, onReachLeft, onReachRight, reachThreshold = 0, scrollEndDelay = 150, updateScrollState, scrollState }) => {
	const isScrolling = useRef(false);
	const handleScrollEnd = useCallback(debounce((finalState) => {
		isScrolling.current = false;
		onScrollEnd === null || onScrollEnd === void 0 || onScrollEnd(finalState);
	}, scrollEndDelay), [onScrollEnd, scrollEndDelay]);
	return { handleScroll: useCallback(() => {
		const container = containerRef.current;
		if (!container) return;
		updateScrollState();
		const { scrollTop, scrollLeft, scrollHeight, scrollWidth, clientHeight, clientWidth } = container;
		const maxScrollTop = Math.max(0, scrollHeight - clientHeight);
		const maxScrollLeft = Math.max(0, scrollWidth - clientWidth);
		const currentData = {
			scrollTop,
			scrollLeft,
			scrollHeight,
			scrollWidth,
			clientHeight,
			clientWidth,
			percentY: maxScrollTop > 0 ? scrollTop / maxScrollTop * 100 : 0,
			percentX: maxScrollLeft > 0 ? scrollLeft / maxScrollLeft * 100 : 0,
			direction: null,
			isScrollToTop: scrollTop <= reachThreshold,
			isScrollToBottom: scrollTop + clientHeight >= scrollHeight - reachThreshold,
			isScrollToLeft: scrollLeft <= reachThreshold,
			isScrollToRight: scrollLeft + clientWidth >= scrollWidth - reachThreshold,
			isOverflows: scrollHeight > clientHeight || scrollWidth > clientWidth
		};
		if (!isScrolling.current) {
			isScrolling.current = true;
			onScrollStart === null || onScrollStart === void 0 || onScrollStart(currentData);
		}
		onScroll === null || onScroll === void 0 || onScroll(currentData);
		if (scrollTop <= reachThreshold) onReachTop === null || onReachTop === void 0 || onReachTop(currentData);
		if (scrollTop + clientHeight >= scrollHeight - reachThreshold) onReachBottom === null || onReachBottom === void 0 || onReachBottom(currentData);
		if (scrollLeft <= reachThreshold) onReachLeft === null || onReachLeft === void 0 || onReachLeft(currentData);
		if (scrollLeft + clientWidth >= scrollWidth - reachThreshold) onReachRight === null || onReachRight === void 0 || onReachRight(currentData);
		handleScrollEnd(currentData);
	}, [
		containerRef,
		updateScrollState,
		onScroll,
		onScrollStart,
		handleScrollEnd,
		onReachTop,
		onReachBottom,
		onReachLeft,
		onReachRight,
		reachThreshold
	]) };
};
//#endregion
export { useScrollEvents };
