package com.applemand.zmanim.core.ui

import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type

/**
 * ↑↓←→ + Enter / Numpad Enter D-pad navigation modifier.
 * Mirrors useKeyboardNavigation.js behavior.
 */
fun Modifier.keyboardCalendarNav(
    onPrev: () -> Unit,
    onNext: () -> Unit,
    onToday: () -> Unit,
    onSelect: () -> Unit,
): Modifier = composed {
    onPreviewKeyEvent { event ->
        if (event.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false
        when (event.key) {
            Key.DirectionLeft, Key.DirectionUp -> { onPrev(); true }
            Key.DirectionRight, Key.DirectionDown -> { onNext(); true }
            Key.T -> { onToday(); true }
            Key.Enter, Key.NumPadEnter, Key.DirectionCenter -> { onSelect(); true }
            else -> false
        }
    }
}
