"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeTrusysAccessToken = normalizeTrusysAccessToken;
exports.attachTrusysAccessTokenToRedteamLiveConfig = attachTrusysAccessTokenToRedteamLiveConfig;
exports.attachTrusysAccessTokenToAzureCustomAIProviders = attachTrusysAccessTokenToAzureCustomAIProviders;
const buildProviderForEval_1 = require("./buildProviderForEval");
/** Normalize Authorization / x-access-token from Express headers for CP blob fetch. */
function normalizeTrusysAccessToken(token) {
    const raw = Array.isArray(token) ? token[0] : token;
    if (typeof raw !== 'string') {
        return undefined;
    }
    const trimmed = raw.trim();
    return trimmed.length > 0 ? trimmed : undefined;
}
/** Attach token and testRunId to azure-sessions-custom-ai entries in a redteam live config. */
function attachTrusysAccessTokenToRedteamLiveConfig(config, accessToken, testRunId, apiKey) {
    if (!config) {
        return config;
    }
    const next = { ...config };
    if (Array.isArray(next.providers)) {
        next.providers = attachTrusysAccessTokenToAzureCustomAIProviders(next.providers, accessToken, testRunId, apiKey);
    }
    if (Array.isArray(next.targets)) {
        next.targets = attachTrusysAccessTokenToAzureCustomAIProviders(next.targets, accessToken, testRunId, apiKey);
    }
    return next;
}
function attachTrusysAccessTokenToAzureCustomAIProviders(providers, accessToken, testRunId, apiKey) {
    const normalizedToken = normalizeTrusysAccessToken(accessToken);
    const normalizedApiKey = typeof apiKey === 'string' && apiKey.trim().length > 0 ? apiKey.trim() : undefined;
    const runId = typeof testRunId === 'string' && testRunId.trim() ? testRunId.trim() : undefined;
    if ((!normalizedToken && !normalizedApiKey && !runId) || !Array.isArray(providers)) {
        return providers;
    }
    return providers.map((provider) => {
        if (!provider || typeof provider !== 'object') {
            return provider;
        }
        const record = provider;
        if (record.id !== buildProviderForEval_1.AZURE_SESSIONS_CUSTOM_AI_PROVIDER_ID) {
            return provider;
        }
        return {
            ...record,
            config: {
                ...record.config,
                ...(normalizedToken ? { trusysAccessToken: normalizedToken } : {}),
                ...(normalizedApiKey ? { trusysApiKey: normalizedApiKey } : {}),
                ...(runId ? { testRunId: runId } : {}),
            },
        };
    });
}
//# sourceMappingURL=attachTrusysAccessTokenToProviders.js.map