"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.AndroidDeviceHashService = void 0;
const path = require("path");
const _ = require("lodash");
const decorators_1 = require("../../decorators");
const helpers_1 = require("../../helpers");
const constants_1 = require("../../constants");
class AndroidDeviceHashService {
    constructor(adb, appIdentifier, $fs, $mobileHelper, $tempService) {
        this.adb = adb;
        this.appIdentifier = appIdentifier;
        this.$fs = $fs;
        this.$mobileHelper = $mobileHelper;
        this.$tempService = $tempService;
    }
    get hashFileDevicePath() {
        return this.$mobileHelper.buildDevicePath(constants_1.LiveSyncPaths.ANDROID_TMP_DIR_NAME, this.appIdentifier, AndroidDeviceHashService.HASH_FILE_NAME);
    }
    async doesShasumFileExistsOnDevice() {
        const lsResult = await this.adb.executeShellCommand([
            "ls",
            this.hashFileDevicePath,
        ]);
        return !!(lsResult && lsResult.trim() === this.hashFileDevicePath);
    }
    async getShasumsFromDevice() {
        const hashFileLocalPath = await this.downloadHashFileFromDevice();
        if (this.$fs.exists(hashFileLocalPath)) {
            return this.$fs.readJson(hashFileLocalPath);
        }
        return null;
    }
    async uploadHashFileToDevice(data) {
        const hashFileLocalPath = await this.getHashFileLocalPath();
        this.$fs.writeJson(hashFileLocalPath, data);
        await this.adb.pushFile(hashFileLocalPath, this.hashFileDevicePath);
    }
    async updateHashes(localToDevicePaths) {
        const oldShasums = (await this.getShasumsFromDevice()) || {};
        await this.generateHashesFromLocalToDevicePaths(localToDevicePaths, oldShasums);
        await this.uploadHashFileToDevice(oldShasums);
    }
    async generateHashesFromLocalToDevicePaths(localToDevicePaths, initialShasums = {}) {
        const action = async (localToDevicePathData) => {
            const localPath = localToDevicePathData.getLocalPath();
            if (this.$fs.getFsStats(localPath).isFile()) {
                initialShasums[localPath] = await this.$fs.getFileShasum(localPath);
            }
        };
        await (0, helpers_1.executeActionByChunks)(localToDevicePaths, constants_1.DEFAULT_CHUNK_SIZE, action);
        return initialShasums;
    }
    getDevicePaths(localToDevicePaths) {
        return _.map(localToDevicePaths, (localToDevicePathData) => {
            return `"${localToDevicePathData.getDevicePath()}"`;
        });
    }
    getChangedShasums(oldShasums, currentShasums) {
        if (!oldShasums) {
            return currentShasums;
        }
        return _.omitBy(currentShasums, (hash, pathToFile) => !!oldShasums[pathToFile] && oldShasums[pathToFile] === hash);
    }
    async removeHashes(localToDevicePaths) {
        const oldShasums = await this.getShasumsFromDevice();
        if (oldShasums) {
            const fileToShasumDictionary = _.omit(oldShasums, localToDevicePaths.map((ldp) => ldp.getLocalPath()));
            await this.uploadHashFileToDevice(fileToShasumDictionary);
            return true;
        }
        return false;
    }
    async getHashFileLocalPath() {
        return path.join(await this.getTempDir(), AndroidDeviceHashService.HASH_FILE_NAME);
    }
    getTempDir() {
        return this.$tempService.mkdirSync(`android-device-hash-service-${this.appIdentifier}`);
    }
    async downloadHashFileFromDevice() {
        const hashFileLocalPath = await this.getHashFileLocalPath();
        if (!this.$fs.exists(hashFileLocalPath)) {
            await this.adb.executeCommand([
                "pull",
                this.hashFileDevicePath,
                await this.getTempDir(),
            ]);
        }
        return hashFileLocalPath;
    }
}
exports.AndroidDeviceHashService = AndroidDeviceHashService;
AndroidDeviceHashService.HASH_FILE_NAME = "hashes";
__decorate([
    (0, decorators_1.cache)()
], AndroidDeviceHashService.prototype, "hashFileDevicePath", null);
__decorate([
    (0, decorators_1.cache)()
], AndroidDeviceHashService.prototype, "getHashFileLocalPath", null);
__decorate([
    (0, decorators_1.cache)()
], AndroidDeviceHashService.prototype, "getTempDir", null);
