import { resolve } from "node:path";
import fs from "fs-extra";
import { switcher } from "js-runtime";

//#region src/fs/write.ts
const writer = switcher({
	node: async (path$1, data, { sanity }) => {
		try {
			if ((await fs.readFile(resolve(path$1), { encoding: "utf-8" }))?.toString() === data?.toString()) return;
		} catch (_err) {}
		await fs.outputFile(resolve(path$1), data, { encoding: "utf-8" });
		if (sanity) {
			const savedData = await fs.readFile(resolve(path$1), { encoding: "utf-8" });
			if (savedData?.toString() !== data?.toString()) throw new Error(`Sanity check failed for ${path$1}\n\nData[${data.length}]:\n${data}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
			return savedData;
		}
		return data;
	},
	bun: async (path$1, data, { sanity }) => {
		try {
			await Bun.write(resolve(path$1), data);
			if (sanity) {
				const savedData = await Bun.file(resolve(path$1)).text();
				if (savedData?.toString() !== data?.toString()) throw new Error(`Sanity check failed for ${path$1}\n\nData[${path$1.length}]:\n${path$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
				return savedData;
			}
			return data;
		} catch (e) {
			console.error(e);
		}
	}
}, "node");
async function write(path$1, data, options = {}) {
	if (data.trim() === "") return;
	return writer(path$1, data.trim(), options);
}

//#endregion
export { write };
//# sourceMappingURL=write-O9QWtBJ_.js.map