/**
 * Shared keyboard navigation for single-column option lists (comboboxes,
 * multi-selects, typeaheads). Pure index math so it can be unit-tested in the
 * node test environment without a DOM. Callers own open/close, Enter, Escape
 * and the visual highlight; this only resolves the next highlighted index for
 * ArrowUp / ArrowDown.
 *
 * @param key      KeyboardEvent.key
 * @param current  current highlighted index (-1 = none)
 * @param count    number of navigable options
 * @returns next index, or null when the key is not an arrow / there is nothing to navigate
 */
export function nextHighlightIndex(
  key: string,
  current: number,
  count: number,
): number | null {
  if (count <= 0) return null
  switch (key) {
    case "ArrowDown":
      return Math.min(current + 1, count - 1)
    case "ArrowUp":
      return Math.max(current - 1, 0)
    default:
      return null
  }
}
