"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
class RunCommandBase {
    constructor($platformService, $usbLiveSyncService, $projectData, $options) {
        this.$platformService = $platformService;
        this.$usbLiveSyncService = $usbLiveSyncService;
        this.$projectData = $projectData;
        this.$options = $options;
        this.$projectData.initializeProjectData();
    }
    executeCore(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const appFilesUpdaterOptions = { bundle: this.$options.bundle, release: this.$options.release };
            const deployOptions = {
                clean: this.$options.clean,
                device: this.$options.device,
                emulator: this.$options.emulator,
                projectDir: this.$options.path,
                platformTemplate: this.$options.platformTemplate,
                release: this.$options.release,
                provision: this.$options.provision,
                teamId: this.$options.teamId,
                keyStoreAlias: this.$options.keyStoreAlias,
                keyStoreAliasPassword: this.$options.keyStoreAliasPassword,
                keyStorePassword: this.$options.keyStorePassword,
                keyStorePath: this.$options.keyStorePath
            };
            yield this.$platformService.deployPlatform(args[0], appFilesUpdaterOptions, deployOptions, this.$projectData, { provision: this.$options.provision, sdk: this.$options.sdk });
            if (this.$options.bundle) {
                this.$options.watch = false;
            }
            if (this.$options.release) {
                const deployOpts = {
                    device: this.$options.device,
                    emulator: this.$options.emulator,
                    justlaunch: this.$options.justlaunch,
                };
                return this.$platformService.runPlatform(args[0], deployOpts, this.$projectData);
            }
            return this.$usbLiveSyncService.liveSync(args[0], this.$projectData);
        });
    }
}
exports.RunCommandBase = RunCommandBase;
class RunIosCommand extends RunCommandBase {
    constructor($platformService, $platformsData, $usbLiveSyncService, $projectData, $options) {
        super($platformService, $usbLiveSyncService, $projectData, $options);
        this.$platformsData = $platformsData;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.executeCore([this.$platformsData.availablePlatforms.iOS]);
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            return args.length === 0 && (yield this.$platformService.validateOptions(this.$options.provision, this.$projectData, this.$platformsData.availablePlatforms.iOS));
        });
    }
}
exports.RunIosCommand = RunIosCommand;
$injector.registerCommand("run|ios", RunIosCommand);
class RunAndroidCommand extends RunCommandBase {
    constructor($platformService, $platformsData, $usbLiveSyncService, $projectData, $options, $errors) {
        super($platformService, $usbLiveSyncService, $projectData, $options);
        this.$platformsData = $platformsData;
        this.$errors = $errors;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.executeCore([this.$platformsData.availablePlatforms.Android]);
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.$options.release && (!this.$options.keyStorePath || !this.$options.keyStorePassword || !this.$options.keyStoreAlias || !this.$options.keyStoreAliasPassword)) {
                this.$errors.fail("When producing a release build, you need to specify all --key-store-* options.");
            }
            return args.length === 0 && (yield this.$platformService.validateOptions(this.$options.provision, this.$projectData, this.$platformsData.availablePlatforms.Android));
        });
    }
}
exports.RunAndroidCommand = RunAndroidCommand;
$injector.registerCommand("run|android", RunAndroidCommand);
