/**
 * 4Runr OS State Management
 * Memory and shortcuts for power-user experience
 */
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
const STATE_FILE = path.join(os.homedir(), '.4runr', 'state.json');
function getStatePath() {
    const dir = path.dirname(STATE_FILE);
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
    }
    return STATE_FILE;
}
export function loadState() {
    try {
        const statePath = getStatePath();
        if (fs.existsSync(statePath)) {
            const data = fs.readFileSync(statePath, 'utf-8');
            return JSON.parse(data);
        }
    }
    catch (error) {
        // Ignore errors, return empty state
    }
    return {};
}
export function saveState(state) {
    try {
        const statePath = getStatePath();
        fs.writeFileSync(statePath, JSON.stringify(state, null, 2));
    }
    catch (error) {
        // Ignore errors, state is best-effort
    }
}
export function updateState(updates) {
    const current = loadState();
    const updated = { ...current, ...updates };
    saveState(updated);
}
export function getLastAgent() {
    return loadState().lastAgent;
}
export function setLastAgent(agentName) {
    updateState({ lastAgent: agentName });
}
export function getLastRun() {
    return loadState().lastRun;
}
export function setLastRun(runId) {
    updateState({ lastRun: runId });
}
export function getLastConnectionTarget() {
    return loadState().lastConnectionTarget;
}
export function setLastConnectionTarget(target) {
    updateState({ lastConnectionTarget: target });
}
export function getLastMode() {
    return loadState().lastMode;
}
export function setLastMode(mode) {
    updateState({ lastMode: mode });
}
/**
 * Check if user is in zero state (first-time experience)
 * Note: This is a simplified check - in practice, we'd need the current user context
 */
export function isZeroState() {
    // Check default agents path (no user context available here)
    const homeDir = os.homedir();
    const defaultAgentsPath = path.join(homeDir, '.4runr', 'agents.json');
    if (!fs.existsSync(defaultAgentsPath)) {
        return true;
    }
    try {
        const data = fs.readFileSync(defaultAgentsPath, 'utf-8');
        const agents = JSON.parse(data);
        return !agents || agents.length === 0;
    }
    catch {
        return true;
    }
}
//# sourceMappingURL=state.js.map