"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
const testsUtil_1 = require("@mablhq-internal/runtime/core/execution/testsUtil");
const constants_1 = require("../../constants");
const constants_2 = require("@mablhq-internal/runtime/core/constants");
const trainingSessions_1 = require("@mablhq-internal/runtime/core/trainer/trainingSessions");
exports.command = 'edit';
exports.describe = 'Edit a browser test in the Trainer';
exports.builder = (yargs) => {
    yargs
        .example('$0 tests edit --id <id>', 'edit test in the Trainer by id')
        .example('$0 tests edit --run-id <test-run-id>', 'edit a browser test in the Trainer in the context of a test configuration from a cloud run')
        .option(constants_2.CommandArgId, {
        describe: 'the id of the test to edit',
        type: 'string',
    })
        .option(constants_2.CommandArgTestRunId, {
        describe: 'The id of the test run to pull config from',
        type: 'string',
    })
        .option(constants_2.CommandArgMablBranch, {
        describe: 'Branch to edit the mabl test against',
        nargs: 1,
        type: 'string',
    })
        .option(constants_1.CommandArgMablAutoBranch, {
        describe: 'Create a mabl branch when a mabl-branch target is specified that does not exist',
        type: 'boolean',
        default: false,
    })
        .option(constants_2.CommandArgEnvironmentId, {
        alias: constants_1.CommandArgAliases.EnvironmentId,
        describe: 'Mabl environment to edit the test under. Specify to edit the test with environment variables and the latest find information.',
        nargs: 1,
        type: 'string',
    })
        .option(constants_2.CommandArgApplicationId, {
        alias: constants_1.CommandArgAliases.ApplicationId,
        describe: 'Mabl application to edit the test under',
        nargs: 1,
        type: 'string',
    })
        .option('width', {
        describe: 'Set the browser width in pixels',
        default: constants_1.DEFAULT_BROWSER_WIDTH,
        nargs: 1,
        type: 'number',
    })
        .option('height', {
        describe: 'Set the browser height in pixels',
        default: constants_1.DEFAULT_BROWSER_HEIGHT,
        nargs: 1,
        type: 'number',
    })
        .option(constants_2.CommandArgUrl, {
        alias: constants_1.CommandArgAliases.Url,
        describe: 'URL to edit the mabl test against',
        nargs: 1,
        type: 'string',
    })
        .option(constants_2.CommandArgCredentials, {
        alias: constants_1.CommandArgAliases.Credentials,
        describe: 'Credentials ID to edit the test with',
        nargs: 1,
        type: 'string',
    })
        .option(constants_1.CommandArgBrowserDisableIsolation, {
        hidden: true,
        describe: 'Disable cross origin isolation',
        default: false,
        type: 'boolean',
    })
        .option(constants_1.CommandArgBrowserIgnoreCertificateErrors, {
        hidden: true,
        describe: 'Ignore certificate errors',
        default: false,
        type: 'boolean',
    })
        .check((argv) => (0, testsUtil_1.validateRunEditCommand)(argv[constants_2.CommandArgId], argv[constants_2.CommandArgTestRunId]));
};
exports.handler = (0, cliUtil_1.failWrapper)(launchEditTrainingSession);
async function launchEditTrainingSession(parsed) {
    const testRunId = parsed['run-id'];
    const testId = parsed.id;
    const url = parsed.url;
    const credentialsId = parsed['credentials-id'];
    const branchName = parsed['mabl-branch'];
    const width = parsed.width;
    const height = parsed.height;
    const autoBranch = parsed['auto-branch'];
    const environmentId = parsed[constants_2.CommandArgEnvironmentId];
    const applicationId = parsed[constants_2.CommandArgApplicationId];
    const disableIsolation = parsed[constants_1.CommandArgBrowserDisableIsolation];
    const ignoreCertificateErrors = parsed[constants_1.CommandArgBrowserIgnoreCertificateErrors];
    const editTrainingSessionOptions = {
        applicationId,
        autoBranch,
        branchName,
        credentialsId,
        disableIsolation,
        environmentId,
        height,
        ignoreCertificateErrors,
        testId,
        testRunId,
        url,
        width,
    };
    await (0, trainingSessions_1.editTest)(editTrainingSessionOptions);
    return 'done';
}
