"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isErrorFailureReason = isErrorFailureReason;
exports.getStrategyIdFromTest = getStrategyIdFromTest;
exports.getPluginIdFromResult = getPluginIdFromResult;
exports.rowMatchesPluginFilter = rowMatchesPluginFilter;
exports.mapRowToDrawerItem = mapRowToDrawerItem;
exports.wrapRowForMetricsPayload = wrapRowForMetricsPayload;
const metadata_1 = require("../../../redteam/constants/metadata");
function isErrorFailureReason(reason) {
    const v = String(reason ?? '').toLowerCase().trim();
    return v === 'error';
}
function getStrategyIdFromTest(test) {
    if (test?.metadata?.strategyId) {
        return String(test.metadata.strategyId);
    }
    if (test?.result?.testCase?.metadata?.strategyId) {
        return String(test.result.testCase.metadata.strategyId);
    }
    if (test?.testCase?.metadata?.strategyId) {
        return String(test.testCase.metadata.strategyId);
    }
    return 'basic';
}
function getPluginIdFromResult(result) {
    if (result?.metadata?.pluginId) {
        return String(result.metadata.pluginId);
    }
    const harmCategory = result?.vars?.harmCategory || result?.metadata?.harmCategory;
    if (harmCategory) {
        return metadata_1.categoryAliasesReverse[harmCategory] ?? null;
    }
    const metricNames = result?.gradingResult?.componentResults?.map((r) => r?.assertion?.metric) || [];
    for (const metric of metricNames) {
        if (!metric) {
            continue;
        }
        const baseName = String(metric).split('/')[0];
        if (baseName && metadata_1.categoryAliasesReverse[baseName]) {
            return metadata_1.categoryAliasesReverse[baseName];
        }
    }
    return null;
}
/** Match UI category keys (e.g. contracts) to plugin ids from results (e.g. trusys:redteam:contracts). */
function rowMatchesPluginFilter(row, filterPlugin) {
    const id = getPluginIdFromResult(row);
    if (!id) {
        return false;
    }
    if (id === filterPlugin) {
        return true;
    }
    const noPrefix = id.replace(/^trusys:redteam:/, '');
    if (noPrefix === filterPlugin) {
        return true;
    }
    return false;
}
function mapRowToDrawerItem(r, resultDetails, bucket) {
    if (bucket === 'passed') {
        return {
            prompt: r?.vars?.query?.toString() || r?.vars?.prompt?.toString() || r?.prompt?.raw || '',
            output: r?.response?.output,
            gradingResult: r.gradingResult || undefined,
            result: r,
        };
    }
    const injectVar = resultDetails?.config?.redteam?.injectVar ?? 'prompt';
    const injectVarValue = r.vars?.[injectVar]?.toString() || r.vars?.['query']?.toString();
    return {
        prompt: injectVarValue || r?.prompt?.raw || '',
        output: r.response?.output,
        gradingResult: r.gradingResult || undefined,
        result: r,
    };
}
/**
 * Same shape as frontend-built rows for charts (StrategyVulnerabilityTrendSection, etc.),
 * without prompt/response payload on the wire.
 */
function wrapRowForMetricsPayload(r) {
    const stripped = r && typeof r === 'object' ? { ...r } : r;
    if (stripped.vars && typeof stripped.vars === 'object') {
        stripped.vars = { ...stripped.vars };
        for (const k of Object.keys(stripped.vars)) {
            const v = stripped.vars[k];
            if (typeof v === 'string' && v.length > 400) {
                stripped.vars[k] = '';
            }
        }
    }
    if (stripped.prompt?.raw != null) {
        stripped.prompt = { ...stripped.prompt, raw: '' };
    }
    if (stripped.response && typeof stripped.response.output === 'string') {
        stripped.response = { ...stripped.response, output: '' };
    }
    return {
        prompt: '',
        output: '',
        gradingResult: stripped.gradingResult,
        result: stripped,
    };
}
//# sourceMappingURL=redteam-report-shared.js.map