'use client';


import { useGetElementOrWindow } from "./useGetElementOrWindow.mjs";
import { useScrollBlockage } from "./useScrollBlockage/index.mjs";
import { useCallback, useEffect, useState } from "react";

//#region src/hooks/useScrollDetection.ts
const useScrollDetection = (props) => {
	const { isEnabled = true, delta = 5, deltaFromTop = 0, onScrollDown, onScrollUp, element } = props ?? {};
	const [isScrolled, setIsScrolled] = useState(false);
	const [isScrollable, setIsScrollable] = useState(false);
	const { isScrollBlocked } = useScrollBlockage();
	const containerElement = useGetElementOrWindow(element);
	const onMobileMove = useCallback((event) => {
		const startY = event.touches?.[0]?.clientY ?? 0;
		const onMove = (event$1) => {
			if (isEnabled) {
				setIsScrolled(startY - (event$1.touches?.[0]?.clientY ?? 0) > 0);
				containerElement?.removeEventListener("touchmove", onMove);
			}
		};
		containerElement?.addEventListener("touchmove", onMove);
	}, [isEnabled, containerElement]);
	const onScroll = useCallback((e) => {
		const isScrolledDown = e.deltaY > 0;
		if (isEnabled && !isScrollBlocked && isScrollable) {
			if (Math.abs(e.deltaY) > Math.abs(delta)) if (isScrolledDown) onScrollDown?.(e);
			else onScrollUp?.(e);
			setIsScrolled(window.scrollY > deltaFromTop);
		}
	}, [
		isEnabled,
		isScrollBlocked,
		isScrollable
	]);
	useEffect(() => {
		if (isEnabled) {
			containerElement?.addEventListener("wheel", onScroll, { passive: true });
			containerElement?.addEventListener("touchstart", onMobileMove, { passive: true });
		} else setIsScrolled(false);
		return () => {
			containerElement?.removeEventListener("wheel", onScroll);
			containerElement?.removeEventListener("touchstart", onMobileMove);
		};
	}, [
		isEnabled,
		onScroll,
		onMobileMove,
		containerElement
	]);
	useEffect(() => {
		setIsScrollable(window.innerHeight < window.document.body.scrollHeight);
	}, []);
	return {
		isScrolled,
		isScrollable
	};
};

//#endregion
export { useScrollDetection };
//# sourceMappingURL=useScrollDetection.mjs.map