"use strict";
const platform_livesync_service_base_1 = require("./platform-livesync-service-base");
class IOSPlatformLiveSyncService extends platform_livesync_service_base_1.PlatformLiveSyncServiceBase {
    constructor(_liveSyncData, $devicesService, $mobileHelper, $logger, $options, $deviceAppDataFactory, $fs, $injector, $projectFilesManager, $projectFilesProvider, $platformService, $liveSyncProvider) {
        super(_liveSyncData, $devicesService, $mobileHelper, $logger, $options, $deviceAppDataFactory, $fs, $injector, $projectFilesManager, $projectFilesProvider, $platformService, $liveSyncProvider);
        this.$devicesService = $devicesService;
        this.$mobileHelper = $mobileHelper;
        this.$logger = $logger;
        this.$options = $options;
        this.$deviceAppDataFactory = $deviceAppDataFactory;
        this.$fs = $fs;
        this.$injector = $injector;
        this.$projectFilesManager = $projectFilesManager;
        this.$projectFilesProvider = $projectFilesProvider;
        this.$platformService = $platformService;
        this.$liveSyncProvider = $liveSyncProvider;
    }
    fullSync(postAction) {
        return (() => {
            let appIdentifier = this.liveSyncData.appIdentifier;
            let platform = this.liveSyncData.platform;
            let projectFilesPath = this.liveSyncData.projectFilesPath;
            let canExecute = this.getCanExecuteAction(platform, appIdentifier);
            let action = (device) => {
                return (() => {
                    let deviceAppData = this.$deviceAppDataFactory.create(appIdentifier, this.$mobileHelper.normalizePlatformName(platform), device);
                    let installed = this.tryInstallApplication(device, deviceAppData).wait();
                    let localToDevicePaths = this.$projectFilesManager.createLocalToDevicePaths(deviceAppData, projectFilesPath, null, this.liveSyncData.excludedProjectDirsAndFiles);
                    let afterSyncAction;
                    this.transferFiles(deviceAppData, localToDevicePaths, this.liveSyncData.projectFilesPath, true).wait();
                    if (installed) {
                        afterSyncAction = () => device.applicationManager.tryStartApplication(deviceAppData.appIdentifier);
                    }
                    else {
                        afterSyncAction = () => this.refreshApplication(deviceAppData, localToDevicePaths);
                    }
                    if (postAction) {
                        this.finishLivesync(deviceAppData).wait();
                        return postAction(deviceAppData, localToDevicePaths).wait();
                    }
                    afterSyncAction().wait();
                    this.finishLivesync(deviceAppData).wait();
                }).future()();
            };
            this.$devicesService.execute(action, canExecute).wait();
        }).future()();
    }
}
$injector.register("iosPlatformLiveSyncServiceLocator", { factory: IOSPlatformLiveSyncService });
