"use strict";
/**
 * Usage aggregation utilities for cost calculation
 *
 * IMPORTANT: This file must NOT import tokenizer to avoid pulling
 * 2MB+ of encoding data into the renderer process.
 *
 * Separated from tokenStatsCalculator.ts to keep tokenizer in main process only.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.sumUsageHistory = sumUsageHistory;
/**
 * Sum multiple ChatUsageDisplay objects into a single cumulative display
 * Used for showing total costs across multiple API responses
 */
function sumUsageHistory(usageHistory) {
    if (usageHistory.length === 0)
        return undefined;
    // Track if any costs are undefined (model pricing unknown)
    let hasUndefinedCosts = false;
    const sum = {
        input: { tokens: 0, cost_usd: 0 },
        cached: { tokens: 0, cost_usd: 0 },
        cacheCreate: { tokens: 0, cost_usd: 0 },
        output: { tokens: 0, cost_usd: 0 },
        reasoning: { tokens: 0, cost_usd: 0 },
    };
    for (const usage of usageHistory) {
        // Iterate over each component and sum tokens and costs
        const componentKeys = [
            "input",
            "cached",
            "cacheCreate",
            "output",
            "reasoning",
        ];
        for (const key of componentKeys) {
            sum[key].tokens += usage[key].tokens;
            if (usage[key].cost_usd === undefined) {
                hasUndefinedCosts = true;
            }
            else {
                sum[key].cost_usd = (sum[key].cost_usd ?? 0) + (usage[key].cost_usd ?? 0);
            }
        }
    }
    // If any costs were undefined, set all to undefined
    if (hasUndefinedCosts) {
        sum.input.cost_usd = undefined;
        sum.cached.cost_usd = undefined;
        sum.cacheCreate.cost_usd = undefined;
        sum.output.cost_usd = undefined;
        sum.reasoning.cost_usd = undefined;
    }
    return sum;
}
//# sourceMappingURL=usageAggregator.js.map