/**
 * useGestureResolver Hook
 *
 * Convenience hook that provides a bound `resolve(descriptor)` function
 * using the gesture rules and palm rejection settings from the store.
 */

import { useAtomValue } from 'jotai';
import { resolveGestureIndexed, type GestureDescriptor, type ResolvedGesture } from '../core/gesture-rules';
import { gestureRuleIndexAtom, palmRejectionEnabledAtom } from '../core/gesture-rule-store';
import { isStylusActiveAtom } from '../core/input-store';

/**
 * Returns a `resolve` function that resolves gesture descriptors against
 * the current rule set, with palm rejection and stylus awareness built in.
 *
 * Usage:
 * ```tsx
 * const resolve = useGestureResolver();
 * const result = resolve({ gesture: 'drag', target: 'node', source: 'mouse' });
 * if (result?.actionId === 'move-node') { ... }
 * ```
 */
export function useGestureResolver() {
	const index = useAtomValue(gestureRuleIndexAtom);
	const palmRejection = useAtomValue(palmRejectionEnabledAtom);
	const isStylusActive = useAtomValue(isStylusActiveAtom);

	return (desc: Omit<GestureDescriptor, 'isStylusActive'> & { isStylusActive?: boolean }): ResolvedGesture | null => {
		return resolveGestureIndexed(
			{ ...desc, isStylusActive: desc.isStylusActive ?? isStylusActive },
			index,
			{ palmRejection },
		);
	};
}

