export type AutocompleteKeyDecision = | { action: 'none' } | { action: 'escape' } | { action: 'move'; nextIndex: number } | { action: 'select'; index: number } export function decideAutocompleteKeyAction(params: { key: string selectedIndex: number suggestionsCount: number }): AutocompleteKeyDecision { if (params.key === 'Escape') { return { action: 'escape' } } if (params.key === 'ArrowDown') { const nextIndex = params.selectedIndex < params.suggestionsCount - 1 ? params.selectedIndex + 1 : params.selectedIndex return { action: 'move', nextIndex } } if (params.key === 'ArrowUp') { const nextIndex = params.selectedIndex > 0 ? params.selectedIndex - 1 : -1 return { action: 'move', nextIndex } } if (params.key === 'Enter' && params.selectedIndex >= 0) { return { action: 'select', index: params.selectedIndex } } return { action: 'none' } }