"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateNewDataset = void 0;
const logger_1 = __importDefault(require("../../../logger"));
const datasetDynamicDatasetLanguage_1 = require("../../services/datasetDynamicDatasetLanguage");
const datasetEncryptGeneratedRows_1 = require("../../services/datasetEncryptGeneratedRows");
const datasetService_1 = require("../../services/datasetService");
const generateNewDataset = async (req, res) => {
    try {
        const { promptLibraryId, tests, provider, instructions, numSamples, prompt = '', fields, language: languageInput = 'English', orgId, } = req.body;
        const langConfig = { language: languageInput };
        const synthesisLanguage = (0, datasetDynamicDatasetLanguage_1.getDatasetLanguageLabelForSynthesis)(langConfig);
        const results = await datasetService_1.datasetService.generateDatasetFromPrompts({
            promptLibraryId,
            tests: tests || [],
            provider,
            instructions,
            numSamples: Number.parseInt(numSamples) || 15,
            prompt,
            fields,
            language: synthesisLanguage,
            orgId,
            workspaceId: orgId,
            datasetId: req.body.datasetId,
        });
        const datasetTokenUsage = results.tokenUsages;
        let encryptedTestCaseVars = results.testCaseVars;
        if (results.testCaseVars &&
            results.testCaseVars.length > 0 &&
            fields &&
            fields.length > 0) {
            try {
                encryptedTestCaseVars = await (0, datasetEncryptGeneratedRows_1.encryptGeneratedDatasetRows)(results.testCaseVars, fields, (0, datasetDynamicDatasetLanguage_1.getDatasetLanguageForPresidio)(langConfig));
            }
            catch (encryptionError) {
                logger_1.default.error(`Encryption failed, returning original results: ${encryptionError}`);
            }
        }
        res.status(201).json({
            testCaseVars: encryptedTestCaseVars,
            totalRows: results.totalRows,
            personas: results.personas,
            tokenUsages: datasetTokenUsage,
        });
    }
    catch (error) {
        logger_1.default.error(`Error generating dataset: ${error}`);
        if (error instanceof datasetService_1.DatasetRestrictionError) {
            res.status(400).json({
                success: false,
                message: error.message,
                showToast: error.showToast,
                toastMetadata: error.toastMetadata,
            });
            return;
        }
        const message = error instanceof Error && error.message.trim()
            ? error.message
            : 'Failed to generate dataset';
        res.status(500).json({ error: message, message });
    }
};
exports.generateNewDataset = generateNewDataset;
//# sourceMappingURL=generate-dataset.js.map