/**
 * Switch to a different session.
 *
 * @task T4782
 * @epic T4654
 */
import { ExitCode } from '@cleocode/contracts';
import { CleoError } from '../errors.js';
import { getAccessor } from '../store/data-accessor.js';
/**
 * Switch to a different session.
 * Suspends the current active session and activates the target.
 * Throws if session not found or archived.
 */
export async function switchSession(projectRoot, sessionId) {
    const accessor = await getAccessor(projectRoot);
    const sessions = await accessor.loadSessions();
    // Find target session
    const targetSession = sessions.find((s) => s.id === sessionId);
    if (!targetSession) {
        throw new CleoError(ExitCode.SESSION_NOT_FOUND, `Session '${sessionId}' not found`);
    }
    if (targetSession.status === 'archived') {
        throw new CleoError(ExitCode.SESSION_NOT_FOUND, `Session '${sessionId}' is archived and cannot be switched to`);
    }
    const now = new Date().toISOString();
    // Suspend the current active session (if different from target)
    const currentActive = await accessor.getActiveSession();
    if (currentActive && currentActive.id !== sessionId) {
        currentActive.status = 'suspended';
        Object.assign(currentActive, { suspendedAt: now });
        if (currentActive.stats) {
            currentActive.stats.suspendCount = (currentActive.stats.suspendCount || 0) + 1;
        }
        await accessor.upsertSingleSession(currentActive);
    }
    // Activate the target session
    targetSession.status = 'active';
    Object.assign(targetSession, { suspendedAt: null });
    targetSession.endedAt = undefined;
    targetSession.resumeCount = (targetSession.resumeCount || 0) + 1;
    // Update focus if target session has a task
    if (targetSession.taskWork?.taskId) {
        const focus = (await accessor.getMetaValue('focus_state')) ?? {};
        focus.currentTask = targetSession.taskWork.taskId;
        await accessor.setMetaValue('focus_state', focus);
    }
    await accessor.upsertSingleSession(targetSession);
    return targetSession;
}
//# sourceMappingURL=session-switch.js.map