const codexApiTokenRates = {
    "gpt-5.4": {
        inputPerMillion: 2.5,
        cachedInputPerMillion: 0.25,
        outputPerMillion: 15
    },
    "gpt-5.4-mini": {
        inputPerMillion: 0.75,
        cachedInputPerMillion: 0.075,
        outputPerMillion: 4.5
    },
    "gpt-5.2": {
        inputPerMillion: 1.75,
        cachedInputPerMillion: 0.175,
        outputPerMillion: 14
    },
    "gpt-5.2-codex": {
        inputPerMillion: 1.75,
        cachedInputPerMillion: 0.175,
        outputPerMillion: 14
    },
    "gpt-5.3-codex": {
        inputPerMillion: 1.75,
        cachedInputPerMillion: 0.175,
        outputPerMillion: 14
    },
    "gpt-5.1-codex-max": {
        inputPerMillion: 1.25,
        cachedInputPerMillion: 0.125,
        outputPerMillion: 10
    },
    "gpt-5.1-codex-mini": {
        inputPerMillion: 0.25,
        cachedInputPerMillion: 0.025,
        outputPerMillion: 2
    },
    "codex-mini-latest": {
        inputPerMillion: 1.5,
        cachedInputPerMillion: 0.375,
        outputPerMillion: 6
    }
};
const fallbackCloudRates = {
    inputPerMillion: 0.5,
    cachedInputPerMillion: 0.05,
    outputPerMillion: 2
};
const geminiApiTokenRates = [
    { pattern: /(gemini-?3|3\.\d).*pro|gemini-?3-pro/i, rate: { inputPerMillion: 2, cachedInputPerMillion: 0.2, outputPerMillion: 12 } },
    { pattern: /2[._-]?5.*pro|gemini-pro|^pro$/i, rate: { inputPerMillion: 1.25, cachedInputPerMillion: 0.125, outputPerMillion: 10 } },
    { pattern: /flash[-_ ]?lite/i, rate: { inputPerMillion: 0.1, cachedInputPerMillion: 0.025, outputPerMillion: 0.4 } },
    { pattern: /flash/i, rate: { inputPerMillion: 0.3, cachedInputPerMillion: 0.075, outputPerMillion: 2.5 } }
];
export function estimateTokens(value) {
    const normalizedValue = value.trim();
    return normalizedValue ? Math.ceil(normalizedValue.length / 4) : 0;
}
export function attachTokenCost(telemetry, provider, model, openRouterRates, providerCostUsd) {
    if (provider === "ollama") {
        return {
            ...telemetry,
            estimatedCostUsd: 0,
            costSource: "local"
        };
    }
    if (provider === "openrouter") {
        if (providerCostUsd !== undefined && providerCostUsd !== null) {
            return {
                ...telemetry,
                cachedPromptTokens: Math.min(telemetry.cachedPromptTokens, telemetry.promptTokens),
                estimatedCostUsd: providerCostUsd,
                costSource: "api-pricing"
            };
        }
        if (!openRouterRates) {
            return {
                ...telemetry,
                cachedPromptTokens: Math.min(telemetry.cachedPromptTokens, telemetry.promptTokens),
                estimatedCostUsd: null,
                costSource: "unknown"
            };
        }
        const cachedPromptTokens = Math.min(telemetry.cachedPromptTokens, telemetry.promptTokens);
        const cacheWriteTokens = Math.min(telemetry.cacheWriteTokens, Math.max(0, telemetry.promptTokens - cachedPromptTokens));
        const uncachedPromptTokens = Math.max(0, telemetry.promptTokens - cachedPromptTokens - cacheWriteTokens);
        const estimatedCostUsd = uncachedPromptTokens * openRouterRates.inputPerToken +
            cachedPromptTokens * openRouterRates.cachedInputPerToken +
            cacheWriteTokens * openRouterRates.cacheWritePerToken +
            telemetry.responseTokens * openRouterRates.outputPerToken;
        return {
            ...telemetry,
            cachedPromptTokens,
            estimatedCostUsd,
            costSource: "api-pricing"
        };
    }
    if (provider === "gemini-wrapper") {
        return {
            ...telemetry,
            cachedPromptTokens: Math.min(telemetry.cachedPromptTokens, telemetry.promptTokens),
            estimatedCostUsd: 0,
            costSource: "free-route"
        };
    }
    const rates = provider === "codex" ? codexApiTokenRates[model] : provider === "gemini" ? readGeminiRates(model) : undefined;
    const effectiveRates = rates ?? fallbackCloudRates;
    const cachedPromptTokens = Math.min(telemetry.cachedPromptTokens, telemetry.promptTokens);
    const uncachedPromptTokens = Math.max(0, telemetry.promptTokens - cachedPromptTokens);
    const estimatedCostUsd = (uncachedPromptTokens * effectiveRates.inputPerMillion +
        cachedPromptTokens * effectiveRates.cachedInputPerMillion +
        telemetry.responseTokens * effectiveRates.outputPerMillion) /
        1_000_000;
    return {
        ...telemetry,
        cachedPromptTokens,
        estimatedCostUsd,
        costSource: rates ? "api-pricing" : "fallback-pricing"
    };
}
export function emptySessionTelemetry() {
    return {
        requests: 0,
        promptTokens: 0,
        cachedPromptTokens: 0,
        cacheWriteTokens: 0,
        responseTokens: 0,
        totalTokens: 0,
        estimatedCostUsd: null,
        costSource: "unknown"
    };
}
export function addTelemetryToSession(session, telemetry) {
    const estimatedCostUsd = session.requests === 0
        ? telemetry.estimatedCostUsd
        : session.estimatedCostUsd === null
            ? telemetry.estimatedCostUsd
            : telemetry.estimatedCostUsd === null
                ? session.estimatedCostUsd
                : session.estimatedCostUsd + telemetry.estimatedCostUsd;
    return {
        requests: session.requests + 1,
        promptTokens: session.promptTokens + telemetry.promptTokens,
        cachedPromptTokens: session.cachedPromptTokens + telemetry.cachedPromptTokens,
        cacheWriteTokens: session.cacheWriteTokens + telemetry.cacheWriteTokens,
        responseTokens: session.responseTokens + telemetry.responseTokens,
        totalTokens: session.totalTokens + telemetry.totalTokens,
        estimatedCostUsd,
        costSource: mergeCostSource(session.costSource, telemetry.costSource, session.requests)
    };
}
export function estimateComparableApiCost(provider, model, promptTokens, responseTokens, cachedPromptTokens = 0) {
    if (provider === "gemini-wrapper" || provider === "gemini") {
        const rates = readGeminiRates(model);
        if (!rates) {
            return {
                costUsd: estimateCost(promptTokens, responseTokens, cachedPromptTokens, fallbackCloudRates),
                source: "fallback-pricing"
            };
        }
        return {
            costUsd: estimateCost(promptTokens, responseTokens, cachedPromptTokens, rates),
            source: "api-pricing"
        };
    }
    if (provider === "ollama") {
        return {
            costUsd: estimateCost(promptTokens, responseTokens, cachedPromptTokens, fallbackCloudRates),
            source: "fallback-pricing"
        };
    }
    if (provider === "codex" || provider === "openrouter" || provider === "nvidia") {
        return {
            costUsd: 0,
            source: "api-pricing"
        };
    }
    return {
        costUsd: null,
        source: "unknown"
    };
}
function readGeminiRates(model) {
    return geminiApiTokenRates.find((entry) => entry.pattern.test(model))?.rate;
}
function estimateCost(promptTokens, responseTokens, cachedPromptTokens, rates) {
    const safeCachedTokens = Math.min(cachedPromptTokens, promptTokens);
    const uncachedTokens = Math.max(0, promptTokens - safeCachedTokens);
    return (uncachedTokens * rates.inputPerMillion + safeCachedTokens * rates.cachedInputPerMillion + responseTokens * rates.outputPerMillion) / 1_000_000;
}
function mergeCostSource(currentSource, nextSource, previousRequests) {
    if (previousRequests === 0) {
        return nextSource;
    }
    return currentSource === nextSource ? currentSource : "mixed";
}
//# sourceMappingURL=tokenAccounting.js.map