// super = parent module (app). Import Action and AppState from sibling modules.
use super::{actions::Action, state::AppState};
use crate::app::reducers;

/// Pure-ish reducer: (state, action) -> new state
pub fn reducer(state: AppState, action: Action) -> AppState {
    // Try domain-specific reducers; the first one that handles the action returns Some(state)
    if let Some(state) = reducers::ui_reducer::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::status::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::diff_reducer::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::branch_reducer::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::commit_reducer::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::stash_reducer::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::tag_reducer::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::reflog_reducer::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::palette_reducer::reduce(state.clone(), &action) { return state; }
    if let Some(state) = reducers::async_reducer::reduce(state.clone(), &action) { return state; }

    // Actions handled elsewhere (command layer or components) fall through.
    match action {
        Action::StageSelectedFile
        | Action::UnstageSelectedFile
        | Action::StageAllFiles
        | Action::UnstageAllFiles
        | Action::CommitSubmit
        | Action::AmendCommit
        | Action::Push
        | Action::CherryPickSelected
        | Action::RevertSelected
        | Action::RebaseContinue
        | Action::RebaseAbort
        |         Action::ResetHard
        | Action::ResetMixed
        | Action::ResetSoft
        | Action::RequestResetHard
        | Action::RequestResetMixed
        | Action::RequestResetSoft
        | Action::CancelReset
        | Action::StageCurrentHunk
        | Action::UnstageCurrentHunk
        | Action::StageSelectedLines
        | Action::UnstageSelectedLines
        | Action::RefreshBranches
        | Action::CheckoutBranch(_)
        | Action::CheckoutRemoteBranch(_)
        | Action::DeleteBranch(_)
        | Action::MergeBranch(_)
        | Action::RebaseBranch(_)
        | Action::RefreshStatus
        | Action::RequestRefreshStatus
        | Action::RefreshCommits
        | Action::ShowCommitDetail(_)
        | Action::CherryPickCommit(_)
        | Action::RevertCommit(_)
        | Action::RefreshStashes
        | Action::ApplyStash(_)
        | Action::PopStash(_)
        | Action::RefreshTags
        | Action::CheckoutTag(_)
        | Action::DeleteTag(_)
        | Action::RefreshReflog
        | Action::CheckoutReflog(_)
        | Action::BranchSubmit => state,

        // Fallback: return unchanged state
        _ => state,
    }
}