"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const commandline_1 = require("../../util/commandline");
const apis_1 = require("../../util/apis");
const interaction_1 = require("../../util/interaction");
const util_1 = require("util");
const validation_utils_1 = require("./lib/validation-utils");
const chalk = require("chalk");
const debug = require("debug")("appcenter-cli:commands:codepush:promote");
let CodePushPromoteCommand = class CodePushPromoteCommand extends commandline_1.AppCommand {
    constructor(args) {
        super(args);
    }
    run(client) {
        return __awaiter(this, void 0, void 0, function* () {
            const app = this.app;
            const rollout = Number(this.rollout);
            if (this.rollout != null && (!Number.isSafeInteger(rollout) || !validation_utils_1.isValidRollout(rollout))) {
                return commandline_1.failure(commandline_1.ErrorCodes.Exception, `Rollout value should be integer value between ${chalk.bold("1")} and ${chalk.bold("100")}.`);
            }
            if (this.targetBinaryRange != null && !validation_utils_1.isValidRange(this.targetBinaryRange)) {
                return commandline_1.failure(commandline_1.ErrorCodes.Exception, "Invalid binary version(s) for a release.");
            }
            const promote = {
                targetBinaryRange: this.targetBinaryRange,
                description: this.description,
                label: this.label,
                isDisabled: this.isDisabled,
                isMandatory: this.isMandatory,
            };
            if (this.rollout != null) {
                promote.rollout = rollout;
            }
            try {
                debug("Promote CodePush release");
                yield interaction_1.out.progress("Promoting CodePush release...", apis_1.clientRequest((cb) => client.codePushDeployments.promote(this.sourceDeploymentName, this.destinationDeploymentName, app.ownerName, app.appName, { release: promote }, cb)));
            }
            catch (error) {
                if (error.response.statusCode === 409 && this.disableDuplicateReleaseError) {
                    // 409 (Conflict) status code means that uploaded package is identical
                    // to the contents of the specified deployment's current release
                    console.warn(chalk.yellow("[Warning] " + error.response.body));
                    return commandline_1.success();
                }
                else {
                    debug(`Failed to promote CodePush release - ${util_1.inspect(error)}`);
                    return commandline_1.failure(commandline_1.ErrorCodes.Exception, error.response.body);
                }
            }
            interaction_1.out.text(`Successfully promoted ${this.label ? `'${this.label}' of` : ""} the '${this.sourceDeploymentName}' deployment of the '${this.identifier}' app to the '${this.destinationDeploymentName}' deployment.`);
            return commandline_1.success();
        });
    }
};
__decorate([
    commandline_1.help("Specifies destination deployment name"),
    commandline_1.required,
    commandline_1.longName("destination-deployment-name"),
    commandline_1.shortName("d"),
    commandline_1.hasArg
], CodePushPromoteCommand.prototype, "destinationDeploymentName", void 0);
__decorate([
    commandline_1.help("Specifies source deployment name"),
    commandline_1.required,
    commandline_1.shortName("s"),
    commandline_1.longName("source-deployment-name"),
    commandline_1.hasArg
], CodePushPromoteCommand.prototype, "sourceDeploymentName", void 0);
__decorate([
    commandline_1.help("Specifies description of the changes made to the app with this release"),
    commandline_1.longName("description"),
    commandline_1.hasArg
], CodePushPromoteCommand.prototype, "description", void 0);
__decorate([
    commandline_1.help("Allows you to pick the specified label from the source deployment and promote it to the destination deployment"),
    commandline_1.shortName("l"),
    commandline_1.longName("label"),
    commandline_1.hasArg
], CodePushPromoteCommand.prototype, "label", void 0);
__decorate([
    commandline_1.help("Specifies whether this release should be considered mandatory. (Putting -m flag means mandatory)"),
    commandline_1.shortName("m"),
    commandline_1.longName("mandatory")
], CodePushPromoteCommand.prototype, "isMandatory", void 0);
__decorate([
    commandline_1.help("Specifies whether this release should be immediately downloadable. (Putting -x flag means disabled)"),
    commandline_1.shortName("x"),
    commandline_1.longName("disabled")
], CodePushPromoteCommand.prototype, "isDisabled", void 0);
__decorate([
    commandline_1.help("Specifies that if the update is identical to the latest release on the deployment, the CLI should generate a warning instead of an error"),
    commandline_1.longName("disable-duplicate-release-error")
], CodePushPromoteCommand.prototype, "disableDuplicateReleaseError", void 0);
__decorate([
    commandline_1.help("Specifies percentage of users this release should be immediately available to. (The specified number must be an integer between 1 and 100)"),
    commandline_1.shortName("r"),
    commandline_1.longName("rollout"),
    commandline_1.hasArg
], CodePushPromoteCommand.prototype, "rollout", void 0);
__decorate([
    commandline_1.help("Specifies binary app version(s) that specifies this release is targeting for. (The value must be a semver expression such as 1.1.0, ~1.2.3)"),
    commandline_1.shortName("t"),
    commandline_1.longName("target-binary-version"),
    commandline_1.hasArg
], CodePushPromoteCommand.prototype, "targetBinaryRange", void 0);
CodePushPromoteCommand = __decorate([
    commandline_1.help("Create a new release for the destination deployment, which includes the exact code and metadata from the latest release of the source deployment")
], CodePushPromoteCommand);
exports.default = CodePushPromoteCommand;
