"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
const constants_1 = require("../../constants");
const constants_2 = require("@mablhq-internal/runtime/core/constants");
const cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
const chalk_1 = __importDefault(require("chalk"));
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
const create_1 = require("./create");
exports.command = 'update <id>';
exports.describe =
    'Update a mabl environment. Note: This overwrites all existing environment variables set for the environment.';
exports.builder = (yargs) => {
    (0, create_1.addUpdateEnvCommands)(yargs, false).positional(constants_2.CommandArgId, {
        describe: 'ID of environment to update',
        type: 'string',
        nargs: 1,
        demand: 'id argument required',
    });
};
exports.handler = (0, cliUtil_1.failWrapper)(updateEnvironment);
async function updateEnvironment(parsed) {
    let environment;
    try {
        const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
        environment = {
            name: parsed[constants_1.CommandArgName],
            description: parsed[constants_1.CommandArgDescription],
            preview: parsed[constants_1.CommandArgPreview],
            link_agent_label: parsed[constants_1.CommandArgLinkLabel],
            use_link_agent: !!parsed[constants_1.CommandArgLinkLabel],
            link_bypass_mabl_proxy: parsed[constants_1.CommandArgLinkBypass],
            source_control_tag_target: parsed[constants_2.CommandArgMablBranch],
            use_source_control_tag: !!parsed[constants_2.CommandArgMablBranch],
            variables: parsed[constants_1.CommandArgVariables],
        };
        environment = await apiClient.updateEnvironment(parsed[constants_2.CommandArgId], environment);
        loggingProvider_1.logger.info(chalk_1.default.green(`Environment ${chalk_1.default.bold(environment.id ?? '')} UPDATED`));
    }
    catch (error) {
        throw new Error(`Error updating environment: ${error}`);
    }
    return environment.id;
}
