"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hashFileContents = hashFileContents;
exports.hashDirectoryContents = hashDirectoryContents;
const hashing_js_1 = require("../signing/hashing.js");
const index_js_1 = require("../signing/index.js");
const unreachable_js_1 = __importDefault(require("../utils/unreachable.js"));
async function hashFileContents(contents) {
    return (0, hashing_js_1.sha256Base64)(contents);
}
function compareFilenames(str1, str2) {
    // JS does string comparison in UTF-16 code units, which might be hard to replicate in other systems.
    // A better approach is to sort by unicode code points. One way to do this is to convert the strings
    // to their UTF-8 representation and compare the bytes. This is equivalent to comparing the unicode
    // code points as it is one of the properties of UTF-8.
    // Note: this code uses node.js specific APIs
    return Buffer.from(str1).compare(Buffer.from(str2));
}
function normalizeEntryForHashing(entry) {
    switch (entry.type) {
        case '-':
            return { type: '-', name: entry.name, executable: entry.executable, hash: entry.hash };
        case 'd':
            return { type: 'd', name: entry.name, hash: entry.hash };
        case 'l':
            return { type: 'l', name: entry.name, target: entry.target };
        default:
            (0, unreachable_js_1.default)(entry);
    }
}
async function hashDirectoryContents(contents) {
    const normalizedContents = contents.map(normalizeEntryForHashing).sort((a, b) => compareFilenames(a.name, b.name));
    return await (0, index_js_1.sha256Object)(normalizedContents);
}
//# sourceMappingURL=hashing.js.map