"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.FilesHashService = void 0;
const helpers_1 = require("../common/helpers");
const constants_1 = require("../common/constants");
const constants_2 = require("../constants");
const path = require("path");
const _ = require("lodash");
const yok_1 = require("../common/yok");
class FilesHashService {
    constructor($fs, $logger) {
        this.$fs = $fs;
        this.$logger = $logger;
    }
    generateHashes(files) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = {};
            const action = (file) => __awaiter(this, void 0, void 0, function* () {
                try {
                    const isFile = this.$fs.getFsStats(file).isFile();
                    if (isFile) {
                        result[file] = yield this.$fs.getFileShasum(file);
                    }
                }
                catch (err) {
                    this.$logger.trace(`Unable to generate hash for file ${file}. Error is: ${err}`);
                }
            });
            yield (0, helpers_1.executeActionByChunks)(files, constants_1.DEFAULT_CHUNK_SIZE, action);
            return result;
        });
    }
    generateHashesForProject(platformData) {
        return __awaiter(this, void 0, void 0, function* () {
            const appFilesPath = path.join(platformData.appDestinationDirectoryPath, constants_2.APP_FOLDER_NAME);
            const files = this.$fs.enumerateFilesInDirectorySync(appFilesPath);
            const hashes = yield this.generateHashes(files);
            return hashes;
        });
    }
    saveHashesForProject(platformData, hashesFileDirectory) {
        return __awaiter(this, void 0, void 0, function* () {
            const hashes = yield this.generateHashesForProject(platformData);
            this.saveHashes(hashes, hashesFileDirectory);
            return hashes;
        });
    }
    getChanges(files, oldHashes) {
        return __awaiter(this, void 0, void 0, function* () {
            const newHashes = yield this.generateHashes(files);
            return this.getChangesInShasums(oldHashes, newHashes);
        });
    }
    hasChangesInShasums(oldHashes, newHashes) {
        return !!_.keys(this.getChangesInShasums(oldHashes, newHashes)).length;
    }
    saveHashes(hashes, hashesFileDirectory) {
        const hashesFilePath = path.join(hashesFileDirectory, constants_2.HASHES_FILE_NAME);
        this.$fs.writeJson(hashesFilePath, hashes);
    }
    getChangesInShasums(oldHashes, newHashes) {
        const addedFileHashes = _.omitBy(newHashes, (hash, pathToFile) => !!oldHashes[pathToFile] && oldHashes[pathToFile] === hash);
        const removedFileHashes = _.omitBy(oldHashes, (hash, pathToFile) => !!newHashes[pathToFile] && newHashes[pathToFile] === hash);
        const result = {};
        _.extend(result, addedFileHashes, removedFileHashes);
        return result;
    }
}
exports.FilesHashService = FilesHashService;
yok_1.injector.register("filesHashService", FilesHashService);
