"use strict";
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.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;
    }
    transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, projectData, liveSyncDeviceDescriptor, options) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceHashService = this.device.fileSystem.getDeviceHashService(deviceAppData.appIdentifier);
            const currentHashes = yield deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths);
            const transferredFiles = yield this.transferFilesCore(deviceAppData, localToDevicePaths, projectFilesPath, currentHashes, options);
            yield this.device.fileSystem.updateHashesOnDevice(currentHashes, deviceAppData.appIdentifier);
            return transferredFiles;
        });
    }
    transferFilesCore(deviceAppData, localToDevicePaths, projectFilesPath, currentHashes, options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (options.force && options.isFullSync) {
                const hashFileDevicePath = this.device.fileSystem.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, currentHashes, options);
            this.$logger.trace("Files to transfer: ", localToDevicePathsToTransfer);
            yield this.transferFilesOnDevice(deviceAppData, localToDevicePathsToTransfer);
            return localToDevicePathsToTransfer;
        });
    }
    getLocalToDevicePathsToTransfer(deviceAppData, localToDevicePaths, currentHashes, options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (options.force || !options.isFullSync) {
                return localToDevicePaths;
            }
            const changedLocalToDevicePaths = yield this.getChangedLocalToDevicePaths(deviceAppData.appIdentifier, localToDevicePaths, currentHashes);
            return changedLocalToDevicePaths;
        });
    }
    getChangedLocalToDevicePaths(appIdentifier, localToDevicePaths, currentHashes) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceHashService = this.device.fileSystem.getDeviceHashService(appIdentifier);
            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;
        });
    }
}
exports.AndroidDeviceLiveSyncServiceBase = AndroidDeviceLiveSyncServiceBase;
//# sourceMappingURL=android-device-livesync-service-base.js.map