import { useEffect } from "react";
//#region src/components/PisellScrollView/hooks/useIntersection.ts
const useIntersection = ({ containerRef, config }) => {
	useEffect(() => {
		if (!(config === null || config === void 0 ? void 0 : config.enabled) || !containerRef.current) return;
		const { threshold = 0, rootMargin = "0px", targetSelector, onEnterViewport, onLeaveViewport, onVisibilityChange } = config;
		const container = containerRef.current;
		const targets = targetSelector ? container.querySelectorAll(targetSelector) : container.children;
		if (!targets || targets.length === 0) return;
		const observer = new IntersectionObserver((entries) => {
			entries.forEach((entry) => {
				const element = entry.target;
				onVisibilityChange === null || onVisibilityChange === void 0 || onVisibilityChange(entry, element);
				if (entry.isIntersecting) onEnterViewport === null || onEnterViewport === void 0 || onEnterViewport(entry, element);
				else onLeaveViewport === null || onLeaveViewport === void 0 || onLeaveViewport(entry, element);
			});
		}, {
			root: container,
			rootMargin,
			threshold
		});
		Array.from(targets).forEach((target) => observer.observe(target));
		return () => {
			observer.disconnect();
		};
	}, [config, containerRef]);
};
//#endregion
export { useIntersection };
