"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.estimateOpenAIUsageCost = void 0;
var resolveio_server_app_1 = require("../resolveio-server-app");
var common_1 = require("./common");
var GPT_5_5_PRICING = { inputPer1k: 0.005, cachedInputPer1k: 0.0005, outputPer1k: 0.03 };
var GPT_5_4_PRICING = { inputPer1k: 0.0025, cachedInputPer1k: 0.00025, outputPer1k: 0.015 };
var GPT_5_4_MINI_PRICING = { inputPer1k: 0.00075, cachedInputPer1k: 0.000075, outputPer1k: 0.0045 };
var DEFAULT_OPENAI_MODEL_PRICING = {
    'gpt-5.5': GPT_5_5_PRICING,
    'gpt-5.5-codex': GPT_5_5_PRICING,
    'gpt-5-codex': GPT_5_5_PRICING,
    'gpt-5.3-codex': GPT_5_5_PRICING,
    'gpt-5.3-codex-spark': GPT_5_5_PRICING,
    'gpt-5.2-codex': GPT_5_5_PRICING,
    'gpt-5.4': GPT_5_4_PRICING,
    'gpt-5.4-codex': GPT_5_4_PRICING,
    'gpt-5.4-mini': GPT_5_4_MINI_PRICING,
    'gpt-5.4-mini-codex': GPT_5_4_MINI_PRICING,
    'codex-mini-latest': GPT_5_4_MINI_PRICING
};
var toNumber = function (value) {
    var parsed = Number(value);
    return Number.isFinite(parsed) ? parsed : 0;
};
var normalizeModelKey = function (value) {
    return String(value || '').trim().toLowerCase();
};
var parsePricingJson = function (raw) {
    if (!raw) {
        return {};
    }
    try {
        var parsed_1 = JSON.parse(raw);
        if (!parsed_1 || typeof parsed_1 !== 'object') {
            return {};
        }
        var out_1 = {};
        Object.keys(parsed_1).forEach(function (key) {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
            var entry = parsed_1[key] || {};
            var inputPer1k = toNumber((_c = (_b = (_a = entry.inputPer1k) !== null && _a !== void 0 ? _a : entry.input) !== null && _b !== void 0 ? _b : entry.prompt) !== null && _c !== void 0 ? _c : 0);
            var cachedInputPer1k = toNumber((_g = (_f = (_e = (_d = entry.cachedInputPer1k) !== null && _d !== void 0 ? _d : entry.cachedInput) !== null && _e !== void 0 ? _e : entry.cached) !== null && _f !== void 0 ? _f : entry.promptCached) !== null && _g !== void 0 ? _g : 0);
            var outputPer1k = toNumber((_k = (_j = (_h = entry.outputPer1k) !== null && _h !== void 0 ? _h : entry.output) !== null && _j !== void 0 ? _j : entry.completion) !== null && _k !== void 0 ? _k : 0);
            if (inputPer1k || cachedInputPer1k || outputPer1k) {
                out_1[normalizeModelKey(key)] = { inputPer1k: inputPer1k, cachedInputPer1k: cachedInputPer1k, outputPer1k: outputPer1k };
            }
        });
        return out_1;
    }
    catch (_a) {
        return {};
    }
};
var resolvePricingConfig = function () {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    var config = resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
    var rawJson = String((_b = (_a = config['OPENAI_MODEL_PRICING']) !== null && _a !== void 0 ? _a : process.env.OPENAI_MODEL_PRICING) !== null && _b !== void 0 ? _b : '').trim();
    var defaultInput = toNumber((_d = (_c = config['OPENAI_COST_PER_1K_INPUT_TOKENS']) !== null && _c !== void 0 ? _c : process.env.OPENAI_COST_PER_1K_INPUT_TOKENS) !== null && _d !== void 0 ? _d : 0);
    var defaultCachedInput = toNumber((_g = (_f = (_e = config['OPENAI_COST_PER_1K_CACHED_INPUT_TOKENS']) !== null && _e !== void 0 ? _e : process.env.OPENAI_COST_PER_1K_CACHED_INPUT_TOKENS) !== null && _f !== void 0 ? _f : process.env.OPENAI_COST_PER_1K_CACHED_TOKENS) !== null && _g !== void 0 ? _g : 0);
    var defaultOutput = toNumber((_j = (_h = config['OPENAI_COST_PER_1K_OUTPUT_TOKENS']) !== null && _h !== void 0 ? _h : process.env.OPENAI_COST_PER_1K_OUTPUT_TOKENS) !== null && _j !== void 0 ? _j : 0);
    return {
        perModel: __assign(__assign({}, DEFAULT_OPENAI_MODEL_PRICING), parsePricingJson(rawJson)),
        defaultPricing: { inputPer1k: defaultInput, cachedInputPer1k: defaultCachedInput, outputPer1k: defaultOutput }
    };
};
var resolveModelPricing = function (model, perModel, defaultPricing) {
    var normalizedModel = normalizeModelKey(model);
    if (perModel[normalizedModel]) {
        return perModel[normalizedModel];
    }
    var prefix = Object.keys(perModel)
        .sort(function (a, b) { return b.length - a.length; })
        .find(function (key) { return normalizedModel.startsWith("".concat(key, "-")); });
    return prefix ? perModel[prefix] : defaultPricing;
};
var estimateOpenAIUsageCost = function (model, inputTokens, outputTokens, cachedInputTokens) {
    if (cachedInputTokens === void 0) { cachedInputTokens = 0; }
    var _a = resolvePricingConfig(), perModel = _a.perModel, defaultPricing = _a.defaultPricing;
    var pricing = resolveModelPricing(model, perModel, defaultPricing);
    if (!pricing.inputPer1k && !pricing.cachedInputPer1k && !pricing.outputPer1k) {
        return 0;
    }
    var uncachedInputTokens = Math.max(0, toNumber(inputTokens) - toNumber(cachedInputTokens));
    var inputCost = (uncachedInputTokens / 1000) * pricing.inputPer1k;
    var cachedInputCost = (toNumber(cachedInputTokens) / 1000) * (pricing.cachedInputPer1k || 0);
    var outputCost = (toNumber(outputTokens) / 1000) * pricing.outputPer1k;
    return (0, common_1.round)(inputCost + cachedInputCost + outputCost, 6);
};
exports.estimateOpenAIUsageCost = estimateOpenAIUsageCost;

//# sourceMappingURL=openai-usage-cost.js.map
