use crate::app::{actions::Action, state::AppState};
pub fn reduce(mut state: AppState, action: &Action) -> Option<AppState> {
match action {
Action::SetBranches(branches) => {
state.branches = branches.clone();
if state.branch_selected >= state.branches.len() && !state.branches.is_empty() {
state.branch_selected = state.branches.len() - 1;
}
}
Action::SetBranchAheadBehind(branch_name, ahead, behind) => {
for branch in &mut state.branches {
if branch.name == *branch_name {
branch.ahead = Some(*ahead);
branch.behind = Some(*behind);
break;
}
}
}
Action::BranchUp => {
if state.branch_selected > 0 {
state.branch_selected -= 1;
}
}
Action::BranchDown => {
if !state.branches.is_empty() && state.branch_selected < state.branches.len() - 1 {
state.branch_selected += 1;
}
}
Action::SetBranchSelected(idx) => {
state.branch_selected = (*idx).min(state.branches.len().saturating_sub(1));
}
Action::StartBranchCreate => {
state.branch_create_mode = true;
state.branch_name_input.clear();
}
Action::CancelBranchCreate => {
state.branch_create_mode = false;
state.branch_name_input.clear();
}
Action::BranchInputChar(c) => {
state.branch_name_input.push(*c);
}
Action::BranchInputBackspace => {
state.branch_name_input.pop();
}
_ => return None,
}
Some(state)
}