import fs from "node:fs";
import { getGlobalContextFilePath } from "./paths";
/**
 * A way to persist global state
 */
export class GlobalContext {
    update(key, value) {
        if (!fs.existsSync(getGlobalContextFilePath())) {
            fs.writeFileSync(getGlobalContextFilePath(), JSON.stringify({
                [key]: value,
            }, null, 2));
        }
        else {
            const data = fs.readFileSync(getGlobalContextFilePath(), "utf-8");
            const parsed = JSON.parse(data);
            parsed[key] = value;
            fs.writeFileSync(getGlobalContextFilePath(), JSON.stringify(parsed, null, 2));
        }
    }
    get(key) {
        if (!fs.existsSync(getGlobalContextFilePath())) {
            return undefined;
        }
        const data = fs.readFileSync(getGlobalContextFilePath(), "utf-8");
        const parsed = JSON.parse(data);
        return parsed[key];
    }
}
