import * as fs from "fs";
import * as path from "path";
export class Registry {
    filePath;
    constructor(baseDir) {
        this.filePath = path.join(baseDir, "schedules.json");
    }
    getAll() {
        if (!fs.existsSync(this.filePath))
            return {};
        try {
            return JSON.parse(fs.readFileSync(this.filePath, "utf-8"));
        }
        catch {
            throw new Error(`Failed to parse schedule registry at ${this.filePath}. The file may be corrupted. Delete it to reset: rm "${this.filePath}"`);
        }
    }
    get(name) {
        return this.getAll()[name];
    }
    has(name) {
        return Object.hasOwn(this.getAll(), name);
    }
    set(entry) {
        const all = this.getAll();
        all[entry.name] = entry;
        this.write(all);
    }
    remove(name) {
        const all = this.getAll();
        delete all[name];
        this.write(all);
    }
    write(data) {
        fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
        fs.writeFileSync(this.filePath, JSON.stringify(data, null, 2), { mode: 0o600 });
    }
}
