"use strict";
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 device_livesync_service_base_1 = require("./device-livesync-service-base");
const android_device_hash_service_1 = require("../../common/mobile/android/android-device-hash-service");
class AndroidDeviceLiveSyncServiceBase extends device_livesync_service_base_1.DeviceLiveSyncServiceBase {
    constructor($injector, $platformsData, $filesHashService, $logger, device) {
        super($platformsData, device);
        this.$injector = $injector;
        this.$platformsData = $platformsData;
        this.$filesHashService = $filesHashService;
        this.$logger = $logger;
        this.device = device;
        this.deviceHashServices = {};
    }
    getDeviceHashService(appIdentifier) {
        const key = `${this.device.deviceInfo.identifier}${appIdentifier}`;
        if (!this.deviceHashServices[key]) {
            const deviceHashService = this.$injector.resolve(android_device_hash_service_1.AndroidDeviceHashService, { adb: this.device.adb, appIdentifier });
            this.deviceHashServices[key] = deviceHashService;
        }
        return this.deviceHashServices[key];
    }
    transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, projectData, liveSyncDeviceInfo, options) {
        return __awaiter(this, void 0, void 0, function* () {
            const transferredFiles = yield this.transferFilesCore(deviceAppData, localToDevicePaths, projectFilesPath, options);
            yield this.updateHashes(deviceAppData, localToDevicePaths, projectData, liveSyncDeviceInfo);
            return transferredFiles;
        });
    }
    transferFilesCore(deviceAppData, localToDevicePaths, projectFilesPath, options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (options.force && options.isFullSync) {
                const hashFileDevicePath = this.getDeviceHashService(deviceAppData.appIdentifier).hashFileDevicePath;
                yield this.device.fileSystem.deleteFile(hashFileDevicePath, deviceAppData.appIdentifier);
                this.$logger.trace("Before transfer directory on device ", localToDevicePaths);
                yield this.transferDirectoryOnDevice(deviceAppData, localToDevicePaths, projectFilesPath);
                return localToDevicePaths;
            }
            const localToDevicePathsToTransfer = yield this.getLocalToDevicePathsToTransfer(deviceAppData, localToDevicePaths, options);
            this.$logger.trace("Files to transfer: ", localToDevicePathsToTransfer);
            yield this.transferFilesOnDevice(deviceAppData, localToDevicePathsToTransfer);
            return localToDevicePathsToTransfer;
        });
    }
    getLocalToDevicePathsToTransfer(deviceAppData, localToDevicePaths, options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (options.force || !options.isFullSync) {
                return localToDevicePaths;
            }
            const changedLocalToDevicePaths = yield this.getChangedLocalToDevicePaths(deviceAppData.appIdentifier, localToDevicePaths);
            return changedLocalToDevicePaths;
        });
    }
    getChangedLocalToDevicePaths(appIdentifier, localToDevicePaths) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceHashService = this.getDeviceHashService(appIdentifier);
            const currentHashes = yield deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths);
            const oldHashes = (yield deviceHashService.getShasumsFromDevice()) || {};
            const changedHashes = deviceHashService.getChangedShasums(oldHashes, currentHashes);
            const changedFiles = _.keys(changedHashes);
            const changedLocalToDevicePaths = localToDevicePaths.filter(localToDevicePathData => changedFiles.indexOf(localToDevicePathData.getLocalPath()) >= 0);
            return changedLocalToDevicePaths;
        });
    }
    updateHashes(deviceAppData, localToDevicePaths, projectData, liveSyncDeviceInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const hashes = yield this.updateHashesOnDevice(deviceAppData, localToDevicePaths, projectData, liveSyncDeviceInfo);
            this.updateLocalHashes(hashes, deviceAppData, projectData, liveSyncDeviceInfo);
        });
    }
    updateHashesOnDevice(deviceAppData, localToDevicePaths, projectData, liveSyncDeviceInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceHashService = this.getDeviceHashService(deviceAppData.appIdentifier);
            const currentHashes = yield deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths);
            yield deviceHashService.uploadHashFileToDevice(currentHashes);
            return currentHashes;
        });
    }
    updateLocalHashes(hashes, deviceAppData, projectData, liveSyncDeviceInfo) {
        const hashFilePath = liveSyncDeviceInfo.outputPath || this.$platformsData.getPlatformData(deviceAppData.platform, projectData).deviceBuildOutputPath;
        this.$filesHashService.saveHashes(hashes, hashFilePath);
    }
}
exports.AndroidDeviceLiveSyncServiceBase = AndroidDeviceLiveSyncServiceBase;
