"use strict";
var path = require("path");
var temp = require("temp");
var LiveSyncProvider = (function () {
    function LiveSyncProvider($androidLiveSyncServiceLocator, $androidPlatformLiveSyncServiceLocator, $iosLiveSyncServiceLocator, $iosPlatformLiveSyncServiceLocator, $platformService, $platformsData, $logger, $childProcess, $options, $devicePlatformsConstants) {
        this.$androidLiveSyncServiceLocator = $androidLiveSyncServiceLocator;
        this.$androidPlatformLiveSyncServiceLocator = $androidPlatformLiveSyncServiceLocator;
        this.$iosLiveSyncServiceLocator = $iosLiveSyncServiceLocator;
        this.$iosPlatformLiveSyncServiceLocator = $iosPlatformLiveSyncServiceLocator;
        this.$platformService = $platformService;
        this.$platformsData = $platformsData;
        this.$logger = $logger;
        this.$childProcess = $childProcess;
        this.$options = $options;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.deviceSpecificLiveSyncServicesCache = {};
        this.platformSpecificLiveSyncServicesCache = {};
    }
    Object.defineProperty(LiveSyncProvider.prototype, "deviceSpecificLiveSyncServices", {
        get: function () {
            var _this = this;
            return {
                android: function (_device, $injector) {
                    if (!_this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier]) {
                        _this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier] = $injector.resolve(_this.$androidLiveSyncServiceLocator.factory, { _device: _device });
                    }
                    return _this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier];
                },
                ios: function (_device, $injector) {
                    if (!_this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier]) {
                        _this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier] = $injector.resolve(_this.$iosLiveSyncServiceLocator.factory, { _device: _device });
                    }
                    return _this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier];
                }
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LiveSyncProvider.prototype, "platformSpecificLiveSyncServices", {
        get: function () {
            var _this = this;
            return {
                android: function (_liveSyncData, $injector) {
                    if (!_this.platformSpecificLiveSyncServicesCache[_this.$devicePlatformsConstants.Android]) {
                        _this.platformSpecificLiveSyncServicesCache[_this.$devicePlatformsConstants.Android] = $injector.resolve(_this.$androidPlatformLiveSyncServiceLocator.factory, { _liveSyncData: _liveSyncData });
                    }
                    return _this.platformSpecificLiveSyncServicesCache[_this.$devicePlatformsConstants.Android];
                },
                ios: function (_liveSyncData, $injector) {
                    if (!_this.platformSpecificLiveSyncServicesCache[_this.$devicePlatformsConstants.iOS]) {
                        _this.platformSpecificLiveSyncServicesCache[_this.$devicePlatformsConstants.iOS] = $injector.resolve(_this.$iosPlatformLiveSyncServiceLocator.factory, { _liveSyncData: _liveSyncData });
                    }
                    return _this.platformSpecificLiveSyncServicesCache[_this.$devicePlatformsConstants.iOS];
                }
            };
        },
        enumerable: true,
        configurable: true
    });
    LiveSyncProvider.prototype.buildForDevice = function (device) {
        var _this = this;
        return (function () {
            _this.$platformService.buildForDeploy(device.deviceInfo.platform, { buildForDevice: !device.isEmulator }).wait();
            var platformData = _this.$platformsData.getPlatformData(device.deviceInfo.platform);
            if (device.isEmulator) {
                return _this.$platformService.getLatestApplicationPackageForEmulator(platformData).wait().packageName;
            }
            return _this.$platformService.getLatestApplicationPackageForDevice(platformData).wait().packageName;
        }).future()();
    };
    LiveSyncProvider.prototype.preparePlatformForSync = function (platform) {
        var _this = this;
        return (function () {
            if (!_this.$platformService.preparePlatform(platform).wait()) {
                _this.$logger.out("Verify that listed files are well-formed and try again the operation.");
            }
        }).future()();
    };
    LiveSyncProvider.prototype.canExecuteFastSync = function (filePath, platform) {
        var platformData = this.$platformsData.getPlatformData(platform);
        var fastSyncFileExtensions = LiveSyncProvider.FAST_SYNC_FILE_EXTENSIONS.concat(platformData.fastLivesyncFileExtensions);
        return _.includes(fastSyncFileExtensions, path.extname(filePath));
    };
    LiveSyncProvider.prototype.transferFiles = function (deviceAppData, localToDevicePaths, projectFilesPath, isFullSync) {
        var _this = this;
        return (function () {
            if (deviceAppData.platform.toLowerCase() === "android" || !deviceAppData.deviceSyncZipPath || !isFullSync) {
                deviceAppData.device.fileSystem.transferFiles(deviceAppData, localToDevicePaths).wait();
            }
            else {
                temp.track();
                var tempZip_1 = temp.path({ prefix: "sync", suffix: ".zip" });
                _this.$logger.trace("Creating zip file: " + tempZip_1);
                if (_this.$options.syncAllFiles) {
                    _this.$childProcess.spawnFromEvent("zip", ["-r", "-0", tempZip_1, "app"], "close", { cwd: path.dirname(projectFilesPath) }).wait();
                }
                else {
                    _this.$childProcess.spawnFromEvent("zip", ["-r", "-0", tempZip_1, "app", "-x", "app/tns_modules/*"], "close", { cwd: path.dirname(projectFilesPath) }).wait();
                }
                deviceAppData.device.fileSystem.transferFiles(deviceAppData, [{
                        getLocalPath: function () { return tempZip_1; },
                        getDevicePath: function () { return deviceAppData.deviceSyncZipPath; },
                        getRelativeToProjectBasePath: function () { return "../sync.zip"; },
                        deviceProjectRootPath: deviceAppData.deviceProjectRootPath
                    }]).wait();
            }
        }).future()();
    };
    LiveSyncProvider.FAST_SYNC_FILE_EXTENSIONS = [".css", ".xml", ".html"];
    return LiveSyncProvider;
}());
exports.LiveSyncProvider = LiveSyncProvider;
$injector.register("liveSyncProvider", LiveSyncProvider);
