"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;
};
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;
    }
    async fullSync(syncInfo) {
        const device = syncInfo.device;
        if (device.isEmulator) {
            return super.fullSync(syncInfo);
        }
        const projectData = syncInfo.projectData;
        const platformData = this.$platformsDataService.getPlatformData(device.deviceInfo.platform, projectData);
        const deviceAppData = await this.getAppData(syncInfo);
        const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, constants_1.APP_FOLDER_NAME);
        const tempZip = await this.$tempService.path({
            prefix: "sync",
            suffix: ".zip",
        });
        this.$logger.trace("Creating zip file: " + tempZip);
        const filesToTransfer = this.$fs.enumerateFilesInDirectorySync(projectFilesPath);
        await this.$fs.zipFiles(tempZip, filesToTransfer, (res) => {
            return path.join(constants_1.APP_FOLDER_NAME, path.relative(projectFilesPath, res));
        });
        await device.fileSystem.transferFiles(deviceAppData, [
            {
                getLocalPath: () => tempZip,
                getDevicePath: () => deviceAppData.deviceSyncZipPath,
                getRelativeToProjectBasePath: () => "../sync.zip",
                deviceProjectRootPath: await deviceAppData.getDeviceProjectRootPath(),
            },
        ]);
        await deviceAppData.device.applicationManager.setTransferredAppFiles(filesToTransfer);
        return {
            deviceAppData,
            isFullSync: true,
            modifiedFilesData: [],
            useHotModuleReload: syncInfo.useHotModuleReload,
        };
    }
    async syncAfterInstall(device, liveSyncInfo) {
        if (!device.isEmulator) {
            await 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;
    }
}
exports.IOSLiveSyncService = IOSLiveSyncService;
__decorate([
    (0, decorators_1.performanceLog)()
], IOSLiveSyncService.prototype, "fullSync", null);
yok_1.injector.register("iOSLiveSyncService", IOSLiveSyncService);
