import { createHash } from "node:crypto";
import { createReadStream, existsSync, readFileSync, readdirSync, statSync } from "node:fs";
import { join, relative } from "node:path";
//#region src/agent/skills/hub-hash.ts
/**
* Deterministic content hash for a skill directory (for hub lock / drift detection).
*/
const SKIP_DIR = new Set([".git", "node_modules"]);
function collectRelativeFiles(root) {
	const out = [];
	function walk(dir) {
		let entries;
		try {
			entries = readdirSync(dir, { withFileTypes: true });
		} catch {
			return;
		}
		for (const e of entries) {
			const full = join(dir, e.name);
			if (e.isDirectory()) {
				if (SKIP_DIR.has(e.name)) continue;
				walk(full);
			} else if (e.isFile()) out.push(relative(root, full).split("\\").join("/"));
		}
	}
	walk(root);
	return out.sort();
}
/** Hash file contents without loading huge trees into one buffer (streaming per file). */
function sha256File(path) {
	return new Promise((resolve, reject) => {
		const hash = createHash("sha256");
		const stream = createReadStream(path);
		stream.on("data", (chunk) => hash.update(chunk));
		stream.on("error", reject);
		stream.on("end", () => resolve(hash.digest("hex")));
	});
}
/**
* Stable hash: sorted relative paths, each file hashed; final digest over `path\\0hex\\n` lines.
*/
async function computeSkillTreeHash(skillDir) {
	const root = skillDir;
	if (!existsSync(root) || !statSync(root).isDirectory()) return createHash("sha256").update("").digest("hex");
	const rels = collectRelativeFiles(root);
	const aggregate = createHash("sha256");
	for (const rel of rels) {
		const fh = await sha256File(join(root, rel));
		aggregate.update(`${rel}\0${fh}\n`);
	}
	return aggregate.digest("hex");
}
/** Sync variant for CLI paths that are already small trees. */
function computeSkillTreeHashSync(skillDir) {
	const root = skillDir;
	if (!existsSync(root) || !statSync(root).isDirectory()) return createHash("sha256").update("").digest("hex");
	const rels = collectRelativeFiles(root);
	const aggregate = createHash("sha256");
	for (const rel of rels) {
		const buf = readFileSync(join(root, rel));
		const fh = createHash("sha256").update(buf).digest("hex");
		aggregate.update(`${rel}\0${fh}\n`);
	}
	return aggregate.digest("hex");
}
//#endregion
export { computeSkillTreeHash, computeSkillTreeHashSync };

//# sourceMappingURL=hub-hash.js.map