import { o as KeyboardEventKey } from "./constants2.js";
import { t as useKeyboardEvent } from "./useKeyboardEvent.js";
import { useEffect } from "react";
const useRovingFocus = (handleNewIndex, inputRefList, containerRef, leftRightNavigation) => {
	leftRightNavigation = leftRightNavigation ?? false;
	useEffect(() => {
		if (!inputRefList.current) return;
		inputRefList.current.forEach((ref) => {
			ref.current?.setAttribute("tabIndex", "-1");
		});
		inputRefList.current[0]?.current?.setAttribute("tabIndex", "0");
	}, []);
	const handleKeyboardNavigation = (event) => {
		if (event.key === KeyboardEventKey.Tab) return;
		event.preventDefault();
		if (!inputRefList.current) return;
		const index = inputRefList.current.findIndex((x) => x.current === event.target);
		let nextIndex = index;
		if (event.key === KeyboardEventKey.Home) nextIndex = 0;
		else if (event.key === KeyboardEventKey.End) nextIndex = inputRefList.current.length - 1;
		else if ((event.key === KeyboardEventKey.ArrowDown || event.key === KeyboardEventKey.ArrowRight) && index < inputRefList.current.length - 1) nextIndex = index + 1;
		else if ((event.key === KeyboardEventKey.ArrowUp || event.key === KeyboardEventKey.ArrowLeft) && index > 0) nextIndex = index - 1;
		else if (event.key === KeyboardEventKey.Enter || event.key === KeyboardEventKey.Space) nextIndex = index;
		if (nextIndex !== -1) {
			inputRefList.current?.forEach((ref, i) => {
				if (ref.current) ref.current.tabIndex = i === nextIndex ? 0 : -1;
			});
			inputRefList.current[nextIndex]?.current?.focus();
			inputRefList.current[nextIndex]?.current?.click?.();
			handleNewIndex(nextIndex);
		}
	};
	const commonKeys = [
		KeyboardEventKey.End,
		KeyboardEventKey.Enter,
		KeyboardEventKey.Escape,
		KeyboardEventKey.Home
	];
	const arrowKeys = leftRightNavigation ? [KeyboardEventKey.ArrowRight, KeyboardEventKey.ArrowLeft] : [KeyboardEventKey.ArrowUp, KeyboardEventKey.ArrowDown];
	useKeyboardEvent(containerRef, handleKeyboardNavigation, [...commonKeys, ...arrowKeys]);
};
export { useRovingFocus as t };

//# sourceMappingURL=useRovingFocus.js.map