import { hashDirectoryContents, hashFileContents, } from "@superblocksteam/shared";
import { unreachable } from "@superblocksteam/util";
import { listLocalDirectory } from "./list-dir.js";
/**
 * Computes the hash of a local directory, including the hashes of all its contents recursively.
 *
 * @param localDirectoryPath - The path to the local directory to hash.
 * @param excludedDirectories - An array of directory names to be ignored.
 * @returns A promise that resolves to the hash of the directory and its contents.
 */
export async function hashLocalDirectory(localDirectoryPath) {
    const directoryContents = [];
    const localDirListing = listLocalDirectory(localDirectoryPath);
    for await (const localDirEntry of localDirListing) {
        let entry;
        switch (localDirEntry.type) {
            case "-": {
                const hash = await hashFileContents(localDirEntry.contents);
                entry = {
                    type: "-",
                    executable: localDirEntry.executable,
                    name: localDirEntry.name,
                    hash,
                };
                break;
            }
            case "d": {
                const { hash, contents: subdirectoryContents } = await hashLocalDirectory(localDirEntry.localPath);
                entry = {
                    type: "d",
                    name: localDirEntry.name,
                    hash,
                    contents: subdirectoryContents,
                };
                break;
            }
            case "l": {
                entry = {
                    type: "l",
                    target: localDirEntry.target,
                    name: localDirEntry.name,
                };
                break;
            }
            default:
                unreachable(localDirEntry);
        }
        directoryContents.push(entry);
    }
    const hash = await hashDirectoryContents(directoryContents);
    return { contents: directoryContents, hash };
}
//# sourceMappingURL=hash-dir-tree.js.map