import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { budgetConfigGuard } from '../../../../../modules/token-accounting/entities/budget/budgetConfig.guard.js';
import { getConfigDir } from '../../../../../shared/services/configDir.js';
const BUDGET_FILE_NAME = 'budget.json';
export class FilesystemBudgetGateway {
    getFilePath() {
        return join(getConfigDir(), BUDGET_FILE_NAME);
    }
    async load() {
        const filePath = this.getFilePath();
        if (!existsSync(filePath)) {
            return null;
        }
        let raw;
        try {
            raw = readFileSync(filePath, 'utf-8');
        }
        catch {
            return null;
        }
        let parsed;
        try {
            parsed = JSON.parse(raw);
        }
        catch {
            return null;
        }
        const result = budgetConfigGuard.safeParse(parsed);
        if (!result.success) {
            return null;
        }
        return result.data;
    }
    async save(config) {
        const dir = getConfigDir();
        if (!existsSync(dir)) {
            mkdirSync(dir, { recursive: true });
        }
        const filePath = join(dir, BUDGET_FILE_NAME);
        writeFileSync(filePath, `${JSON.stringify(config, null, 2)}\n`);
    }
}
//# sourceMappingURL=budget.filesystem.gateway.js.map