// ---------------------------------------------------------------------------
// Defaults
// ---------------------------------------------------------------------------
const EMPTY_GIT = { staged: [], unstaged: [], branch: 'unknown' };
const EMPTY_PIPELINE = { typecheck: false, lint: false, tests: false };
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/** Build a session checkpoint from available data. */
export function generateCheckpoint(projectId, contextUsage, options) {
    return {
        timestamp: new Date().toISOString(),
        projectId,
        summary: options?.summary ?? buildAutoSummary(contextUsage),
        decisions: options?.decisions ?? [],
        filesModified: options?.filesModified ?? [],
        filesRead: options?.filesRead ?? [],
        specsInProgress: options?.specsInProgress ?? [],
        pendingWork: options?.pendingWork ?? [],
        gitStatus: options?.gitStatus ?? EMPTY_GIT,
        pipelineStatus: options?.pipelineStatus ?? EMPTY_PIPELINE,
        contextUsage,
    };
}
/** Format a checkpoint as human-readable Markdown. */
export function formatCheckpoint(cp) {
    const lines = [
        `# Session Checkpoint`,
        '',
        `**Project:** ${cp.projectId}`,
        `**Timestamp:** ${cp.timestamp}`,
        `**Context usage:** ${String(cp.contextUsage.percentUsed)}% (${cp.contextUsage.alertLevel})`,
        '',
        `## Summary`,
        cp.summary,
        '',
    ];
    if (cp.decisions.length > 0) {
        lines.push('## Decisions', ...cp.decisions.map((d) => `- ${d}`), '');
    }
    if (cp.filesModified.length > 0) {
        lines.push('## Files Modified', ...cp.filesModified.map((f) => `- ${f}`), '');
    }
    if (cp.filesRead.length > 0) {
        lines.push('## Files Read', ...cp.filesRead.map((f) => `- ${f}`), '');
    }
    if (cp.specsInProgress.length > 0) {
        lines.push('## Specs In Progress', ...cp.specsInProgress.map((s) => `- ${s}`), '');
    }
    if (cp.pendingWork.length > 0) {
        lines.push('## Pending Work', ...cp.pendingWork.map((w) => `- ${w}`), '');
    }
    lines.push('## Git Status', `Branch: ${cp.gitStatus.branch}`, `Staged: ${cp.gitStatus.staged.length > 0 ? cp.gitStatus.staged.join(', ') : 'none'}`, `Unstaged: ${cp.gitStatus.unstaged.length > 0 ? cp.gitStatus.unstaged.join(', ') : 'none'}`, '', '## Pipeline', `- Typecheck: ${cp.pipelineStatus.typecheck ? 'PASS' : 'PENDING'}`, `- Lint: ${cp.pipelineStatus.lint ? 'PASS' : 'PENDING'}`, `- Tests: ${cp.pipelineStatus.tests ? 'PASS' : 'PENDING'}`, '', '## Context Usage', `- Tokens used: ${String(cp.contextUsage.tokensEstimatedUsed)}`, `- Tokens available: ${String(cp.contextUsage.tokensEstimatedAvailable)}`, `- Operations: ${String(cp.contextUsage.operationCount)}`);
    if (cp.contextUsage.topConsumers.length > 0) {
        lines.push('- Top consumers:', ...cp.contextUsage.topConsumers.map((c) => `  - ${c.tool}: ${String(c.tokens)} tokens`));
    }
    return lines.join('\n');
}
/** Extract a one-line summary from a checkpoint. */
export function parseCheckpointSummary(cp) {
    const parts = [
        `${String(cp.contextUsage.percentUsed)}% context`,
        `${String(cp.contextUsage.operationCount)} ops`,
    ];
    if (cp.filesModified.length > 0) {
        parts.push(`${String(cp.filesModified.length)} files modified`);
    }
    if (cp.specsInProgress.length > 0) {
        parts.push(`specs: ${cp.specsInProgress.join(', ')}`);
    }
    return parts.join(' | ');
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
function buildAutoSummary(usage) {
    return `Auto-checkpoint at ${String(usage.percentUsed)}% context usage (${String(usage.operationCount)} operations, ${String(usage.tokensEstimatedUsed)} tokens estimated).`;
}
//# sourceMappingURL=checkpoint.js.map