"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRiskCategorySeverityMap = getRiskCategorySeverityMap;
exports.getSeverityBadgeVariant = getSeverityBadgeVariant;
exports.getUnifiedConfig = getUnifiedConfig;
// This file is imported by the frontend and shouldn't use native dependencies.
const path = __importStar(require("path"));
const logger_1 = __importDefault(require("../logger"));
const constants_1 = require("./constants");
const buildProviderForEval_1 = require("../util/customAIApplication/buildProviderForEval");
const handleHttpApiStructure_1 = require("../util/handleHttpApiStructure");
function getRiskCategorySeverityMap(plugins) {
    const overrides = plugins?.reduce((acc, plugin) => {
        if (plugin.severity) {
            acc[plugin.id] = plugin.severity;
        }
        return acc;
    }, {}) || {};
    return {
        ...constants_1.riskCategorySeverityMap,
        ...overrides,
    };
}
/**
 * Badge colors for red-team severity: Critical→red, High→orange, Medium→yellow, Low→green.
 * Accepts enum values (`critical`, …), display names (`Critical`, …), or API strings like `high`.
 */
function getSeverityBadgeVariant(severity) {
    if (severity == null || severity === '' || severity === 'Unknown') {
        return 'gray';
    }
    const s = severity.toLowerCase();
    if (s === constants_1.Severity.Critical) {
        return 'red';
    }
    if (s === constants_1.Severity.High) {
        return 'orange';
    }
    if (s === constants_1.Severity.Medium) {
        return 'yellow';
    }
    if (s === constants_1.Severity.Low) {
        return 'green';
    }
    return 'gray';
}
function getUnifiedConfig(config, applicationPath) {
    // Remove UI specific configs from target; normalize Custom AI upload targets for strict redteam schema
    const normalizedTarget = (0, buildProviderForEval_1.normalizeRedteamCustomAITarget)(config.target, config?.applicationIds?.[0]);
    const targetConfig = {
        ...(normalizedTarget.config && typeof normalizedTarget.config === 'object'
            ? normalizedTarget.config
            : {}),
    };
    const target = {
        ...normalizedTarget,
        config: targetConfig,
    };
    delete targetConfig.sessionSource;
    delete targetConfig.stateful;
    if (targetConfig.custom_provider_type === 'HTTP API') {
        const targetLabel = typeof target.label === 'string' ? target.label : '';
        const httpTarget = (0, handleHttpApiStructure_1.handleHTTPAPIStructure)(targetConfig, targetLabel);
        if (httpTarget?.config) {
            target.config = httpTarget.config;
        }
    }
    if (applicationPath) {
        const modulePath = path.isAbsolute(applicationPath)
            ? applicationPath
            : path.join(process.cwd(), applicationPath);
        logger_1.default.info(`modulePath ${modulePath}`);
        target.type = 'Custom AI Application';
        target.id = `file://${modulePath}`;
        targetConfig.basePath = modulePath;
    }
    const defaultTest = {
        ...(config.defaultTest ?? {}),
        options: {
            ...(config.defaultTest?.options ?? {}),
            transformVars: '{ ...vars, sessionId: context.uuid }',
        },
        vars: config.defaultTest?.vars,
    };
    return {
        description: config.description,
        targets: [target],
        prompts: config.prompts,
        extensions: config.extensions,
        defaultTest,
        redteam: {
            purpose: config.purpose,
            numTests: Number(config.numTests),
            ...(config.maxConcurrency && { maxConcurrency: config.maxConcurrency }),
            plugins: config.plugins.map((plugin) => {
                if (typeof plugin === 'string') {
                    return { id: plugin };
                }
                return {
                    id: plugin.id,
                    ...(plugin.config && Object.keys(plugin.config).length > 0 && { config: plugin.config }),
                };
            }),
            strategies: config.strategies.map((strategy) => {
                if (typeof strategy === 'string') {
                    if (constants_1.MULTI_TURN_STRATEGIES.includes(strategy) && config.target.config.stateful) {
                        return { id: strategy, config: { stateful: true } };
                    }
                    return { id: strategy };
                }
                // Determine if this is a stateful multi-turn strategy
                const isStatefulMultiTurn = constants_1.MULTI_TURN_STRATEGIES.includes(strategy.id) && config.target.config.stateful;
                // Check if we have any custom configuration
                const hasCustomConfig = strategy.config && Object.keys(strategy.config).length > 0;
                // If we don't need any configuration, return just the ID
                if (!isStatefulMultiTurn && !hasCustomConfig) {
                    return { id: strategy.id };
                }
                // Build the configuration object
                const configObject = {
                    ...(isStatefulMultiTurn && { stateful: true }),
                    ...(strategy.config || {}),
                };
                // Return the strategy with its configuration
                return {
                    id: strategy.id,
                    config: configObject,
                };
            }),
            ...(config.testGenerationInstructions && {
                testGenerationInstructions: config.testGenerationInstructions,
            }),
        },
    };
}
//# sourceMappingURL=sharedFrontend.js.map