"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addEnvironmentBuildFilesOptions = addEnvironmentBuildFilesOptions;
exports.createDeploymentForBuildFile = createDeploymentForBuildFile;
const constants_1 = 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 mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
exports.command = `add <${constants_1.CommandArgId}>`;
exports.describe = 'Associate mobile build file with an environment';
exports.builder = (yargs) => {
    addEnvironmentBuildFilesOptions(yargs)
        .positional(constants_1.CommandArgId, {
        describe: 'ID of environment to add mobile build file associations to',
        type: 'string',
    })
        .demandOption(constants_1.CommandArgApplicationId)
        .demandOption(constants_1.CommandArgMobileBuildFileId);
};
exports.handler = (0, cliUtil_1.failWrapper)(createDeployment);
function addEnvironmentBuildFilesOptions(yargs) {
    return yargs
        .option(constants_1.CommandArgApplicationId, {
        describe: 'ID of application to associate with this environment',
        nargs: 1,
        type: 'string',
    })
        .option(constants_1.CommandArgMobileBuildFileId, {
        describe: 'mobile build file ID to associate with the application-environment pair',
        nargs: 1,
        type: 'string',
    });
}
async function createDeployment(parsed) {
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const applicationId = parsed[constants_1.CommandArgApplicationId];
    const environmentId = parsed[constants_1.CommandArgId];
    const buildFileId = parsed[constants_1.CommandArgMobileBuildFileId];
    try {
        await apiClient.getApplication(applicationId);
        await apiClient.getMobileAppFile(buildFileId);
        const environment = await apiClient.getEnvironment(environmentId);
        await createDeploymentForBuildFile(apiClient, environment, applicationId, buildFileId);
        return 'done';
    }
    catch (error) {
        throw new Error(`Error adding build file for environment ${parsed.id}: ${error}`);
    }
}
async function createDeploymentForBuildFile(apiClient, environment, applicationId, buildFileId) {
    try {
        const deployment = {
            organization_id: environment.organization_id,
            environment_id: environment.id,
            application_id: applicationId,
            mobile_app_file_id: buildFileId,
            deployment_type: mablApi_1.Deployment.DeploymentTypeEnum.MobileApplication,
        };
        await apiClient.createDeployment(deployment);
        loggingProvider_1.logger.info(chalk_1.default.white(`Associated ${buildFileId} with environment ${environment.id} and application ${applicationId}`));
    }
    catch (error) {
        loggingProvider_1.logger.error(error);
        const reRunCommandString = `mabl environments build-files add ${environment.id} --applicationId ${applicationId} --build-file-id ${buildFileId}`;
        loggingProvider_1.logger.info(chalk_1.default.white(`Associating build file with environment failed. You can reattempt this association by running ${chalk_1.default.bold(reRunCommandString)}`));
    }
}
