"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const js_yaml_1 = require("js-yaml");
const env_1 = require("@mablhq-internal/runtime/env/env");
const mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
const util_1 = require("../../commandUtil/util");
const cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
const constants_1 = require("../../constants");
const constants_2 = require("@mablhq-internal/runtime/core/constants");
const markdownUtil_1 = require("@mablhq-internal/runtime/util/markdownUtil");
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
const chalk = require('chalk');
exports.command = 'describe <id>';
exports.describe = 'Describe a specific mabl deployment event';
exports.builder = {
    [constants_1.CommandArgOutput]: {
        alias: constants_1.CommandArgAliases.OutputType,
        describe: 'Specify and alternate output for the deployment data',
        choices: [...constants_1.DefaultOutputFormatChoices, constants_2.OutputFormats.Markdown],
    },
};
exports.handler = (0, cliUtil_1.failWrapper)(getDeployment);
async function getDeployment(parsed) {
    const output = parsed.output;
    const deploymentId = parsed.id;
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const deployment = await apiClient.getDeploymentEvent(deploymentId);
    switch (output) {
        case constants_2.OutputFormats.Markdown:
            if (deployment.run_result) {
                const executionResult = deployment.run_result;
                loggingProvider_1.logger.info((0, markdownUtil_1.formatResultsAsGithubMarkdown)(executionResult));
            }
            break;
        case constants_2.OutputFormats.Json:
            loggingProvider_1.logger.info(JSON.stringify(deployment, null, 2));
            break;
        case constants_2.OutputFormats.Yaml:
            loggingProvider_1.logger.info((0, js_yaml_1.dump)(deployment));
            break;
        default:
            if (deployment.run_result) {
                const { executions, ...rest } = deployment.run_result;
                deployment.run_result = rest;
            }
            delete deployment.triggered_plan_run_summaries;
            const workspaceId = await (0, util_1.getWorkspaceIdFromAppOrEnv)(apiClient, deployment.application_id, deployment.environment_id);
            loggingProvider_1.logger.info((0, js_yaml_1.dump)(deployment));
            loggingProvider_1.logger.info(`VIEW RESULTS: ${chalk.cyan(getOutputLink(workspaceId, deployment.id))}`);
            break;
    }
    return deployment.id;
}
function getOutputLink(workspaceId, deploymentId) {
    return `${env_1.BASE_APP_URL}/workspaces/${workspaceId}/events/${deploymentId}`;
}
