import { init_write_file_atomic, writeTextAtomicSync } from "../../infra/write-file-atomic.js";
import { init_paths, resolveSkillsLockPath } from "../../config/paths.js";
import { existsSync, readFileSync } from "node:fs";
//#region src/agent/skills/hub-lock.ts
/**
* Skills hub lock (~/.xopc/skills-lock.json): records install source + tree hash per managed skill id.
*/
init_paths();
init_write_file_atomic();
function emptyLock() {
	return {
		version: 1,
		entries: {}
	};
}
function loadSkillsLock() {
	const p = resolveSkillsLockPath();
	if (!existsSync(p)) return emptyLock();
	try {
		const raw = readFileSync(p, "utf-8");
		const j = JSON.parse(raw);
		if (j.version !== 1 || !j.entries || typeof j.entries !== "object") return emptyLock();
		return {
			version: 1,
			entries: { ...j.entries }
		};
	} catch {
		return emptyLock();
	}
}
function saveSkillsLock(lock) {
	writeTextAtomicSync(resolveSkillsLockPath(), `${JSON.stringify(lock, null, 2)}\n`);
}
/** Record or replace hub metadata for a managed skill directory id (folder name under ~/.xopc/skills). */
function recordSkillsHubInstall(skillId, meta, contentHash) {
	const lock = loadSkillsLock();
	const now = (/* @__PURE__ */ new Date()).toISOString();
	const prev = lock.entries[skillId];
	lock.entries[skillId] = {
		kind: meta.kind,
		source: meta.source,
		ref: meta.ref,
		subpath: meta.subpath,
		contentHash,
		installedAt: prev?.installedAt ?? now,
		updatedAt: now
	};
	saveSkillsLock(lock);
}
function removeSkillsLockEntry(skillId) {
	const lock = loadSkillsLock();
	if (!lock.entries[skillId]) return;
	delete lock.entries[skillId];
	saveSkillsLock(lock);
}
function getSkillsLockEntry(skillId) {
	return loadSkillsLock().entries[skillId];
}
//#endregion
export { getSkillsLockEntry, loadSkillsLock, recordSkillsHubInstall, removeSkillsLockEntry, saveSkillsLock };

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