"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 LiveSyncCommandHelper {
    constructor($platformService, $projectData, $options, $liveSyncService, $iosDeviceOperations, $mobileHelper, $platformsData, $analyticsService, $errors) {
        this.$platformService = $platformService;
        this.$projectData = $projectData;
        this.$options = $options;
        this.$liveSyncService = $liveSyncService;
        this.$iosDeviceOperations = $iosDeviceOperations;
        this.$mobileHelper = $mobileHelper;
        this.$platformsData = $platformsData;
        this.$analyticsService = $analyticsService;
        this.$errors = $errors;
        this.$analyticsService.setShouldDispose(this.$options.justlaunch || !this.$options.watch);
    }
    getPlatformsForOperation(platform) {
        const availablePlatforms = platform ? [platform] : _.values(this.$platformsData.availablePlatforms);
        return availablePlatforms;
    }
    executeLiveSyncOperation(devices, platform, deviceDebugMap) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!devices || !devices.length) {
                if (platform) {
                    this.$errors.failWithoutHelp("Unable to find applicable devices to execute operation. Ensure connected devices are trusted and try again.");
                }
                else {
                    this.$errors.failWithoutHelp("Unable to find applicable devices to execute operation and unable to start emulator when platform is not specified.");
                }
            }
            const workingWithiOSDevices = !platform || this.$mobileHelper.isiOSPlatform(platform);
            const shouldKeepProcessAlive = this.$options.watch || !this.$options.justlaunch;
            if (workingWithiOSDevices && shouldKeepProcessAlive) {
                this.$iosDeviceOperations.setShouldDispose(false);
            }
            if (this.$options.release || this.$options.bundle) {
                yield this.runInReleaseMode(platform);
                return;
            }
            const deviceDescriptors = devices
                .map(d => {
                const info = {
                    identifier: d.deviceInfo.identifier,
                    platformSpecificOptions: this.$options,
                    buildAction: () => __awaiter(this, void 0, void 0, function* () {
                        const buildConfig = {
                            buildForDevice: !d.isEmulator,
                            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
                        };
                        yield this.$platformService.buildPlatform(d.deviceInfo.platform, buildConfig, this.$projectData);
                        const result = yield this.$platformService.lastOutputPath(d.deviceInfo.platform, buildConfig, this.$projectData);
                        return result;
                    }),
                    debugggingEnabled: deviceDebugMap && deviceDebugMap[d.deviceInfo.identifier],
                    debugOptions: this.$options
                };
                return info;
            });
            const liveSyncInfo = {
                projectDir: this.$projectData.projectDir,
                skipWatcher: !this.$options.watch,
                watchAllFiles: this.$options.syncAllFiles,
                clean: this.$options.clean,
                env: this.$options.env
            };
            yield this.$liveSyncService.liveSync(deviceDescriptors, liveSyncInfo);
        });
    }
    runInReleaseMode(platform) {
        return __awaiter(this, void 0, void 0, function* () {
            const runPlatformOptions = {
                device: this.$options.device,
                emulator: this.$options.emulator,
                justlaunch: this.$options.justlaunch
            };
            const deployOptions = _.merge({
                projectDir: this.$projectData.projectDir,
                clean: true,
            }, this.$options.argv);
            const availablePlatforms = this.getPlatformsForOperation(platform);
            for (const currentPlatform of availablePlatforms) {
                const deployPlatformInfo = {
                    platform: currentPlatform,
                    appFilesUpdaterOptions: this.$options,
                    deployOptions,
                    projectData: this.$projectData,
                    config: this.$options,
                    env: this.$options.env
                };
                yield this.$platformService.deployPlatform(deployPlatformInfo);
                yield this.$platformService.startApplication(currentPlatform, runPlatformOptions, this.$projectData.projectId);
                this.$platformService.trackProjectType(this.$projectData);
            }
        });
    }
}
exports.LiveSyncCommandHelper = LiveSyncCommandHelper;
$injector.register("liveSyncCommandHelper", LiveSyncCommandHelper);
