import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { tokenUsageRecordSchema } from '../../../../../modules/token-accounting/entities/tokenUsage/tokenUsage.schema.js';
const USAGE_FILE = '.claude/reviews/usage.jsonl';
export class FilesystemTokenUsageGateway {
    async record(record) {
        const usageDir = join(record.localPath, '.claude', 'reviews');
        if (!existsSync(usageDir)) {
            mkdirSync(usageDir, { recursive: true });
        }
        const filePath = join(record.localPath, USAGE_FILE);
        appendFileSync(filePath, JSON.stringify(record) + '\n');
    }
    async loadAll(localPath) {
        const filePath = join(localPath, USAGE_FILE);
        if (!existsSync(filePath)) {
            return [];
        }
        const content = readFileSync(filePath, 'utf-8');
        const lines = content.split('\n').filter(line => line.trim() !== '');
        const records = [];
        for (const line of lines) {
            try {
                const parsed = JSON.parse(line);
                const result = tokenUsageRecordSchema.safeParse(parsed);
                if (result.success) {
                    records.push(result.data);
                }
            }
            catch {
                // skip invalid lines silently
            }
        }
        return records;
    }
}
//# sourceMappingURL=tokenUsage.filesystem.gateway.js.map