import fs from "node:fs";
import path from "node:path";
//#region src/channels/pairing/allow-from-file.ts
function readAllowFromFile(filePath) {
	try {
		if (!fs.existsSync(filePath)) return {
			version: 1,
			allowFrom: []
		};
		const raw = fs.readFileSync(filePath, "utf-8");
		const parsed = JSON.parse(raw);
		if (Array.isArray(parsed.allowFrom)) return {
			version: 1,
			allowFrom: parsed.allowFrom.filter((id) => typeof id === "string" && id.trim() !== "")
		};
	} catch {}
	return {
		version: 1,
		allowFrom: []
	};
}
function readAllowFromIdsSync(filePath) {
	return readAllowFromFile(filePath).allowFrom;
}
function appendAllowFromIdSync(filePath, userId) {
	const trimmed = userId.trim();
	if (!trimmed) return { changed: false };
	const dir = path.dirname(filePath);
	fs.mkdirSync(dir, { recursive: true });
	const content = readAllowFromFile(filePath);
	if (content.allowFrom.includes(trimmed)) return { changed: false };
	content.allowFrom.push(trimmed);
	fs.writeFileSync(filePath, JSON.stringify(content, null, 2), "utf-8");
	try {
		fs.chmodSync(filePath, 384);
	} catch {}
	return { changed: true };
}
function removeAllowFromIdSync(filePath, userId) {
	const trimmed = userId.trim();
	if (!trimmed) return { changed: false };
	const content = readAllowFromFile(filePath);
	const next = content.allowFrom.filter((id) => id !== trimmed);
	if (next.length === content.allowFrom.length) return { changed: false };
	const dir = path.dirname(filePath);
	fs.mkdirSync(dir, { recursive: true });
	fs.writeFileSync(filePath, JSON.stringify({
		version: 1,
		allowFrom: next
	}, null, 2), "utf-8");
	try {
		fs.chmodSync(filePath, 384);
	} catch {}
	return { changed: true };
}
//#endregion
export { appendAllowFromIdSync, readAllowFromIdsSync, removeAllowFromIdSync };

//# sourceMappingURL=allow-from-file.js.map