// engine/usage-tracker/stats.ts — Usage statistics calculation (pure functions, no I/O).
// ---------------------------------------------------------------------------
// Stats calculation
// ---------------------------------------------------------------------------
/**
 * Computes full UsageStats from a set of events and runtime context.
 * All parameters are passed in — no I/O performed here.
 */
export function calculateUsageStats(events, activeSpecs, activeProjects, currentTier, daysSinceFirstRun, isTrialActive, remainingDailyCalls) {
    const callsByTool = {};
    const callsByTier = {};
    for (const event of events) {
        callsByTool[event.toolName] = (callsByTool[event.toolName] ?? 0) + 1;
        callsByTier[event.tier] = (callsByTier[event.tier] ?? 0) + 1;
    }
    const topTools = buildTopTools(callsByTool, 10);
    return {
        totalCalls: events.length,
        callsByTool,
        callsByTier,
        topTools,
        activeSpecs,
        activeProjects,
        currentTier,
        daysSinceFirstRun,
        isTrialActive,
        remainingDailyCalls,
    };
}
// ---------------------------------------------------------------------------
// Top tools helper
// ---------------------------------------------------------------------------
function buildTopTools(callsByTool, limit) {
    return Object.entries(callsByTool)
        .map(([name, count]) => ({ name, count }))
        .sort((a, b) => b.count - a.count)
        .slice(0, limit);
}
// ---------------------------------------------------------------------------
// Markdown formatting
// ---------------------------------------------------------------------------
/**
 * Formats a UsageStats object as a readable markdown report.
 */
export function formatStatsAsMarkdown(stats) {
    const lines = [];
    lines.push('## Usage Statistics');
    lines.push('');
    lines.push(`**Tier:** ${stats.currentTier}`);
    lines.push(`**Days since first run:** ${String(stats.daysSinceFirstRun)}`);
    lines.push(`**Trial active:** ${stats.isTrialActive ? 'Yes' : 'No'}`);
    lines.push(`**Total calls:** ${String(stats.totalCalls)}`);
    lines.push(`**Active specs:** ${String(stats.activeSpecs)}`);
    lines.push(`**Active projects:** ${String(stats.activeProjects)}`);
    if (stats.remainingDailyCalls >= 0) {
        lines.push(`**Remaining daily calls:** ${String(stats.remainingDailyCalls)}`);
    }
    lines.push('');
    lines.push('### Top Tools');
    lines.push('');
    lines.push('| Tool | Calls |');
    lines.push('|------|-------|');
    for (const tool of stats.topTools) {
        lines.push(`| ${tool.name} | ${String(tool.count)} |`);
    }
    if (stats.topTools.length === 0) {
        lines.push('| *(no calls yet)* | 0 |');
    }
    lines.push('');
    lines.push('### Calls by Tier');
    lines.push('');
    lines.push('| Tier | Calls |');
    lines.push('|------|-------|');
    const tierEntries = Object.entries(stats.callsByTier);
    if (tierEntries.length === 0) {
        lines.push('| *(none)* | 0 |');
    }
    else {
        for (const [tier, count] of tierEntries) {
            lines.push(`| ${tier} | ${String(count)} |`);
        }
    }
    return lines.join('\n');
}
//# sourceMappingURL=stats.js.map