"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 });
exports.RunAndroidCommand = exports.RunIosCommand = exports.RunCommandBase = void 0;
const constants_1 = require("../common/constants");
const decorators_1 = require("../common/decorators");
const helpers_1 = require("../common/helpers");
const yok_1 = require("../common/yok");
const constants_2 = require("../constants");
class RunCommandBase {
    constructor($devicePlatformsConstants, $errors, $hostInfo, $liveSyncCommandHelper, $migrateController, $options, $projectData, $keyCommandHelper) {
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$errors = $errors;
        this.$hostInfo = $hostInfo;
        this.$liveSyncCommandHelper = $liveSyncCommandHelper;
        this.$migrateController = $migrateController;
        this.$options = $options;
        this.$projectData = $projectData;
        this.$keyCommandHelper = $keyCommandHelper;
        this.liveSyncCommandHelperAdditionalOptions = {};
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.$liveSyncCommandHelper.executeCommandLiveSync(this.platform, this.liveSyncCommandHelperAdditionalOptions);
            if (process.env.NS_IS_INTERACTIVE) {
                this.$keyCommandHelper.attachKeyCommands(this.platform, "run");
            }
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.length) {
                this.$errors.failWithHelp(constants_1.ERROR_NO_VALID_SUBCOMMAND_FORMAT, "run");
            }
            this.platform = args[0] || this.platform;
            if (!this.platform && !this.$hostInfo.isDarwin) {
                this.platform = this.$devicePlatformsConstants.Android;
            }
            this.$projectData.initializeProjectData();
            const platforms = this.platform
                ? [this.platform]
                : [
                    this.$devicePlatformsConstants.Android,
                    this.$devicePlatformsConstants.iOS,
                ];
            if (!this.$options.force) {
                yield this.$migrateController.validate({
                    projectDir: this.$projectData.projectDir,
                    platforms,
                });
            }
            yield this.$liveSyncCommandHelper.validatePlatform(this.platform);
            return true;
        });
    }
}
exports.RunCommandBase = RunCommandBase;
yok_1.injector.registerCommand("run|*all", RunCommandBase);
class RunIosCommand {
    get runCommand() {
        const runCommand = this.$injector.resolve(RunCommandBase);
        runCommand.platform = this.platform;
        return runCommand;
    }
    get platform() {
        return this.$devicePlatformsConstants.iOS;
    }
    constructor($devicePlatformsConstants, $errors, $injector, $options, $platformValidationService, $projectDataService) {
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$errors = $errors;
        this.$injector = $injector;
        this.$options = $options;
        this.$platformValidationService = $platformValidationService;
        this.$projectDataService = $projectDataService;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.runCommand.execute(args);
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const projectData = this.$projectDataService.getProjectData();
            if (!this.$platformValidationService.isPlatformSupportedForOS(this.$devicePlatformsConstants.iOS, projectData)) {
                this.$errors.fail(`Applications for platform ${this.$devicePlatformsConstants.iOS} can not be built on this OS`);
            }
            const result = (yield this.runCommand.canExecute(args)) &&
                (yield this.$platformValidationService.validateOptions(this.$options.provision, this.$options.teamId, projectData, this.$devicePlatformsConstants.iOS.toLowerCase()));
            return result;
        });
    }
}
__decorate([
    (0, decorators_1.cache)()
], RunIosCommand.prototype, "runCommand", null);
exports.RunIosCommand = RunIosCommand;
yok_1.injector.registerCommand("run|ios", RunIosCommand);
class RunAndroidCommand {
    get runCommand() {
        const runCommand = this.$injector.resolve(RunCommandBase);
        runCommand.platform = this.platform;
        return runCommand;
    }
    get platform() {
        return this.$devicePlatformsConstants.Android;
    }
    constructor($devicePlatformsConstants, $errors, $injector, $options, $platformValidationService, $projectData) {
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$errors = $errors;
        this.$injector = $injector;
        this.$options = $options;
        this.$platformValidationService = $platformValidationService;
        this.$projectData = $projectData;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.runCommand.execute(args);
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.runCommand.canExecute(args);
            if (!this.$platformValidationService.isPlatformSupportedForOS(this.$devicePlatformsConstants.Android, this.$projectData)) {
                this.$errors.fail(`Applications for platform ${this.$devicePlatformsConstants.Android} can not be built on this OS`);
            }
            if ((this.$options.release || this.$options.aab) &&
                !(0, helpers_1.hasValidAndroidSigning)(this.$options)) {
                if (this.$options.release) {
                    this.$errors.failWithHelp(constants_2.ANDROID_RELEASE_BUILD_ERROR_MESSAGE);
                }
                else {
                    this.$errors.failWithHelp(constants_2.ANDROID_APP_BUNDLE_SIGNING_ERROR_MESSAGE);
                }
            }
            return this.$platformValidationService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, this.$devicePlatformsConstants.Android.toLowerCase());
        });
    }
}
__decorate([
    (0, decorators_1.cache)()
], RunAndroidCommand.prototype, "runCommand", null);
exports.RunAndroidCommand = RunAndroidCommand;
yok_1.injector.registerCommand("run|android", RunAndroidCommand);
//# sourceMappingURL=run.js.map