"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatetestSuiteLists = void 0;
const handleHttpApiStructure_1 = require("../handleHttpApiStructure");
const buildProviderForEval_1 = require("../customAIApplication/buildProviderForEval");
/** Distinct non-empty dataset ids referenced on conversation-flow actions (e.g. user response nodes). */
function extractDistinctDatasetIdsFromConversationFlow(conversationFlow) {
    if (!Array.isArray(conversationFlow)) {
        return [];
    }
    const ids = new Set();
    for (const item of conversationFlow) {
        if (item === null || typeof item !== 'object' || Array.isArray(item)) {
            continue;
        }
        const raw = item.datasetId;
        if (typeof raw === 'string' && raw.trim()) {
            ids.add(raw.trim());
        }
    }
    return Array.from(ids);
}
const generatetestSuiteLists = (testSuiteListsParams) => {
    const { promptLibrary, applications, description = '', defaultTest = {}, scenarios = [], evaluateOptions = {}, } = testSuiteListsParams;
    const globalMetrics = promptLibrary?.globalMetrics ?? [];
    const prompts = promptLibrary?.prompts || [];
    // Create providers array and combine all envs
    const { providers, combinedEnv } = applications.reduce((acc, app) => {
        // Handle provider configuration
        const providerConfig = app?.provider_config?.config?.provider;
        let provider;
        if (app?.provider_config?.provider_type === 'agentic') {
            provider = {
                id: `agentic:${providerConfig?.platform}`,
                label: app?.name,
                config: {
                    ...providerConfig,
                },
            };
        }
        else if (app?.provider_config?.provider_type === 'phone') {
            provider = {
                id: 'phone',
                config: {
                    number: providerConfig?.number,
                },
            };
        }
        else if (app?.provider_config?.config?.custom_provider_type === 'HTTP API') {
            provider = (0, handleHttpApiStructure_1.handleHTTPAPIStructure)(providerConfig, app?.name);
        }
        else if (app?.provider_config?.config?.custom_provider_type === 'Websocket') {
            provider = {
                id: providerConfig.url,
                config: {
                    messageTemplate: providerConfig.messageTemplate,
                    transform: providerConfig.transformResponse,
                    timeout: providerConfig.timeoutMs || 10000,
                },
            };
        }
        else if (app?.provider_config?.config?.custom_provider_type === 'Custom AI Application') {
            provider = (0, buildProviderForEval_1.buildCustomAIProviderForEval)(app) ?? providerConfig;
        }
        else {
            provider = providerConfig;
        }
        // Remove env from provider config if it exists
        if (provider?.config?.env) {
            const { env, ...configWithoutEnv } = provider.config;
            provider.config = configWithoutEnv;
            // Merge env into combined env
            Object.assign(acc.combinedEnv, env);
        }
        provider.label = app?.name;
        if (provider) {
            provider.enableEncryption = app?.enableEncryption ?? '0';
            provider.applicationId = app?.id;
            acc.providers.push(provider);
        }
        return acc;
    }, { providers: [], combinedEnv: {} });
    // Create default test suite configuration
    const defaultTestSuite = {
        defaultTest: defaultTest || {},
        description: description || '',
        env: Object.fromEntries(Object.entries(combinedEnv)?.filter(([_, value]) => value !== '')),
        scenarios: scenarios || [],
        evaluateOptions,
        applicationIds: applications?.map((app) => app?.id),
        libraryId: promptLibrary?.id,
        providers,
    };
    const testSuiteLists = [];
    // Run test suite for each prompt
    for (const prompt of prompts) {
        const testSuite = {
            ...defaultTestSuite,
            tests: prompt?.tests?.map((test) => ({
                assert: [...test.assert, ...globalMetrics],
                vars: test?.vars,
                description: test?.description || '',
                dataset: test?.dataset,
            })),
            turns: prompt?.turns,
            conversationFlow: prompt?.conversationFlow,
            prompts: [prompt?.prompt],
            parentPromptId: prompt?.id || '',
            userIntentions: prompt?.userIntentions || '',
            userProfiles: prompt?.userProfiles || '',
            maximumTurns: prompt?.maximumTurns || '',
            minimumTurns: prompt?.minimumTurns || '',
            openingMessage: prompt?.openingMessage || '',
            chatStopCriteria: prompt?.chatStopCriteria || '',
            promptType: prompt?.promptType,
            conversationBuilderType: prompt?.conversation_builder_type,
            datasetIds: prompt?.promptType === 'conversation_builder'
                ? extractDistinctDatasetIdsFromConversationFlow(prompt?.conversationFlow)
                : (prompt?.tests?.map((test) => test?.dataset?.id) ?? []).filter((id) => id != null),
        };
        testSuiteLists.push(testSuite);
    }
    return testSuiteLists;
};
exports.generatetestSuiteLists = generatetestSuiteLists;
//# sourceMappingURL=index.js.map