"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($buildDataService, $buildController, $devicesService, $deployController, $options, $projectData) {
        this.$buildDataService = $buildDataService;
        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 outputPath = additionalOptions && additionalOptions.getOutputDirectory && additionalOptions.getOutputDirectory({
                    platform: d.deviceInfo.platform,
                    emulator: d.isEmulator,
                    projectDir: this.$projectData.projectDir
                });
                const buildData = this.$buildDataService.getBuildData(this.$projectData.projectDir, d.deviceInfo.platform, Object.assign({}, this.$options, { outputPath, buildForDevice: !d.isEmulator }));
                const buildAction = additionalOptions && additionalOptions.buildPlatform ?
                    additionalOptions.buildPlatform.bind(additionalOptions.buildPlatform, d.deviceInfo.platform, buildData, this.$projectData) :
                    this.$buildController.prepareAndBuild.bind(this.$buildController, d.deviceInfo.platform, buildData, this.$projectData);
                const info = {
                    identifier: d.deviceInfo.identifier,
                    buildAction,
                    debuggingEnabled: additionalOptions && additionalOptions.deviceDebugMap && additionalOptions.deviceDebugMap[d.deviceInfo.identifier],
                    debugOptions: this.$options,
                    skipNativePrepare: additionalOptions && additionalOptions.skipNativePrepare,
                    buildData
                };
                return info;
            });
            yield this.$deployController.deploy({
                buildData: this.$buildDataService.getBuildData(this.$projectData.projectDir, platform, Object.assign({}, this.$options, { skipWatcher: !this.$options.watch })),
                deviceDescriptors
            });
        });
    }
}
exports.DeployCommandHelper = DeployCommandHelper;
$injector.register("deployCommandHelper", DeployCommandHelper);
