"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInitialState = createInitialState;
exports.setPhase = setPhase;
exports.setEnvironment = setEnvironment;
exports.setPlan = setPlan;
exports.advanceStep = advanceStep;
exports.updateStepStatus = updateStepStatus;
exports.appendHistory = appendHistory;
exports.appendError = appendError;
exports.appendReflection = appendReflection;
exports.incrementIteration = incrementIteration;
exports.markComplete = markComplete;
exports.markFailed = markFailed;
const node_crypto_1 = require("node:crypto");
// ─── State Factory ─────────────────────────────────────────────────
function createInitialState(userGoal, maxIterations = 3) {
    return {
        sessionId: (0, node_crypto_1.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.
function setPhase(state, phase) {
    return { ...state, currentPhase: phase };
}
function setEnvironment(state, env) {
    return { ...state, environment: env };
}
function setPlan(state, plan) {
    return {
        ...state,
        executionPlan: plan,
        currentStepIndex: 0,
    };
}
function advanceStep(state) {
    return { ...state, currentStepIndex: state.currentStepIndex + 1 };
}
function updateStepStatus(state, stepId, status, result, error) {
    return {
        ...state,
        executionPlan: state.executionPlan.map((s) => s.id === stepId ? { ...s, status, result, error } : s),
    };
}
function appendHistory(state, entry) {
    return {
        ...state,
        history: [
            ...state.history,
            { ...entry, timestamp: new Date().toISOString() },
        ],
    };
}
function appendError(state, error) {
    return {
        ...state,
        errors: [
            ...state.errors,
            { ...error, timestamp: new Date().toISOString() },
        ],
    };
}
function appendReflection(state, reflection) {
    return {
        ...state,
        reflections: [
            ...state.reflections,
            { ...reflection, timestamp: new Date().toISOString() },
        ],
    };
}
function incrementIteration(state) {
    return { ...state, iterationCount: state.iterationCount + 1 };
}
function markComplete(state) {
    return {
        ...state,
        currentPhase: "complete",
        completedAt: new Date().toISOString(),
    };
}
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