import { t as writeFileAtomic } from "./atomic-write-8yjqqLtS.js";
import fs from "fs";
//#region src/fs/json-store.ts
/**
* Small shared JSON persistence helpers. Many modules independently reimplemented
* the same "read a JSON file, fall back to a default on missing/parse-error" and
* "write a `{ key: items }` array store" logic — this centralizes both so the
* behavior (and the silent-fallback semantics) is defined in exactly one place.
*
* Writes go through writeFileAtomic, so a crash mid-write can never leave a
* half-written (corrupt) JSON file — readers always see either the old or the
* new complete document. This matters for the config, audit, and state files
* the whole product depends on.
*/
/** Read and parse a JSON file, returning `fallback` if it is missing or invalid. */
function readJsonFile(filePath, fallback) {
	if (!fs.existsSync(filePath)) return fallback;
	try {
		return JSON.parse(fs.readFileSync(filePath, "utf-8"));
	} catch {
		return fallback;
	}
}
/** Atomically write `value` as pretty-printed JSON, creating parent dirs as needed. */
function writeJsonFile(filePath, value) {
	writeFileAtomic(filePath, JSON.stringify(value, null, 2));
}
/**
* Read an array stored under `key` in a `{ [key]: T[] }` JSON document. Returns
* an empty array if the file is missing, unparsable, or the key is not an array.
*/
function readJsonArray(filePath, key) {
	const arr = readJsonFile(filePath, {})[key];
	return Array.isArray(arr) ? arr : [];
}
/** Write an array under `key` as a `{ [key]: items }` JSON document. */
function writeJsonArray(filePath, key, items) {
	writeJsonFile(filePath, { [key]: items });
}
//#endregion
export { writeJsonFile as i, readJsonFile as n, writeJsonArray as r, readJsonArray as t };

//# sourceMappingURL=json-store-WWsFzXub.js.map