#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TreeShellKeyboardIntent {
    MoveNext,
    MovePrevious,
    MoveHome,
    MoveEnd,
    Expand,
    Collapse,
    Open,
    ContextMenu,
    None,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TreeShellKeyboardModifiers {
    pub shift_key: bool,
    pub ctrl_key: bool,
    pub meta_key: bool,
}

pub fn resolve_tree_shell_keyboard_intent(
    key: &str,
    modifiers: TreeShellKeyboardModifiers,
) -> TreeShellKeyboardIntent {
    match key {
        "ArrowDown" => TreeShellKeyboardIntent::MoveNext,
        "ArrowUp" => TreeShellKeyboardIntent::MovePrevious,
        "Home" => TreeShellKeyboardIntent::MoveHome,
        "End" => TreeShellKeyboardIntent::MoveEnd,
        "ArrowRight" => TreeShellKeyboardIntent::Expand,
        "ArrowLeft" => TreeShellKeyboardIntent::Collapse,
        "Enter" => TreeShellKeyboardIntent::Open,
        "ContextMenu" => TreeShellKeyboardIntent::ContextMenu,
        "F10" if modifiers.shift_key && !modifiers.ctrl_key && !modifiers.meta_key => {
            TreeShellKeyboardIntent::ContextMenu
        }
        _ => TreeShellKeyboardIntent::None,
    }
}

#[cfg(test)]
mod tests {
    use super::{
        resolve_tree_shell_keyboard_intent, TreeShellKeyboardIntent, TreeShellKeyboardModifiers,
    };

    #[test]
    fn tree_shell_keyboard_state_maps_navigation_open_and_context_menu_intents() {
        assert_eq!(
            resolve_tree_shell_keyboard_intent("ArrowDown", TreeShellKeyboardModifiers::default()),
            TreeShellKeyboardIntent::MoveNext
        );
        assert_eq!(
            resolve_tree_shell_keyboard_intent("ArrowUp", TreeShellKeyboardModifiers::default()),
            TreeShellKeyboardIntent::MovePrevious
        );
        assert_eq!(
            resolve_tree_shell_keyboard_intent("Home", TreeShellKeyboardModifiers::default()),
            TreeShellKeyboardIntent::MoveHome
        );
        assert_eq!(
            resolve_tree_shell_keyboard_intent("End", TreeShellKeyboardModifiers::default()),
            TreeShellKeyboardIntent::MoveEnd
        );
        assert_eq!(
            resolve_tree_shell_keyboard_intent("Enter", TreeShellKeyboardModifiers::default()),
            TreeShellKeyboardIntent::Open
        );
        assert_eq!(
            resolve_tree_shell_keyboard_intent(
                "F10",
                TreeShellKeyboardModifiers {
                    shift_key: true,
                    ..TreeShellKeyboardModifiers::default()
                },
            ),
            TreeShellKeyboardIntent::ContextMenu
        );
    }
}
