import { randomUUID } from "node:crypto";
// ─── State Factory ─────────────────────────────────────────────────
export function createInitialState(userGoal, maxIterations = 3) {
    return {
        sessionId: randomUUID(),
        userGoal,
        currentPhase: "init",
        environment: {
            os: process.platform === "win32"
                ? "windows"
                : process.platform === "darwin"
                    ? "darwin"
                    : "linux",
            route: "native",
            toolchain: [],
        },
        executionPlan: [],
        currentStepIndex: 0,
        history: [],
        errors: [],
        reflections: [],
        iterationCount: 0,
        maxIterations,
        startedAt: new Date().toISOString(),
    };
}
// ─── Immutable State Reducers ──────────────────────────────────────
// Every reducer returns a NEW state object. Never mutate in place.
export function setPhase(state, phase) {
    return { ...state, currentPhase: phase };
}
export function setEnvironment(state, env) {
    return { ...state, environment: env };
}
export function setPlan(state, plan) {
    return {
        ...state,
        executionPlan: plan,
        currentStepIndex: 0,
    };
}
export function advanceStep(state) {
    return { ...state, currentStepIndex: state.currentStepIndex + 1 };
}
export function updateStepStatus(state, stepId, status, result, error) {
    return {
        ...state,
        executionPlan: state.executionPlan.map((s) => s.id === stepId ? { ...s, status, result, error } : s),
    };
}
export function appendHistory(state, entry) {
    return {
        ...state,
        history: [
            ...state.history,
            { ...entry, timestamp: new Date().toISOString() },
        ],
    };
}
export function appendError(state, error) {
    return {
        ...state,
        errors: [
            ...state.errors,
            { ...error, timestamp: new Date().toISOString() },
        ],
    };
}
export function appendReflection(state, reflection) {
    return {
        ...state,
        reflections: [
            ...state.reflections,
            { ...reflection, timestamp: new Date().toISOString() },
        ],
    };
}
export function incrementIteration(state) {
    return { ...state, iterationCount: state.iterationCount + 1 };
}
export function markComplete(state) {
    return {
        ...state,
        currentPhase: "complete",
        completedAt: new Date().toISOString(),
    };
}
export function markFailed(state, reason) {
    return {
        ...state,
        currentPhase: "failed",
        completedAt: new Date().toISOString(),
        errors: [
            ...state.errors,
            {
                timestamp: new Date().toISOString(),
                message: reason,
                category: "AGENT_FAILED",
            },
        ],
    };
}
//# sourceMappingURL=state.js.map