import { init_write_file_atomic, writeTextAtomic } from "../../../infra/write-file-atomic.js";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { homedir } from "node:os";
//#region src/gateway/hono/lib/extension-store.ts
init_write_file_atomic();
/** Extension UI: write-through JSON KV per namespace under ~/.xopc/extensions/{namespace}/storage.json */
const extensionStoreCache = /* @__PURE__ */ new Map();
function getExtensionStorePath(namespace) {
	const safeNamespace = namespace.replace(/[^a-zA-Z0-9_-]/g, "_");
	return join(homedir(), ".xopc", "extensions", safeNamespace, "storage.json");
}
async function loadExtensionStore(namespace) {
	const cached = extensionStoreCache.get(namespace);
	if (cached) return cached;
	const filePath = getExtensionStorePath(namespace);
	try {
		const raw = await readFile(filePath, "utf-8");
		const data = JSON.parse(raw);
		extensionStoreCache.set(namespace, data);
		return data;
	} catch {
		const empty = {};
		extensionStoreCache.set(namespace, empty);
		return empty;
	}
}
async function saveExtensionStore(namespace, data) {
	await writeTextAtomic(getExtensionStorePath(namespace), JSON.stringify(data, null, 2));
	extensionStoreCache.set(namespace, data);
}
//#endregion
export { loadExtensionStore, saveExtensionStore };

//# sourceMappingURL=extension-store.js.map