import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { parseProjectConfig } from '../../../../config/projectConfig.js';
function resolveConfigPath(projectPath) {
    return join(projectPath, '.claude', 'reviews', 'config.json');
}
function isPlainObject(value) {
    return value !== null && typeof value === 'object' && !Array.isArray(value);
}
export class ProjectConfigFileSystemGateway {
    read(projectPath) {
        const configPath = resolveConfigPath(projectPath);
        if (!existsSync(configPath)) {
            return { status: 'not-found' };
        }
        let parsed;
        try {
            parsed = JSON.parse(readFileSync(configPath, 'utf-8'));
        }
        catch {
            return { status: 'malformed' };
        }
        if (!isPlainObject(parsed)) {
            return { status: 'malformed' };
        }
        try {
            const config = parseProjectConfig(parsed);
            return { status: 'ok', config };
        }
        catch {
            return { status: 'malformed' };
        }
    }
    write(projectPath, config) {
        const configPath = resolveConfigPath(projectPath);
        const tempPath = configPath + '.tmp';
        try {
            writeFileSync(tempPath, JSON.stringify(config, null, 2), 'utf-8');
            renameSync(tempPath, configPath);
            return { ok: true };
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            return { ok: false, reason: message };
        }
    }
}
//# sourceMappingURL=projectConfig.fileSystem.gateway.js.map