"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildPlanningArtifacts = buildPlanningArtifacts;
exports.runCloudTestGeneration = runCloudTestGeneration;
const mablApi_1 = require("../../mablApi");
const uuid_1 = require("uuid");
function buildPlanningArtifacts(artifact) {
    const planningArtifacts = [];
    const testInformation = artifact.test_information;
    if (testInformation) {
        planningArtifacts.push({
            id: (0, uuid_1.v4)(),
            description: 'Test information',
            artifact_type: 'inline',
            name: 'test_information',
            artifact_data: {
                type: 'json',
                value: testInformation,
            },
        });
    }
    const testOutline = artifact.agent_state?.entireTestPlan;
    if (testOutline) {
        planningArtifacts.push({
            id: (0, uuid_1.v4)(),
            description: 'Test outline',
            artifact_type: 'inline',
            name: 'test_outline',
            artifact_data: {
                type: 'json',
                value: testOutline,
            },
        });
    }
    return planningArtifacts;
}
async function runCloudTestGeneration(params, apiClient) {
    const { url, testName, workspaceId, environmentId, applicationId, credentialsId, testIntent, snapshot, } = params;
    const planningSessionResponse = await apiClient.generateTestPlan({
        workspaceId,
        messages: [
            {
                role: mablApi_1.ChatMessageRoleEnum.User,
                message: testIntent,
                timestamp: Date.now(),
            },
        ],
        artifact: {
            version: 0,
            timestamp: Date.now(),
        },
        generationReferences: {},
        trainingSessionId: `cloud-${Date.now()}`,
    });
    const planningArtifacts = [];
    const testInformation = planningSessionResponse.artifact.test_information;
    if (testInformation) {
        planningArtifacts.push({
            id: (0, uuid_1.v4)(),
            description: 'Test information',
            artifact_type: 'inline',
            name: 'test_information',
            artifact_data: {
                type: 'json',
                value: {
                    ...testInformation,
                    name: testName,
                    url_override: url,
                    application_id: applicationId,
                    environment_id: environmentId,
                    credentials_id: credentialsId ?? testInformation.credentials_id,
                },
            },
        });
    }
    const testOutline = planningSessionResponse.artifact.agent_state?.entireTestPlan;
    if (testOutline) {
        planningArtifacts.push({
            id: (0, uuid_1.v4)(),
            description: 'Test outline',
            artifact_type: 'inline',
            name: 'test_outline',
            artifact_data: {
                type: 'json',
                value: testOutline,
            },
        });
    }
    const initiateRequest = {
        is_cloud: true,
        planning_artifacts: planningArtifacts,
        planning_messages: planningSessionResponse.messages,
        application_id: applicationId,
        environment_id: environmentId,
        snapshot,
    };
    const initiateResponse = await apiClient.initiateTestAuthoringSession(workspaceId, initiateRequest);
    return {
        sessionId: initiateResponse.session_id,
        instanceId: initiateResponse.instance_id,
    };
}
