import { hashProjectPath } from '../storage/base-store.js';
import * as sessionStore from '../storage/session-store.js';
import { diffSessions, formatDiffSummary } from '../engine/session/session-diff.js';
export async function handleRestoreSession(params) {
    const projectId = hashProjectPath(params.projectPath);
    try {
        let session;
        if (params.sessionId) {
            session = await sessionStore.load(projectId, params.sessionId);
        }
        else {
            session = await sessionStore.getLatest(projectId);
        }
        if (!session) {
            return {
                content: [
                    {
                        type: 'text',
                        text: JSON.stringify({
                            message: 'No sessions found for this project. Start working and sessions will be saved automatically.',
                            projectId,
                        }, null, 2),
                    },
                ],
            };
        }
        // Try to compute diff with the second-latest session
        let diffSummary = '';
        const allSessions = await sessionStore.list(projectId);
        if (allSessions.length >= 2) {
            const previous = allSessions[1];
            if (previous) {
                const diff = diffSessions(previous, session);
                diffSummary = formatDiffSummary(diff);
            }
        }
        const result = {
            sessionId: session.id,
            startedAt: session.startedAt,
            lastSavedAt: session.lastSavedAt,
            contextSummary: session.contextSummary,
            activeSpecs: session.activeSpecs,
            pendingDecisions: session.decisions.slice(-5),
            toolCallCount: session.toolCallCount,
            diffSummary: diffSummary || undefined,
            message: 'Session restored successfully.',
        };
        return {
            content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
        };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `Error restoring session: ${message}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=restore-session.js.map