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

// src/cli/components/hooks/use-focused-list-item.ts
init_esm_shims();
import { useState, useCallback, useRef } from "react";
function useFocusedListItem(rowCount, getColCount, options = {}) {
  const {
    wrap = true,
    isRowLocked,
    findValidCol,
    adjustCol,
    onChange,
    initialRow = 0,
    initialCol = 0
  } = options;
  const [focusedRow, setFocusedRow] = useState(initialRow);
  const [focusedCol, setFocusedCol] = useState(initialCol);
  const focusedRowRef = useRef(focusedRow);
  focusedRowRef.current = focusedRow;
  const focusedColRef = useRef(focusedCol);
  focusedColRef.current = focusedCol;
  const onChangeRef = useRef(onChange);
  onChangeRef.current = onChange;
  const applyFocus = useCallback((row, col) => {
    setFocusedRow(row);
    setFocusedCol(col);
    onChangeRef.current?.(row, col);
  }, []);
  const setFocused = applyFocus;
  const findNextUnlockedRow = useCallback(
    (fromRow, direction) => {
      if (!isRowLocked || rowCount === 0) {
        if (wrap) {
          return (fromRow + direction + rowCount) % rowCount;
        }
        const next = fromRow + direction;
        return Math.max(0, Math.min(rowCount - 1, next));
      }
      let index = fromRow;
      let attempts = 0;
      while (attempts < rowCount) {
        index += direction;
        if (wrap) {
          if (index < 0) index = rowCount - 1;
          if (index >= rowCount) index = 0;
        } else {
          if (index < 0) index = 0;
          if (index >= rowCount) index = rowCount - 1;
        }
        if (!isRowLocked(index)) {
          return index;
        }
        attempts++;
      }
      return fromRow;
    },
    [rowCount, wrap, isRowLocked]
  );
  const moveFocus = useCallback(
    (direction) => {
      const currentRow = focusedRowRef.current;
      const currentCol = focusedColRef.current;
      if (direction === "left" || direction === "right") {
        const colCount = getColCount(currentRow);
        if (colCount === 0) return;
        const step = direction === "right" ? 1 : -1;
        if (findValidCol) {
          const newCol = findValidCol(currentRow, currentCol, step);
          applyFocus(currentRow, newCol);
        } else if (wrap) {
          const newCol = (currentCol + step + colCount) % colCount;
          applyFocus(currentRow, newCol);
        } else {
          const newCol = Math.max(0, Math.min(colCount - 1, currentCol + step));
          applyFocus(currentRow, newCol);
        }
      } else {
        const step = direction === "down" ? 1 : -1;
        const newRow = findNextUnlockedRow(currentRow, step);
        const newRowColCount = getColCount(newRow);
        let finalCol = Math.min(currentCol, Math.max(0, newRowColCount - 1));
        if (adjustCol) {
          finalCol = adjustCol(newRow, finalCol);
        }
        applyFocus(newRow, finalCol);
      }
    },
    [getColCount, wrap, findValidCol, adjustCol, findNextUnlockedRow, applyFocus]
  );
  return { focusedRow, focusedCol, setFocused, moveFocus };
}

export {
  useFocusedListItem
};
//# sourceMappingURL=chunk-GG4BSB6S.js.map