// ---------------------------------------------------------------------------
// Reducer
// ---------------------------------------------------------------------------
/** Initial idle state for the agent setup reducer. */
export const INITIAL_STATE = {
    status: "idle",
    error: null,
};
/**
 * Pure reducer for the agent setup state machine.
 *
 * Transitions through `idle → resolving → needsEnvVars → submitting → ready`,
 * with error handling orthogonal to the current phase.
 */
export function agentSetupReducer(state, action) {
    switch (action.type) {
        case "RESOLVE_START":
            return { status: "resolving", agentRef: action.agentRef, error: null };
        case "RESOLVE_NEEDS_ENV":
            return {
                status: "needsEnvVars",
                agentRef: action.agentRef,
                agentId: action.agentId,
                agentName: action.agentName,
                missingVariables: action.missingVariables,
                error: null,
            };
        case "RESOLVE_READY":
            return {
                status: "ready",
                agentRef: action.agentRef,
                agentName: action.agentName,
                resolution: action.resolution,
                error: null,
            };
        case "POOL_RESOLVE": {
            if (state.status !== "needsEnvVars")
                return state;
            if (action.missingVariables.length === 0) {
                return {
                    status: "ready",
                    agentRef: state.agentRef,
                    agentName: state.agentName,
                    resolution: { mode: "direct" },
                    error: null,
                };
            }
            return {
                ...state,
                missingVariables: action.missingVariables,
            };
        }
        case "SUBMIT_START": {
            if (state.status !== "needsEnvVars")
                return state;
            return {
                status: "submitting",
                agentRef: state.agentRef,
                agentId: state.agentId,
                agentName: state.agentName,
                missingVariables: state.missingVariables,
                error: null,
            };
        }
        case "SUBMIT_READY":
            return {
                status: "ready",
                agentRef: action.agentRef,
                agentName: action.agentName,
                resolution: action.resolution,
                error: null,
            };
        case "ERROR":
            if (state.status === "submitting") {
                return {
                    status: "needsEnvVars",
                    agentRef: state.agentRef,
                    agentId: state.agentId,
                    agentName: state.agentName,
                    missingVariables: state.missingVariables,
                    error: action.error,
                };
            }
            return { ...state, error: action.error };
        case "CLEAR_ERROR":
            return { ...state, error: null };
        case "RESET":
            return INITIAL_STATE;
        default:
            return state;
    }
}
//# sourceMappingURL=agentSetupReducer.js.map