"use strict";
var iOSProxyServices = require("../../../mobile/ios/device/ios-proxy-services");
var path = require("path");
var shell = require("shelljs");
var osenv = require("osenv");
var constants_1 = require("../../../mobile/constants");
var IOSLiveSyncService = (function () {
    function IOSLiveSyncService(_device, $fs, $injector, $logger, $errors, $projectConstants) {
        this._device = _device;
        this.$fs = $fs;
        this.$injector = $injector;
        this.$logger = $logger;
        this.$errors = $errors;
        this.$projectConstants = $projectConstants;
    }
    Object.defineProperty(IOSLiveSyncService.prototype, "$project", {
        get: function () {
            return this.$injector.resolve("project");
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IOSLiveSyncService.prototype, "device", {
        get: function () {
            return this._device;
        },
        enumerable: true,
        configurable: true
    });
    IOSLiveSyncService.prototype.refreshApplication = function (deviceAppData, localToDevicePaths) {
        var _this = this;
        return (function () {
            if (_this.device.isEmulator) {
                var simulatorLogFilePath = path.join(osenv.home(), "/Library/Developer/CoreSimulator/Devices/" + _this.device.deviceInfo.identifier + "/data/Library/Logs/system.log");
                var simulatorLogFileContent = _this.$fs.readText(simulatorLogFilePath).wait() || "";
                var simulatorCachePath = path.join(osenv.home(), "/Library/Developer/CoreSimulator/Devices/" + _this.device.deviceInfo.identifier + "/data/Containers/Data/Application/");
                var regex = new RegExp("^(?:.*?)" + deviceAppData.appIdentifier + "(?:.*?)" + simulatorCachePath + "(.*?)$", "gm");
                var guid = "";
                while (true) {
                    var parsed = regex.exec(simulatorLogFileContent);
                    if (!parsed) {
                        break;
                    }
                    guid = parsed[1];
                }
                if (!guid) {
                    _this.$errors.failWithoutHelp("Unable to find application GUID for application " + deviceAppData.appIdentifier + ". Make sure application is installed on Simulator.");
                }
                var sourcePath = deviceAppData.deviceProjectRootPath;
                var destinationPath = path.join(simulatorCachePath, guid, constants_1.LiveSyncConstants.IOS_PROJECT_PATH);
                _this.$logger.trace("Transferring from " + sourcePath + " to " + destinationPath);
                shell.cp("-Rf", path.join(sourcePath, "*"), destinationPath);
                var cfBundleExecutable = "" + _this.$project.projectData.Framework + _this.$project.projectData.FrameworkVersion.split(".").join("");
                _this.device.applicationManager.restartApplication(deviceAppData.appIdentifier, cfBundleExecutable).wait();
            }
            else {
                _this.device.fileSystem.deleteFile("/Library/Preferences/ServerInfo.plist", deviceAppData.appIdentifier);
                var notificationProxyClient = _this.$injector.resolve(iOSProxyServices.NotificationProxyClient, { device: _this.device });
                var notification = _this.$project.projectData.Framework === _this.$projectConstants.TARGET_FRAMEWORK_IDENTIFIERS.NativeScript ? "com.telerik.app.refreshApp" : "com.telerik.app.refreshWebView";
                notificationProxyClient.postNotification(notification);
                notificationProxyClient.closeSocket();
            }
        }).future()();
    };
    IOSLiveSyncService.prototype.removeFiles = function (appIdentifier, localToDevicePaths) {
        var _this = this;
        return (function () {
            localToDevicePaths
                .map(function (localToDevicePath) { return localToDevicePath.getDevicePath(); })
                .forEach(function (deviceFilePath) { return _this.device.fileSystem.deleteFile(deviceFilePath, appIdentifier); });
        }).future()();
    };
    return IOSLiveSyncService;
})();
exports.IOSLiveSyncService = IOSLiveSyncService;
$injector.register("iosLiveSyncServiceLocator", { factory: IOSLiveSyncService });
