'use client';

import { useEffect, useRef, useState } from "react";

//#region src/hooks/useHorizontalSwipe.ts
const useHorizontalSwipe = (config) => {
	const { enable = true, onSwipeLeft, onSwipeRight, itemIndex, itemCount, thresholdPct = 20, touchAction = "pan-y", disableWhenSelectingText = true } = config;
	const containerRef = useRef(null);
	const [isDragging, setIsDragging] = useState(false);
	const [dragDeltaPct, setDragDeltaPct] = useState(0);
	const pointerStateRef = useRef({
		pointerId: null,
		startX: 0,
		startY: 0,
		lastX: 0,
		axisLocked: false,
		isHorizontal: false
	});
	const isTextSelectingRef = useRef(false);
	const getContainerWidth = () => {
		const node = containerRef.current;
		return node ? node.clientWidth : 0;
	};
	const applyEdgeResistance = (pct) => {
		const atFirst = itemIndex <= 0;
		const atLast = itemIndex >= itemCount - 1;
		if (atFirst && pct > 0 || atLast && pct < 0) return pct / (1 + Math.abs(pct) / 30);
		return pct;
	};
	const onPointerDown = (e) => {
		if (!enable) return;
		if (pointerStateRef.current.pointerId !== null) return;
		pointerStateRef.current.pointerId = e.pointerId;
		pointerStateRef.current.startX = e.clientX;
		pointerStateRef.current.startY = e.clientY;
		pointerStateRef.current.lastX = e.clientX;
		pointerStateRef.current.axisLocked = false;
		pointerStateRef.current.isHorizontal = false;
		isTextSelectingRef.current = false;
		setIsDragging(false);
		setDragDeltaPct(0);
	};
	const onPointerMove = (e) => {
		if (!enable) return;
		if (pointerStateRef.current.pointerId !== e.pointerId) return;
		const width = getContainerWidth();
		if (!width) return;
		const dx = e.clientX - pointerStateRef.current.startX;
		const dy = e.clientY - pointerStateRef.current.startY;
		if (disableWhenSelectingText) {
			const sel = typeof window !== "undefined" ? window.getSelection?.() : null;
			if (!!sel && sel.rangeCount > 0 && !sel.isCollapsed) {
				isTextSelectingRef.current = true;
				return;
			}
		}
		if (!pointerStateRef.current.axisLocked) {
			const absDx = Math.abs(dx);
			const absDy = Math.abs(dy);
			if (absDx > 6 || absDy > 6) {
				pointerStateRef.current.axisLocked = true;
				pointerStateRef.current.isHorizontal = absDx > absDy;
			}
		}
		if (!pointerStateRef.current.isHorizontal) return;
		try {
			e.currentTarget.setPointerCapture(e.pointerId);
		} catch {}
		e.preventDefault();
		if (!isDragging) setIsDragging(true);
		setDragDeltaPct(applyEdgeResistance(dx / width * 100));
		pointerStateRef.current.lastX = e.clientX;
	};
	const endDrag = (e) => {
		try {
			e.currentTarget.releasePointerCapture(e.pointerId);
		} catch {}
		const wasHorizontal = pointerStateRef.current.isHorizontal;
		const delta = dragDeltaPct;
		setIsDragging(false);
		setDragDeltaPct(0);
		pointerStateRef.current.pointerId = null;
		pointerStateRef.current.axisLocked = false;
		pointerStateRef.current.isHorizontal = false;
		if (isTextSelectingRef.current) {
			isTextSelectingRef.current = false;
			return;
		}
		if (!wasHorizontal) return;
		if (delta > thresholdPct) onSwipeRight?.();
		else if (delta < -thresholdPct) onSwipeLeft?.();
	};
	const onPointerUp = (e) => {
		if (!enable) return;
		if (pointerStateRef.current.pointerId !== e.pointerId) return;
		endDrag(e);
	};
	const onPointerCancel = (e) => {
		if (!enable) return;
		if (pointerStateRef.current.pointerId !== e.pointerId) return;
		endDrag(e);
	};
	useEffect(() => {
		setDragDeltaPct(0);
		setIsDragging(false);
	}, [itemIndex]);
	return {
		containerRef,
		isDragging,
		dragDeltaPct,
		containerProps: {
			ref: containerRef,
			onPointerDown,
			onPointerMove,
			onPointerUp,
			onPointerCancel,
			style: { touchAction }
		}
	};
};

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