"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AndroidDeviceLiveSyncServiceBase = void 0;
const device_livesync_service_base_1 = require("./device-livesync-service-base");
const _ = require("lodash");
class AndroidDeviceLiveSyncServiceBase extends device_livesync_service_base_1.DeviceLiveSyncServiceBase {
    constructor($injector, $platformsDataService, $filesHashService, $logger, device) {
        super($platformsDataService, device);
        this.$injector = $injector;
        this.$platformsDataService = $platformsDataService;
        this.$filesHashService = $filesHashService;
        this.$logger = $logger;
        this.device = device;
    }
    async transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, projectData, liveSyncDeviceDescriptor, options) {
        const deviceHashService = this.device.fileSystem.getDeviceHashService(deviceAppData.appIdentifier);
        const currentHashes = await deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths);
        const transferredFiles = await this.transferFilesCore(deviceAppData, localToDevicePaths, projectFilesPath, currentHashes, options);
        await this.device.fileSystem.updateHashesOnDevice(currentHashes, deviceAppData.appIdentifier);
        return transferredFiles;
    }
    async transferFilesCore(deviceAppData, localToDevicePaths, projectFilesPath, currentHashes, options) {
        if (options.force && options.isFullSync) {
            const hashFileDevicePath = this.device.fileSystem.getDeviceHashService(deviceAppData.appIdentifier).hashFileDevicePath;
            await this.device.fileSystem.deleteFile(hashFileDevicePath, deviceAppData.appIdentifier);
            this.$logger.trace("Before transfer directory on device ", localToDevicePaths);
            await this.transferDirectoryOnDevice(deviceAppData, localToDevicePaths, projectFilesPath);
            return localToDevicePaths;
        }
        const localToDevicePathsToTransfer = await this.getLocalToDevicePathsToTransfer(deviceAppData, localToDevicePaths, currentHashes, options);
        this.$logger.trace("Files to transfer: ", localToDevicePathsToTransfer);
        await this.transferFilesOnDevice(deviceAppData, localToDevicePathsToTransfer);
        return localToDevicePathsToTransfer;
    }
    async getLocalToDevicePathsToTransfer(deviceAppData, localToDevicePaths, currentHashes, options) {
        if (options.force || !options.isFullSync) {
            return localToDevicePaths;
        }
        const changedLocalToDevicePaths = await this.getChangedLocalToDevicePaths(deviceAppData.appIdentifier, localToDevicePaths, currentHashes);
        return changedLocalToDevicePaths;
    }
    async getChangedLocalToDevicePaths(appIdentifier, localToDevicePaths, currentHashes) {
        const deviceHashService = this.device.fileSystem.getDeviceHashService(appIdentifier);
        const oldHashes = (await deviceHashService.getShasumsFromDevice()) || {};
        const changedHashes = deviceHashService.getChangedShasums(oldHashes, currentHashes);
        const changedFiles = _.keys(changedHashes);
        const changedLocalToDevicePaths = localToDevicePaths.filter((localToDevicePathData) => changedFiles.indexOf(localToDevicePathData.getLocalPath()) >= 0);
        return changedLocalToDevicePaths;
    }
}
exports.AndroidDeviceLiveSyncServiceBase = AndroidDeviceLiveSyncServiceBase;
