use crate::app::{actions::Action, state::AppState};

pub fn reduce(mut state: AppState, action: &Action) -> Option<AppState> {
    match action {
        Action::SetReflog(entries) => {
            state.reflog_entries = entries.clone();
            if state.reflog_selected >= state.reflog_entries.len() && !state.reflog_entries.is_empty() {
                state.reflog_selected = state.reflog_entries.len() - 1;
            }
        }
        Action::ReflogUp => {
            if state.reflog_selected > 0 {
                state.reflog_selected -= 1;
            }
        }
        Action::ReflogDown => {
            if !state.reflog_entries.is_empty() && state.reflog_selected < state.reflog_entries.len() - 1 {
                state.reflog_selected += 1;
            }
        }
        _ => return None,
    }
    Some(state)
}
