"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const StorageUtilities_1 = require("../storage/StorageUtilities");
const Log_1 = require("./Log");
const md5 = require("js-md5");
const JSONC = require("jsonc-parser");
class HashUtilities {
    /**
     * Generates hash catalog entries for a file (both complete file hash and property hashes for JSON files)
     * @param file The file to generate hashes for
     * @param filePath The file path to store in the catalog entry
     * @param hashCatalog The hash catalog to populate
     * @param onError Optional error callback for handling errors
     */
    static async generateHashesForFile(file, filePath, hashCatalog) {
        try {
            // Complete file hash with filename included
            await file.loadContent();
            if (file.content !== null && file.content !== undefined) {
                const fileHash = await file.getHash();
                if (fileHash) {
                    hashCatalog[fileHash] = {
                        fileName: file.name,
                        propertyName: "",
                        filePath: filePath,
                    };
                }
            }
            // Property hashes for JSON files
            if (file.name.endsWith(".json")) {
                await file.loadContent();
                if (file.isString && typeof file.content === "string") {
                    try {
                        // Check for BOM and handle it properly
                        const bytes = StorageUtilities_1.default.getContentsAsBinary(file);
                        const hasBOM = StorageUtilities_1.default.hasUTF8ByteOrderMark(bytes);
                        let cleanContent = file.content;
                        if (hasBOM && bytes) {
                            // Convert back to string without BOM
                            const withoutBOM = bytes.slice(3); // Remove the 3-byte UTF-8 BOM
                            cleanContent = new TextDecoder("utf-8").decode(withoutBOM);
                        }
                        // Parse as JSONC (handles both JSON and JSON with comments)
                        const jsonContent = JSONC.parse(cleanContent);
                        // Process each top-level property
                        for (const key in jsonContent) {
                            const value = jsonContent[key];
                            if (value !== null && !key.includes(":") && typeof value === "object") {
                                const stringifiedObj = JSON.stringify(value);
                                const stringifiedObjAndKey = stringifiedObj + key;
                                const propertyHash = md5.md5(stringifiedObjAndKey).toLowerCase();
                                hashCatalog[propertyHash] = {
                                    fileName: file.name,
                                    propertyName: key,
                                    filePath: filePath,
                                };
                            }
                        }
                    }
                    catch (error) {
                        Log_1.default.verbose(`Skipping property hash generation for ${file.fullPath}: ${error}`);
                    }
                }
            }
        }
        catch (error) {
            Log_1.default.verbose(`Skipping property hash generation for ${file.fullPath}: ${error}`);
        }
    }
}
exports.default = HashUtilities;

//# sourceMappingURL=../maps/core/HashUtilities.js.map
