"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildCliproxyStatsFromUsageResponse = void 0;
const account_stats_key_1 = require("../accounts/account-stats-key");
const provider_capabilities_1 = require("../provider-capabilities");
const email_account_identity_1 = require("../accounts/email-account-identity");
function normalizeProvider(provider) {
    const normalized = provider.trim().toLowerCase();
    if (!normalized) {
        return 'unknown';
    }
    return (0, provider_capabilities_1.mapExternalProviderName)(normalized) ?? normalized;
}
function extractAuthFilenameFromSource(source) {
    const authFileMatch = source.match(/(?:^|\s)auth_file=("[^"]+"|'[^']+'|[^\s]+)/i);
    const rawValue = authFileMatch?.[1] ?? source;
    const value = rawValue.trim().replace(/^['"]|['"]$/g, '');
    const filenameCandidate = value.split('|').pop() ?? value;
    return filenameCandidate.split(/[\\/]/).pop() ?? filenameCandidate.trim();
}
function stripKnownAuthPlanSuffix(value) {
    return value.replace(/-(?:free|plus|pro|team|business|enterprise)$/i, '');
}
function normalizeAuthFilenameSource(provider, source) {
    const filename = extractAuthFilenameFromSource(source);
    const normalizedProvider = normalizeProvider(provider);
    const hadJsonExtension = /\.json$/i.test(filename);
    let candidate = filename.replace(/\.json$/i, '');
    const providerPrefix = `${normalizedProvider}-`;
    if (candidate.toLowerCase().startsWith(providerPrefix)) {
        candidate = candidate.slice(providerPrefix.length);
    }
    candidate = candidate.replace(/^[a-f0-9]{8}[-_]/i, '');
    if (hadJsonExtension) {
        candidate = stripKnownAuthPlanSuffix(candidate);
    }
    const parts = candidate.split('@');
    if (parts.length < 2) {
        return null;
    }
    const localPart = parts[0]?.trim();
    const domainPart = parts.slice(1).join('@').trim();
    const email = `${localPart}@${domainPart}`;
    return /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email) ? email.toLowerCase() : null;
}
function buildAuthIndexLookup(authFiles) {
    const lookup = new Map();
    const duplicateEmailCounts = new Map();
    for (const authFile of authFiles ?? []) {
        if (!authFile.provider || !authFile.email) {
            continue;
        }
        const key = `${normalizeProvider(authFile.provider)}:${authFile.email.trim().toLowerCase()}`;
        duplicateEmailCounts.set(key, (duplicateEmailCounts.get(key) ?? 0) + 1);
    }
    for (const authFile of authFiles ?? []) {
        if (authFile.auth_index === undefined || authFile.auth_index === null) {
            continue;
        }
        const provider = authFile.provider ? normalizeProvider(authFile.provider) : undefined;
        const email = authFile.email?.trim() || undefined;
        const name = authFile.name?.trim() || undefined;
        const source = authFile.email?.trim() || authFile.name?.trim() || undefined;
        if (!provider && !source) {
            continue;
        }
        lookup.set(String(authFile.auth_index), {
            provider,
            source,
            email,
            name,
            duplicateEmailCount: provider && email
                ? (duplicateEmailCounts.get(`${provider}:${email.toLowerCase()}`) ?? 1)
                : 1,
        });
    }
    return lookup;
}
function resolveProviderForDetail(usageProvider, detail, authIndexLookup) {
    const resolvedAuthFile = authIndexLookup.get(String(detail.auth_index));
    if (resolvedAuthFile?.provider) {
        return resolvedAuthFile.provider;
    }
    return normalizeProvider(usageProvider);
}
function resolveSourceForDetail(resolvedProvider, detail, authIndexLookup) {
    const resolvedAuthFile = authIndexLookup.get(String(detail.auth_index));
    if (resolvedAuthFile?.email && resolvedAuthFile?.name) {
        const derivedSource = (0, email_account_identity_1.buildEmailBackedAccountId)(resolvedProvider, resolvedAuthFile.name, resolvedAuthFile.email, resolvedAuthFile.duplicateEmailCount ?? 1);
        if (derivedSource) {
            return derivedSource;
        }
    }
    const source = detail.source?.trim();
    if (source) {
        return normalizeAuthFilenameSource(resolvedProvider, source) ?? source;
    }
    return resolvedAuthFile?.source ?? 'unknown';
}
function resolveCountWithDetails(aggregateCount, detailCount) {
    if (aggregateCount === undefined) {
        return detailCount;
    }
    return aggregateCount < detailCount ? detailCount : aggregateCount;
}
function shouldReplaceLastUsedAt(current, next) {
    if (!next) {
        return false;
    }
    const nextTime = Date.parse(next);
    if (!Number.isFinite(nextTime)) {
        return false;
    }
    if (!current) {
        return true;
    }
    const currentTime = Date.parse(current);
    return !Number.isFinite(currentTime) || nextTime > currentTime;
}
function buildCliproxyStatsFromUsageResponse(data, options = {}) {
    const usage = data.usage;
    const requestsByModel = {};
    const requestsByProvider = {};
    const accountStats = {};
    const authIndexLookup = buildAuthIndexLookup(options.authFiles);
    let totalSuccessCount = 0;
    let totalFailureCount = 0;
    let totalInputTokens = 0;
    let totalOutputTokens = 0;
    let sawAnyDetail = false;
    if (usage?.apis) {
        for (const [provider, providerData] of Object.entries(usage.apis)) {
            let providerDetailCount = 0;
            if (!providerData.models) {
                const normalizedProvider = normalizeProvider(provider);
                requestsByProvider[normalizedProvider] =
                    (requestsByProvider[normalizedProvider] ?? 0) + (providerData.total_requests ?? 0);
                continue;
            }
            for (const [model, modelData] of Object.entries(providerData.models)) {
                let modelDetailCount = 0;
                if (!modelData.details) {
                    requestsByModel[model] = (requestsByModel[model] ?? 0) + (modelData.total_requests ?? 0);
                    continue;
                }
                for (const detail of modelData.details) {
                    sawAnyDetail = true;
                    providerDetailCount++;
                    modelDetailCount++;
                    const resolvedProvider = resolveProviderForDetail(provider, detail, authIndexLookup);
                    const source = resolveSourceForDetail(resolvedProvider, detail, authIndexLookup);
                    const accountKey = (0, account_stats_key_1.buildQualifiedAccountStatsKey)(resolvedProvider, source);
                    requestsByProvider[resolvedProvider] = (requestsByProvider[resolvedProvider] ?? 0) + 1;
                    if (!accountStats[accountKey]) {
                        accountStats[accountKey] = {
                            accountKey,
                            provider: resolvedProvider,
                            source,
                            successCount: 0,
                            failureCount: 0,
                            totalTokens: 0,
                        };
                    }
                    if (detail.failed) {
                        accountStats[accountKey].failureCount++;
                        totalFailureCount++;
                    }
                    else {
                        accountStats[accountKey].successCount++;
                        totalSuccessCount++;
                    }
                    const tokens = detail.tokens?.total_tokens ?? 0;
                    accountStats[accountKey].totalTokens += tokens;
                    if (shouldReplaceLastUsedAt(accountStats[accountKey].lastUsedAt, detail.timestamp)) {
                        accountStats[accountKey].lastUsedAt = detail.timestamp;
                    }
                    totalInputTokens += detail.tokens?.input_tokens ?? 0;
                    totalOutputTokens += detail.tokens?.output_tokens ?? 0;
                }
                requestsByModel[model] =
                    (requestsByModel[model] ?? 0) +
                        resolveCountWithDetails(modelData.total_requests, modelDetailCount);
            }
            const providerTotal = providerData.total_requests ?? 0;
            if (providerDetailCount === 0) {
                const normalizedProvider = normalizeProvider(provider);
                requestsByProvider[normalizedProvider] =
                    (requestsByProvider[normalizedProvider] ?? 0) + providerTotal;
            }
            else if (providerTotal > providerDetailCount) {
                const normalizedProvider = normalizeProvider(provider);
                requestsByProvider[normalizedProvider] =
                    (requestsByProvider[normalizedProvider] ?? 0) + (providerTotal - providerDetailCount);
            }
        }
    }
    const aggregateFailureCount = usage?.failure_count ?? data.failed_requests;
    const successCount = sawAnyDetail
        ? resolveCountWithDetails(usage?.success_count, totalSuccessCount)
        : (usage?.success_count ?? 0);
    const failureCount = sawAnyDetail
        ? resolveCountWithDetails(aggregateFailureCount, totalFailureCount)
        : (aggregateFailureCount ?? 0);
    const providerTotalRequests = Object.values(requestsByProvider).reduce((total, count) => total + count, 0);
    const totalRequests = Math.max(usage?.total_requests ?? 0, successCount + failureCount, providerTotalRequests);
    return {
        totalRequests,
        successCount,
        failureCount,
        tokens: {
            input: totalInputTokens,
            output: totalOutputTokens,
            total: usage?.total_tokens ?? 0,
        },
        requestsByModel,
        requestsByProvider,
        accountStats,
        quotaExceededCount: failureCount,
        retryCount: 0,
        collectedAt: new Date().toISOString(),
    };
}
exports.buildCliproxyStatsFromUsageResponse = buildCliproxyStatsFromUsageResponse;
//# sourceMappingURL=stats-transformer.js.map