import { __require as e } from "../../_virtual/_rolldown/runtime.mjs";
import { CustomJSONStringify as t, safeJsonStringify as n } from "../../functions/utils/serialize.utils.mjs";
import { BaseCacheDevice as r } from "./BaseCacheDevice.mjs";
import { logError as i } from "../../error-handling/logError.mjs";
//#region libs/shared/common/src/data-structures/cacheDevices/FileCacheDevice.ts
var a = class extends r {
	filePath;
	useFileCache;
	constructor(e, t) {
		super(), this.useFileCache = !!e, this.filePath = t, this.readFileFromDevice();
	}
	readFileFromDevice() {
		if (this.useFileCache && this.filePath) try {
			let t = e("fs");
			if (t.existsSync(this.filePath)) {
				let e = JSON.parse(t.readFileSync(this.filePath).toString());
				Object.entries(e).forEach(([e, t]) => {
					this.setInMemory(e, t);
				});
			}
		} catch (e) {
			i("FileCacheDevice Constructor Error", { err: e });
		}
	}
	get(e) {
		return this.getInMemory(e);
	}
	set(t, r) {
		if (this.readFileFromDevice(), this.setInMemory(t, r), this.useFileCache && this.filePath) try {
			e("fs").writeFileSync(this.filePath, n(Object.fromEntries(this.inMemoryCache)));
		} catch (e) {
			i("FileCacheDevice set function", { err: e });
		}
	}
	serialize() {
		return Object.fromEntries([...this.inMemoryCache].map(([e, n]) => [e, t(n)]));
	}
};
//#endregion
export { a as FileCacheDevice };
