import "./fs-safe-defaults-A4awSMom.js";
import { b as writeJsonSync$1, y as tryReadJsonSync$1 } from "./json-files-BiJ8jTLe.js";
import fs from "node:fs";
import path from "node:path";
//#region src/infra/json-file.ts
function tryReadJsonSync(pathname) {
	return tryReadJsonSync$1(pathname);
}
function writeJsonSync(...args) {
	return writeJsonSync$1(...args);
}
function resolveJsonSymlinkTarget(pathname) {
	let stat;
	try {
		stat = fs.lstatSync(pathname);
	} catch (error) {
		if (error.code === "ENOENT") return;
		throw error;
	}
	if (!stat.isSymbolicLink()) return;
	return path.resolve(path.dirname(pathname), fs.readlinkSync(pathname));
}
function resolveJsonSaveTarget(pathname) {
	const target = resolveJsonSymlinkTarget(pathname);
	if (!target) return pathname;
	fs.statSync(path.dirname(target));
	return target;
}
function saveJsonFile(pathname, data) {
	writeJsonSync(resolveJsonSaveTarget(pathname), data);
}
function repairJsonFilePermissions(pathname) {
	const target = resolveJsonSaveTarget(pathname);
	let fd;
	try {
		fd = fs.openSync(target, fs.constants.O_RDONLY | (process.platform !== "win32" && "O_NOFOLLOW" in fs.constants ? fs.constants.O_NOFOLLOW : 0));
		fs.fchmodSync(fd, 384);
	} catch {} finally {
		if (fd !== void 0) try {
			fs.closeSync(fd);
		} catch {}
	}
}
function loadJsonFile(pathname) {
	const direct = tryReadJsonSync(pathname);
	if (direct !== null) return direct;
	const target = resolveJsonSymlinkTarget(pathname);
	return target ? tryReadJsonSync(target) ?? void 0 : void 0;
}
//#endregion
export { repairJsonFilePermissions as n, saveJsonFile as r, loadJsonFile as t };
