/**
 * ConfigManager - Unified configuration persistence with Zod validation
 *
 * Handles atomic read-modify-write operations for .specweave/config.json
 * with runtime schema validation using Zod.
 */
import * as fs from 'fs/promises';
import * as path from 'path';
import { SpecWeaveConfigSchema, ResearchConfigSchema } from './types.js';
export class ConfigManager {
    static async load(configPath = ConfigManager.CONFIG_PATH) {
        try {
            const content = await fs.readFile(configPath, 'utf-8');
            const parsed = JSON.parse(content);
            return SpecWeaveConfigSchema.parse(parsed);
        }
        catch (error) {
            if (error.code === 'ENOENT') {
                return {
                    version: '1.0.0',
                    project: { name: 'specweave-project', type: 'multi' },
                    research: {},
                    livingDocs: { enabled: true, baseDir: '.specweave/docs' }
                };
            }
            throw error;
        }
    }
    static async save(config, configPath = ConfigManager.CONFIG_PATH) {
        const validated = SpecWeaveConfigSchema.parse(config);
        const dir = path.dirname(configPath);
        await fs.mkdir(dir, { recursive: true });
        await fs.writeFile(configPath, JSON.stringify(validated, null, 2), 'utf-8');
    }
    static async updateResearch(updates, configPath = ConfigManager.CONFIG_PATH) {
        const config = await ConfigManager.load(configPath);
        config.research = { ...config.research, ...updates };
        if (Object.keys(config.research).length > 0) {
            ResearchConfigSchema.parse(config.research);
        }
        await ConfigManager.save(config, configPath);
    }
}
ConfigManager.CONFIG_PATH = '.specweave/config.json';
//# sourceMappingURL=ConfigManager.js.map