"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.coalesceLegacyProviderlessBreakdowns = exports.getModelsUsed = exports.getProviderModelKey = exports.normalizeUsageProvider = void 0;
const pricing_resolver_1 = require("../models-dev/pricing-resolver");
function normalizeUsageProvider(provider) {
    return (0, pricing_resolver_1.normalizeModelsDevProviderId)(provider);
}
exports.normalizeUsageProvider = normalizeUsageProvider;
function getProviderKey(provider) {
    return normalizeUsageProvider(provider) ?? '';
}
function getModelUsageLabel(item, ambiguousModelNames) {
    const provider = getProviderKey(item.provider);
    if (provider && ambiguousModelNames.has(item.modelName)) {
        return `${provider}/${item.modelName}`;
    }
    return item.modelName;
}
function getProviderModelKey(item) {
    return `${getProviderKey(item.provider)}\u0000${item.modelName}`;
}
exports.getProviderModelKey = getProviderModelKey;
function getModelsUsed(items) {
    const providersByModel = new Map();
    for (const item of items) {
        const providers = providersByModel.get(item.modelName) ?? new Set();
        providers.add(getProviderKey(item.provider));
        providersByModel.set(item.modelName, providers);
    }
    const ambiguousModelNames = new Set(Array.from(providersByModel.entries())
        .filter(([, providers]) => providers.size > 1)
        .map(([modelName]) => modelName));
    return [...new Set(items.map((item) => getModelUsageLabel(item, ambiguousModelNames)))];
}
exports.getModelsUsed = getModelsUsed;
function addBreakdownTokens(target, source) {
    target.inputTokens += source.inputTokens;
    target.outputTokens += source.outputTokens;
    target.cacheCreationTokens += source.cacheCreationTokens;
    target.cacheReadTokens += source.cacheReadTokens;
    target.cost += source.cost;
}
function coalesceLegacyProviderlessBreakdowns(items) {
    const byModel = new Map();
    for (const item of items) {
        const existing = byModel.get(item.modelName) ?? [];
        existing.push(item);
        byModel.set(item.modelName, existing);
    }
    const coalesced = [];
    for (const group of byModel.values()) {
        const providerBreakdowns = group.filter((item) => getProviderKey(item.provider));
        const legacyBreakdowns = group.filter((item) => !getProviderKey(item.provider));
        const providerKeys = new Set(providerBreakdowns.map((item) => getProviderKey(item.provider)));
        if (legacyBreakdowns.length > 0 && providerKeys.size === 1 && providerBreakdowns.length > 0) {
            const provider = Array.from(providerKeys)[0];
            const [firstProviderBreakdown, ...remainingProviderBreakdowns] = providerBreakdowns;
            const merged = { ...firstProviderBreakdown, provider };
            for (const breakdown of [...remainingProviderBreakdowns, ...legacyBreakdowns]) {
                addBreakdownTokens(merged, breakdown);
            }
            coalesced.push(merged);
            continue;
        }
        coalesced.push(...group.map((item) => ({ ...item })));
    }
    return coalesced;
}
exports.coalesceLegacyProviderlessBreakdowns = coalesceLegacyProviderlessBreakdowns;
//# sourceMappingURL=model-identity.js.map