"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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 });
const path = require("path");
const temp = require("temp");
const ios_device_livesync_service_1 = require("./ios-device-livesync-service");
const platform_livesync_service_base_1 = require("./platform-livesync-service-base");
const constants_1 = require("../../constants");
const decorators_1 = require("../../common/decorators");
class IOSLiveSyncService extends platform_livesync_service_base_1.PlatformLiveSyncServiceBase {
    constructor($fs, $platformsDataService, $projectFilesManager, $injector, $devicePathProvider, $logger) {
        super($fs, $logger, $platformsDataService, $projectFilesManager, $devicePathProvider);
        this.$fs = $fs;
        this.$platformsDataService = $platformsDataService;
        this.$projectFilesManager = $projectFilesManager;
        this.$injector = $injector;
    }
    fullSync(syncInfo) {
        const _super = Object.create(null, {
            fullSync: { get: () => super.fullSync }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const device = syncInfo.device;
            if (device.isEmulator) {
                return _super.fullSync.call(this, syncInfo);
            }
            const projectData = syncInfo.projectData;
            const platformData = this.$platformsDataService.getPlatformData(device.deviceInfo.platform, projectData);
            const deviceAppData = yield this.getAppData(syncInfo);
            const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, constants_1.APP_FOLDER_NAME);
            temp.track();
            const tempZip = temp.path({ prefix: "sync", suffix: ".zip" });
            const tempApp = temp.mkdirSync("app");
            this.$logger.trace("Creating zip file: " + tempZip);
            this.$fs.copyFile(path.join(path.dirname(projectFilesPath), `${constants_1.APP_FOLDER_NAME}/*`), tempApp);
            this.$fs.deleteDirectory(path.join(tempApp, constants_1.TNS_MODULES_FOLDER_NAME));
            yield this.$fs.zipFiles(tempZip, this.$fs.enumerateFilesInDirectorySync(tempApp), (res) => {
                return path.join(constants_1.APP_FOLDER_NAME, path.relative(tempApp, res));
            });
            yield device.fileSystem.transferFiles(deviceAppData, [{
                    getLocalPath: () => tempZip,
                    getDevicePath: () => deviceAppData.deviceSyncZipPath,
                    getRelativeToProjectBasePath: () => "../sync.zip",
                    deviceProjectRootPath: yield deviceAppData.getDeviceProjectRootPath()
                }]);
            return {
                deviceAppData,
                isFullSync: true,
                modifiedFilesData: [],
                useHotModuleReload: syncInfo.useHotModuleReload
            };
        });
    }
    liveSyncWatchAction(device, liveSyncInfo) {
        if (liveSyncInfo.isReinstalled) {
            return this.fullSync({
                projectData: liveSyncInfo.projectData,
                device,
                liveSyncDeviceData: liveSyncInfo.liveSyncDeviceData,
                watch: true,
                useHotModuleReload: liveSyncInfo.useHotModuleReload
            });
        }
        else {
            return super.liveSyncWatchAction(device, liveSyncInfo);
        }
    }
    _getDeviceLiveSyncService(device, data) {
        const service = this.$injector.resolve(ios_device_livesync_service_1.IOSDeviceLiveSyncService, { device, data });
        return service;
    }
}
__decorate([
    decorators_1.performanceLog()
], IOSLiveSyncService.prototype, "fullSync", null);
__decorate([
    decorators_1.performanceLog()
], IOSLiveSyncService.prototype, "liveSyncWatchAction", null);
exports.IOSLiveSyncService = IOSLiveSyncService;
$injector.register("iOSLiveSyncService", IOSLiveSyncService);
