"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
class DeployCommandHelper {
    constructor($buildController, $devicesService, $deployController, $options, $projectData) {
        this.$buildController = $buildController;
        this.$devicesService = $devicesService;
        this.$deployController = $deployController;
        this.$options = $options;
        this.$projectData = $projectData;
    }
    deploy(platform, additionalOptions) {
        return __awaiter(this, void 0, void 0, function* () {
            const emulator = this.$options.emulator;
            yield this.$devicesService.initialize({
                deviceId: this.$options.device,
                platform,
                emulator,
                skipInferPlatform: !platform,
                sdk: this.$options.sdk
            });
            const devices = this.$devicesService.getDeviceInstances()
                .filter(d => !platform || d.deviceInfo.platform.toLowerCase() === platform.toLowerCase());
            const deviceDescriptors = devices
                .map(d => {
                const buildConfig = {
                    buildForDevice: !d.isEmulator,
                    iCloudContainerEnvironment: this.$options.iCloudContainerEnvironment,
                    projectDir: this.$options.path,
                    clean: this.$options.clean,
                    teamId: this.$options.teamId,
                    device: this.$options.device,
                    provision: this.$options.provision,
                    release: this.$options.release,
                    keyStoreAlias: this.$options.keyStoreAlias,
                    keyStorePath: this.$options.keyStorePath,
                    keyStoreAliasPassword: this.$options.keyStoreAliasPassword,
                    keyStorePassword: this.$options.keyStorePassword
                };
                const buildAction = additionalOptions && additionalOptions.buildPlatform ?
                    additionalOptions.buildPlatform.bind(additionalOptions.buildPlatform, d.deviceInfo.platform, buildConfig, this.$projectData) :
                    this.$buildController.prepareAndBuild.bind(this.$buildController, d.deviceInfo.platform, buildConfig, this.$projectData);
                const outputPath = additionalOptions && additionalOptions.getOutputDirectory && additionalOptions.getOutputDirectory({
                    platform: d.deviceInfo.platform,
                    emulator: d.isEmulator,
                    projectDir: this.$projectData.projectDir
                });
                const info = {
                    identifier: d.deviceInfo.identifier,
                    buildAction,
                    debuggingEnabled: additionalOptions && additionalOptions.deviceDebugMap && additionalOptions.deviceDebugMap[d.deviceInfo.identifier],
                    debugOptions: this.$options,
                    outputPath,
                    skipNativePrepare: additionalOptions && additionalOptions.skipNativePrepare,
                };
                return info;
            });
            const liveSyncInfo = {
                projectDir: this.$projectData.projectDir,
                skipWatcher: !this.$options.watch,
                clean: this.$options.clean,
                release: this.$options.release,
                env: this.$options.env,
                timeout: this.$options.timeout,
                useHotModuleReload: this.$options.hmr,
                force: this.$options.force,
                emulator: this.$options.emulator
            };
            yield this.$deployController.deploy({
                liveSyncInfo,
                deviceDescriptors
            });
        });
    }
}
exports.DeployCommandHelper = DeployCommandHelper;
$injector.register("deployCommandHelper", DeployCommandHelper);
