import { hasServerEntry, safeParseMcpSettings } from '../../../../modules/cli-configuration/entities/mcpSettings/mcpSettings.guard.js';
export class ConfigureMcpUseCase {
    deps;
    constructor(deps) {
        this.deps = deps;
    }
    execute() {
        if (!this.deps.isClaudeInstalled()) {
            return 'claude-not-found';
        }
        const mcpServerPath = this.deps.resolveMcpServerPath();
        let settings;
        try {
            const content = this.deps.readFileSync(this.deps.settingsPath, 'utf-8');
            settings = JSON.parse(content);
        }
        catch {
            settings = {};
        }
        const parseResult = safeParseMcpSettings(settings);
        const mcpServers = parseResult.success
            ? parseResult.data.mcpServers
            : {};
        const existingArgs = mcpServers['review-progress']?.args;
        if (existingArgs?.[0] === mcpServerPath) {
            return 'already-configured';
        }
        if (this.deps.existsSync(this.deps.settingsPath)) {
            this.deps.copyFileSync(this.deps.settingsPath, `${this.deps.settingsPath}.bak`);
        }
        mcpServers['review-progress'] = {
            command: 'node',
            args: [mcpServerPath],
        };
        settings.mcpServers = mcpServers;
        this.deps.writeFileSync(this.deps.settingsPath, JSON.stringify(settings, null, 2));
        try {
            const written = this.deps.readFileSync(this.deps.settingsPath, 'utf-8');
            if (!hasServerEntry(JSON.parse(written), 'review-progress')) {
                return 'validation-failed';
            }
        }
        catch {
            return 'validation-failed';
        }
        return 'configured';
    }
}
//# sourceMappingURL=configureMcp.usecase.js.map