"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) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IOSLiveSyncService = void 0;
const path = require("path");
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");
const yok_1 = require("../../common/yok");
class IOSLiveSyncService extends platform_livesync_service_base_1.PlatformLiveSyncServiceBase {
    constructor($fs, $platformsDataService, $projectFilesManager, $injector, $tempService, $devicePathProvider, $logger, $options) {
        super($fs, $logger, $platformsDataService, $projectFilesManager, $devicePathProvider, $options);
        this.$fs = $fs;
        this.$platformsDataService = $platformsDataService;
        this.$projectFilesManager = $projectFilesManager;
        this.$injector = $injector;
        this.$tempService = $tempService;
    }
    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);
            const tempZip = yield this.$tempService.path({
                prefix: "sync",
                suffix: ".zip",
            });
            this.$logger.trace("Creating zip file: " + tempZip);
            const filesToTransfer = this.$fs.enumerateFilesInDirectorySync(projectFilesPath);
            yield this.$fs.zipFiles(tempZip, filesToTransfer, (res) => {
                return path.join(constants_1.APP_FOLDER_NAME, path.relative(projectFilesPath, res));
            });
            yield device.fileSystem.transferFiles(deviceAppData, [
                {
                    getLocalPath: () => tempZip,
                    getDevicePath: () => deviceAppData.deviceSyncZipPath,
                    getRelativeToProjectBasePath: () => "../sync.zip",
                    deviceProjectRootPath: yield deviceAppData.getDeviceProjectRootPath(),
                },
            ]);
            yield deviceAppData.device.applicationManager.setTransferredAppFiles(filesToTransfer);
            return {
                deviceAppData,
                isFullSync: true,
                modifiedFilesData: [],
                useHotModuleReload: syncInfo.useHotModuleReload,
            };
        });
    }
    syncAfterInstall(device, liveSyncInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!device.isEmulator) {
                // In this case we should execute fullsync because iOS Runtime requires the full content of app dir to be extracted in the root of sync dir.
                yield this.fullSync({
                    projectData: liveSyncInfo.projectData,
                    device,
                    liveSyncDeviceData: liveSyncInfo.liveSyncDeviceData,
                    watch: true,
                    useHotModuleReload: liveSyncInfo.useHotModuleReload,
                });
            }
        });
    }
    _getDeviceLiveSyncService(device, data) {
        const service = this.$injector.resolve(ios_device_livesync_service_1.IOSDeviceLiveSyncService, { device, data });
        return service;
    }
}
__decorate([
    (0, decorators_1.performanceLog)()
], IOSLiveSyncService.prototype, "fullSync", null);
exports.IOSLiveSyncService = IOSLiveSyncService;
yok_1.injector.register("iOSLiveSyncService", IOSLiveSyncService);
//# sourceMappingURL=ios-livesync-service.js.map