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

pub fn reduce(mut state: AppState, action: &Action) -> Option<AppState> {
    match action {
        Action::SetStashes(list) => {
            state.stashes = list.clone();
            if state.stash_selected >= state.stashes.len() && !state.stashes.is_empty() {
                state.stash_selected = state.stashes.len() - 1;
            }
        }
        Action::StashUp => {
            if state.stash_selected > 0 {
                state.stash_selected -= 1;
            }
        }
        Action::StashDown => {
            if !state.stashes.is_empty() && state.stash_selected < state.stashes.len() - 1 {
                state.stash_selected += 1;
            }
        }
        Action::StartStashCreate => {
            state.stash_create_mode = true;
            state.stash_message_input.clear();
        }
        Action::CancelStashCreate => {
            state.stash_create_mode = false;
            state.stash_message_input.clear();
        }
        Action::StashInputChar(c) => {
            state.stash_message_input.push(*c);
        }
        Action::StashInputBackspace => {
            state.stash_message_input.pop();
        }
        _ => return None,
    }
    Some(state)
}
