"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateRedteamConfig = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
const generate_1 = require("../../../redteam/commands/generate");
const logger_1 = __importDefault(require("../../../logger"));
const generateRedteamConfig = async (req, res) => {
    try {
        const body = (req.body || {});
        const configYaml = typeof body.configYaml === 'string' ? body.configYaml : '';
        if (!configYaml.trim()) {
            res.status(400).json({ error: 'configYaml is required' });
            return;
        }
        const tmpDir = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), 'trusys-redteam-'));
        const configPath = node_path_1.default.join(tmpDir, 'trusysconfig.yaml');
        const outputPath = node_path_1.default.join(tmpDir, 'redteam.yaml');
        node_fs_1.default.writeFileSync(configPath, configYaml, 'utf8');
        const redteamConfig = await (0, generate_1.doGenerateRedteam)({
            ...(body.commandLineOptions || {}),
            force: body.force ?? true,
            cache: body.cache ?? false,
            config: configPath,
            output: outputPath,
            inRedteamRun: true,
        });
        res.json({ data: redteamConfig });
    }
    catch (error) {
        logger_1.default.error(`Error generating redteam config: ${error instanceof Error ? error.message : String(error)}`);
        res.status(500).json({
            error: error instanceof Error ? error.message : 'Failed to generate redteam config',
        });
    }
};
exports.generateRedteamConfig = generateRedteamConfig;
//# sourceMappingURL=generate-redteam-config.js.map