// ---------------------------------------------------------------------------
// Static tool classification (local/template/light-llm/full-llm)
// ---------------------------------------------------------------------------
const TOOL_MODES = {
    // local — zero LLM tokens, pure logic
    list_specs: 'local',
    update_status: 'local',
    set_locale: 'local',
    manage_context: 'local',
    manage_git: 'local',
    capture_learning: 'local',
    // template — minimal tokens, mostly template filling
    create_spec: 'template',
    generate_docs: 'template',
    generate_checklist: 'template',
    generate_skills: 'template',
    clarify_requirements: 'template',
    // light-llm — moderate reasoning required
    estimate: 'light-llm',
    suggest_stack: 'light-llm',
    challenge_spec: 'light-llm',
    validate: 'light-llm',
    suggest_tooling: 'light-llm',
    suggest_mcps: 'light-llm',
    learn_pattern: 'light-llm',
    // full-llm — complex reasoning
    audit: 'full-llm',
    orchestrate: 'full-llm',
    reverse_engineer: 'full-llm',
    init_project: 'full-llm',
    update_spec: 'full-llm',
    fetch_web: 'full-llm',
};
// Estimated tokens per mode (conservative baseline)
const TOKENS_BY_MODE = {
    local: 0,
    template: 500,
    'light-llm': 2000,
    'full-llm': 8000,
};
// Naive baseline — assume every operation would be full-llm
const NAIVE_TOKENS = 8000;
// Sonnet pricing per million tokens (output)
const COST_PER_TOKEN = 3 / 1_000_000;
// Budget warning threshold
const WARNING_THRESHOLD = 0.8;
// ---------------------------------------------------------------------------
// Model recommendations by operation type
// ---------------------------------------------------------------------------
const TOOL_MODEL = {
    // haiku: read-only, search, simple lookups
    list_specs: 'haiku',
    manage_context: 'haiku',
    fetch_web: 'haiku',
    learn_pattern: 'haiku',
    capture_learning: 'haiku',
    // sonnet: implementation, writing, refactoring
    create_spec: 'sonnet',
    generate_docs: 'sonnet',
    generate_checklist: 'sonnet',
    generate_skills: 'sonnet',
    clarify_requirements: 'sonnet',
    update_spec: 'sonnet',
    suggest_stack: 'sonnet',
    suggest_tooling: 'sonnet',
    suggest_mcps: 'sonnet',
    validate: 'sonnet',
    estimate: 'sonnet',
    challenge_spec: 'sonnet',
    update_status: 'sonnet',
    manage_git: 'sonnet',
    // opus: architecture, complex debugging, multi-system design
    audit: 'opus',
    orchestrate: 'opus',
    reverse_engineer: 'opus',
    init_project: 'opus',
};
// ---------------------------------------------------------------------------
// classifyOperation
// ---------------------------------------------------------------------------
export function classifyOperation(tool) {
    return TOOL_MODES[tool] ?? 'full-llm';
}
// ---------------------------------------------------------------------------
// recommendModel
// ---------------------------------------------------------------------------
export function recommendModel(tool) {
    return TOOL_MODEL[tool] ?? 'sonnet';
}
// ---------------------------------------------------------------------------
// estimateTokensForOperation
// ---------------------------------------------------------------------------
export function estimateTokensForOperation(tool) {
    const mode = classifyOperation(tool);
    return TOKENS_BY_MODE[mode];
}
// ---------------------------------------------------------------------------
// estimateCostUsd
// ---------------------------------------------------------------------------
export function estimateCostUsd(tokensUsed) {
    return Math.round(tokensUsed * COST_PER_TOKEN * 100_000) / 100_000;
}
// ---------------------------------------------------------------------------
// calculateSavingsVsNaive
// ---------------------------------------------------------------------------
export function calculateSavingsVsNaive(usages) {
    if (usages.length === 0) {
        return 0;
    }
    const actualTokens = usages.reduce((sum, u) => sum + u.tokensUsed, 0);
    const naiveTokens = usages.length * NAIVE_TOKENS;
    if (naiveTokens === 0) {
        return 0;
    }
    const savings = ((naiveTokens - actualTokens) / naiveTokens) * 100;
    return Math.max(0, Math.round(savings));
}
// ---------------------------------------------------------------------------
// getBudgetStatus
// ---------------------------------------------------------------------------
export function getBudgetStatus(monthlyLimitUsd, usages) {
    // Filter to current calendar month
    const now = new Date();
    const monthStart = new Date(now.getFullYear(), now.getMonth(), 1).toISOString();
    const monthUsages = usages.filter((u) => u.timestamp >= monthStart);
    const currentUsageUsd = monthUsages.reduce((sum, u) => sum + u.costUsd, 0);
    const percentUsed = monthlyLimitUsd > 0 ? Math.round((currentUsageUsd / monthlyLimitUsd) * 100) : 0;
    // Reset date = first day of next month
    const nextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);
    return {
        monthlyLimitUsd,
        currentUsageUsd: Math.round(currentUsageUsd * 10000) / 10000,
        percentUsed,
        resetDate: nextMonth.toISOString().slice(0, 10),
        isWarning: percentUsed >= WARNING_THRESHOLD * 100,
        isExceeded: percentUsed >= 100,
    };
}
// ---------------------------------------------------------------------------
// formatSavingsDashboard
// ---------------------------------------------------------------------------
export function formatSavingsDashboard(usages) {
    if (usages.length === 0) {
        return 'No token usage recorded yet.';
    }
    const savings = calculateSavingsVsNaive(usages);
    const totalTokens = usages.reduce((sum, u) => sum + u.tokensUsed, 0);
    const totalCost = usages.reduce((sum, u) => sum + u.costUsd, 0);
    const byMode = {};
    for (const u of usages) {
        const m = byMode[u.mode] ?? { count: 0, tokens: 0 };
        m.count += 1;
        m.tokens += u.tokensUsed;
        byMode[u.mode] = m;
    }
    const modeLines = Object.entries(byMode)
        .map(([mode, { count, tokens }]) => `  ${mode}: ${String(count)} ops, ${String(tokens)} tokens`)
        .join('\n');
    return [
        `Token Optimization Dashboard`,
        `Operations: ${String(usages.length)} | Total tokens: ${String(totalTokens)} | Cost: $${totalCost.toFixed(4)}`,
        `Savings vs naive (all full-llm): ${String(savings)}%`,
        `Breakdown by mode:`,
        modeLines,
    ].join('\n');
}
// ---------------------------------------------------------------------------
// getOperationHistory
// ---------------------------------------------------------------------------
export function getOperationHistory(usages, tool) {
    if (!tool) {
        return usages;
    }
    return usages.filter((u) => u.tool === tool);
}
//# sourceMappingURL=token-tracker.js.map