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

pub fn reduce(mut state: AppState, action: &Action) -> Option<AppState> {
    match action {
        Action::SetTags(tags) => {
            state.tags = tags.clone();
            if state.tag_selected >= state.tags.len() && !state.tags.is_empty() {
                state.tag_selected = state.tags.len() - 1;
            }
        }
        Action::TagUp => {
            if state.tag_selected > 0 {
                state.tag_selected -= 1;
            }
        }
        Action::TagDown => {
            if !state.tags.is_empty() && state.tag_selected < state.tags.len() - 1 {
                state.tag_selected += 1;
            }
        }
        _ => return None,
    }
    Some(state)
}
