"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeTest = exports.testOutputSchema = void 0;
exports.needCredentialsError = needCredentialsError;
exports.getViewTestUrl = getViewTestUrl;
exports.getTestOutputUrl = getTestOutputUrl;
exports.getCreateTestOutputText = getCreateTestOutputText;
const zod_1 = require("zod");
const env_1 = require("@mablhq-internal/runtime/env/env");
const openUtils_1 = require("@mablhq-internal/runtime/core/trainer/openUtils");
exports.testOutputSchema = zod_1.z.object({
    id: zod_1.z.string(),
    name: zod_1.z.string(),
    description: zod_1.z.string().optional(),
    enabled: zod_1.z.boolean().optional(),
    testType: zod_1.z.string().optional(),
    tags: zod_1.z.array(zod_1.z.string()).optional(),
    labels: zod_1.z.array(zod_1.z.string()).optional(),
    applicationId: zod_1.z.string().optional(),
    environmentId: zod_1.z.string().optional(),
    url: zod_1.z.string().optional(),
    intent: zod_1.z.string().optional(),
    viewTestUrl: zod_1.z.string().optional(),
    credentialsId: zod_1.z.string().optional(),
});
const normalizeTest = (test) => ({
    id: test.invariant_id,
    name: test.name,
    description: test.description,
    enabled: test.enabled,
    testType: test.test_type,
    tags: test.tags?.map((tag) => tag.name) ?? [],
    labels: test.labels?.map((label) => label.name) ?? [],
    applicationId: test.application_id,
    environmentId: test.environment_id,
    url: test.url,
    intent: test.intent,
    viewTestUrl: getViewTestUrl(test.workspace_id, test.invariant_id),
    credentialsId: test.credentials_id,
});
exports.normalizeTest = normalizeTest;
async function needCredentialsError(api, workspaceId) {
    const allCredentials = await api.getCredentials(workspaceId, 500);
    const nonCloudCredentials = allCredentials
        .filter((credential) => !credential.cloud_only)
        .map((cred) => `- ${cred.name} (ID: ${cred.id}, Type: ${cred.type})`)
        .join('\n');
    return `Credentials are required to run the test. Please provide a valid credentialsId. Here are the available non-cloud credentials:\n\n${nonCloudCredentials}\n\nIf you want to use one of these credentials, please specify the credentialsId parameter with the desired credential ID.`;
}
function getViewTestUrl(workspaceId, invariantId) {
    return `${env_1.BASE_APP_URL}/workspaces/${workspaceId}/train/tests/${invariantId}/current`;
}
function getTestOutputUrl(workspaceId, testRunId) {
    return `${env_1.BASE_APP_URL}/workspaces/${workspaceId}/output/test-runs/${testRunId}`;
}
function getCreateTestOutputText(trainerUrl) {
    return process.platform === openUtils_1.WIN32_PLATFORM
        ? `Tell the user to open the following URL to launch the mabl trainer: ${trainerUrl}. Show it using a link, trying to hide the full URL. If you have a tool that can open a link use it.`
        : `A mabl trainer was launched. If you don't see the trainer running, open the following URL: ${trainerUrl} to open the trainer manually (Show the URL to the user using and explain to the user how to open it. If you have a tool that can open a link use it.).`;
}
