#!/usr/bin/env node
import {
  init_esm_shims
} from "./chunk-DHET7RCE.js";

// src/cli/components/hooks/use-keyboard-navigation.ts
init_esm_shims();
import { useState, useCallback, useRef, useEffect } from "react";
import { useInput } from "ink";
function useKeyboardNavigation(itemCount, handlers = {}, options = {}) {
  const { onEnter, onEscape } = handlers;
  const { wrap = true, vimKeys = true, active = true } = options;
  const [focusedIndex, setFocusedIndex] = useState(0);
  const focusedIndexRef = useRef(focusedIndex);
  useEffect(() => {
    focusedIndexRef.current = focusedIndex;
  }, [focusedIndex]);
  const moveUp = useCallback(() => {
    setFocusedIndex((prev) => {
      if (wrap) {
        return (prev - 1 + itemCount) % itemCount;
      }
      return Math.max(0, prev - 1);
    });
  }, [itemCount, wrap]);
  const moveDown = useCallback(() => {
    setFocusedIndex((prev) => {
      if (wrap) {
        return (prev + 1) % itemCount;
      }
      return Math.min(itemCount - 1, prev + 1);
    });
  }, [itemCount, wrap]);
  useInput(
    useCallback(
      (input, key) => {
        if (key.escape) {
          onEscape?.();
          return;
        }
        if (key.return) {
          onEnter?.(focusedIndexRef.current);
          return;
        }
        if (key.upArrow || vimKeys && input === "k") {
          moveUp();
          return;
        }
        if (key.downArrow || vimKeys && input === "j") {
          moveDown();
        }
      },
      [onEnter, onEscape, vimKeys, moveUp, moveDown]
    ),
    { isActive: active }
  );
  return { focusedIndex, setFocusedIndex };
}

export {
  useKeyboardNavigation
};
//# sourceMappingURL=chunk-KUV24B5M.js.map