"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const DifferenceSet_1 = require("./DifferenceSet");
const IFileDifference_1 = require("./IFileDifference");
const StorageUtilities_1 = require("./StorageUtilities");
class Thumbprint {
    constructor() {
        this.fileInfos = {};
    }
    compare(secondThumbprint, compareOnlyLeafs) {
        const tempFileInfos = {};
        for (const fileInfoPath in this.fileInfos) {
            let comparePath = fileInfoPath;
            if (compareOnlyLeafs) {
                comparePath = StorageUtilities_1.default.canonicalizeName(StorageUtilities_1.default.getLeafName(fileInfoPath));
            }
            tempFileInfos[comparePath] = this.fileInfos[fileInfoPath];
        }
        for (const fileInfoPath in secondThumbprint.fileInfos) {
            const compareFile = secondThumbprint.fileInfos[fileInfoPath];
            if (compareFile) {
                let comparePath = fileInfoPath;
                if (compareOnlyLeafs) {
                    comparePath = StorageUtilities_1.default.canonicalizeName(StorageUtilities_1.default.getLeafName(fileInfoPath));
                }
                const sourceFileInfo = tempFileInfos[comparePath];
                if (sourceFileInfo !== undefined) {
                    if (sourceFileInfo.size === compareFile.size) {
                        tempFileInfos[comparePath] = undefined;
                    }
                }
            }
        }
        const diffSet = new DifferenceSet_1.default();
        for (const fileInfoPath in tempFileInfos) {
            const fi = tempFileInfos[fileInfoPath];
            if (fi) {
                diffSet.fileDifferences.push({
                    type: IFileDifference_1.FileDifferenceType.contentsDifferent,
                    path: fileInfoPath,
                });
            }
        }
        return diffSet;
    }
    async create(folder) {
        const tempFileInfos = {};
        await this._createInternal(folder, tempFileInfos);
        this.fileInfos = tempFileInfos;
    }
    async _createInternal(folder, fileInfos) {
        await folder.load();
        for (const fileName in folder.files) {
            const file = folder.files[fileName];
            if (file) {
                fileInfos[file.storageRelativePath] = { size: file.content ? file.content.length : -1 };
            }
        }
        for (const folderName in folder.folders) {
            const childFolder = folder.folders[folderName];
            if (childFolder && !childFolder.errorStatus) {
                await this._createInternal(childFolder, fileInfos);
            }
        }
    }
}
exports.default = Thumbprint;

//# sourceMappingURL=../maps/storage/Thumbprint.js.map
