"use strict";
const iOSProxyServices = require("../../../mobile/ios/device/ios-proxy-services");
const path = require("path");
const shell = require("shelljs");
let osenv = require("osenv");
const constants_1 = require("../../../constants");
const constants_2 = require("../../../constants");
class IOSLiveSyncService {
    constructor(_device, $fs, $injector, $logger, $errors) {
        this._device = _device;
        this.$fs = $fs;
        this.$injector = $injector;
        this.$logger = $logger;
        this.$errors = $errors;
    }
    get $project() {
        return this.$injector.resolve("project");
    }
    get device() {
        return this._device;
    }
    refreshApplication(deviceAppData, localToDevicePaths) {
        return (() => {
            if (this.device.isEmulator) {
                let simulatorLogFilePath = path.join(osenv.home(), `/Library/Developer/CoreSimulator/Devices/${this.device.deviceInfo.identifier}/data/Library/Logs/system.log`);
                let simulatorLogFileContent = this.$fs.readText(simulatorLogFilePath).wait() || "";
                let simulatorCachePath = path.join(osenv.home(), `/Library/Developer/CoreSimulator/Devices/${this.device.deviceInfo.identifier}/data/Containers/Data/Application/`);
                let regex = new RegExp(`^(?:.*?)${deviceAppData.appIdentifier}(?:.*?)${simulatorCachePath}(.*?)$`, "gm");
                let guid = "";
                while (true) {
                    let 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.`);
                }
                let sourcePath = deviceAppData.deviceProjectRootPath;
                let 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);
                let cfBundleExecutable = `${this.$project.projectData.Framework}${this.$project.projectData.FrameworkVersion.split(".").join("")}`;
                this.device.applicationManager.restartApplication(deviceAppData.appIdentifier, cfBundleExecutable).wait();
            }
            else {
                this.device.fileSystem.deleteFile("/Documents/AppBuilder/ServerInfo.plist", deviceAppData.appIdentifier);
                let notificationProxyClient = this.$injector.resolve(iOSProxyServices.NotificationProxyClient, { device: this.device });
                let notification = this.$project.projectData.Framework === constants_2.TARGET_FRAMEWORK_IDENTIFIERS.NativeScript ? "com.telerik.app.refreshApp" : "com.telerik.app.refreshWebView";
                notificationProxyClient.postNotification(notification);
                notificationProxyClient.closeSocket();
            }
        }).future()();
    }
    removeFiles(appIdentifier, localToDevicePaths) {
        return (() => {
            localToDevicePaths
                .map(localToDevicePath => localToDevicePath.getDevicePath())
                .forEach(deviceFilePath => this.device.fileSystem.deleteFile(deviceFilePath, appIdentifier));
        }).future()();
    }
}
exports.IOSLiveSyncService = IOSLiveSyncService;
$injector.register("iosLiveSyncServiceLocator", { factory: IOSLiveSyncService });
