"use strict";
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 });
exports.UpdatePlatformCommand = void 0;
const _ = require("lodash");
const yok_1 = require("../common/yok");
class UpdatePlatformCommand {
    constructor($errors, $options, $platformEnvironmentRequirements, $platformCommandHelper, $platformValidationService, $projectData) {
        this.$errors = $errors;
        this.$options = $options;
        this.$platformEnvironmentRequirements = $platformEnvironmentRequirements;
        this.$platformCommandHelper = $platformCommandHelper;
        this.$platformValidationService = $platformValidationService;
        this.$projectData = $projectData;
        this.allowedParameters = [];
        this.$projectData.initializeProjectData();
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.$platformCommandHelper.updatePlatforms(args, this.$projectData);
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!args || args.length === 0) {
                this.$errors.failWithHelp("No platform specified. Please specify platforms to update.");
            }
            _.each(args, (arg) => {
                const platform = arg.split("@")[0];
                this.$platformValidationService.validatePlatform(platform, this.$projectData);
            });
            for (const arg of args) {
                const [platform, versionToBeInstalled] = arg.split("@");
                const checkEnvironmentRequirementsInput = {
                    platform,
                    options: this.$options,
                };
                // If version is not specified, we know the command will install the latest compatible Android runtime.
                // The latest compatible Android runtime supports Java version, so we do not need to pass it here.
                // Passing projectDir to the @nativescript/doctor validation will cause it to check the runtime from the current package.json
                // So in this case, where we do not want to validate the runtime, just do not pass both projectDir and runtimeVersion.
                if (versionToBeInstalled) {
                    checkEnvironmentRequirementsInput.projectDir = this.$projectData.projectDir;
                    checkEnvironmentRequirementsInput.runtimeVersion = versionToBeInstalled;
                }
                yield this.$platformEnvironmentRequirements.checkEnvironmentRequirements(checkEnvironmentRequirementsInput);
            }
            return true;
        });
    }
}
exports.UpdatePlatformCommand = UpdatePlatformCommand;
yok_1.injector.registerCommand("platform|update", UpdatePlatformCommand);
//# sourceMappingURL=update-platform.js.map