import { hashProjectPath } from '../storage/base-store.js';
import * as sessionStore from '../storage/session-store.js';
function formatAsMarkdown(session) {
    const lines = [];
    lines.push(`# Session Export: ${session.id}`);
    lines.push('');
    lines.push(`## Summary`);
    lines.push(`- **Project**: ${session.projectId}`);
    lines.push(`- **Started**: ${session.startedAt}`);
    lines.push(`- **Last Saved**: ${session.lastSavedAt}`);
    lines.push(`- **Tool Calls**: ${String(session.toolCallCount)}`);
    lines.push('');
    if (session.activeSpecs.length > 0) {
        lines.push('## Active Specs');
        for (const spec of session.activeSpecs) {
            lines.push(`- **${spec.specId}** (${spec.status}): ${spec.summary}`);
        }
        lines.push('');
    }
    if (session.decisions.length > 0) {
        lines.push('## Decisions');
        for (const d of session.decisions) {
            const specRef = d.specId ? ` [${d.specId}]` : '';
            lines.push(`- **${d.decision}**${specRef}`);
            if (d.rationale) {
                lines.push(`  - _${d.rationale}_`);
            }
        }
        lines.push('');
    }
    if (session.modifiedFiles.length > 0) {
        lines.push('## Modified Files');
        for (const f of session.modifiedFiles) {
            lines.push(`- \`${f.path}\` (${f.action})`);
        }
        lines.push('');
    }
    if (Object.keys(session.metadata).length > 0) {
        lines.push('## Metadata');
        lines.push('```json');
        lines.push(JSON.stringify(session.metadata, null, 2));
        lines.push('```');
    }
    return lines.join('\n');
}
export async function handleExportSession(params) {
    const projectId = hashProjectPath(params.projectPath);
    const format = params.format ?? 'markdown';
    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.',
                            projectId,
                        }, null, 2),
                    },
                ],
            };
        }
        if (format === 'json') {
            return {
                content: [{ type: 'text', text: JSON.stringify(session, null, 2) }],
            };
        }
        return {
            content: [{ type: 'text', text: formatAsMarkdown(session) }],
        };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `Error exporting session: ${message}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=export-session.js.map