"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.installMcpCommand = installMcpCommand;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const path = tslib_1.__importStar(require("path"));
const logger_js_1 = require("../utils/logger.js");
async function installMcpCommand() {
    logger_js_1.logger.info('Installing Crafted Context Manager MCP for Claude Code...\n');
    const projectPath = process.cwd();
    const configPath = path.join(projectPath, '.mcp.json');
    const contextMcpPath = path.resolve(projectPath, '.ai/context/dist/mcp-server.js');
    const contextMcpDir = path.resolve(projectPath, '.ai/context');
    if (!fs.existsSync(contextMcpDir)) {
        logger_js_1.logger.error('Context Manager directory not found!');
        logger_js_1.logger.info('Make sure you are in a Crafted SaaS project with AI system installed.');
        return;
    }
    if (!fs.existsSync(contextMcpPath)) {
        logger_js_1.logger.info('Context Manager not built. Installing dependencies and building...\n');
        const { execSync } = await import('child_process');
        try {
            logger_js_1.logger.info('Installing dependencies...');
            execSync('pnpm install --ignore-workspace', {
                stdio: 'inherit',
                cwd: contextMcpDir
            });
            logger_js_1.logger.info('\nBuilding Context Manager...');
            execSync('pnpm build', {
                stdio: 'inherit',
                cwd: contextMcpDir
            });
            logger_js_1.logger.success('\nContext Manager built successfully!\n');
        }
        catch (error) {
            logger_js_1.logger.error('Failed to build Context Manager');
            logger_js_1.logger.info('Try manually:\n  cd .ai/context\n  pnpm install\n  pnpm build');
            return;
        }
    }
    let mcpConfig = {};
    if (fs.existsSync(configPath)) {
        try {
            const content = fs.readFileSync(configPath, 'utf-8');
            mcpConfig = JSON.parse(content);
            logger_js_1.logger.info('Existing .mcp.json found, updating...');
        }
        catch {
            logger_js_1.logger.warn('Could not parse existing config, creating new one...');
        }
    }
    else {
        logger_js_1.logger.info('Creating .mcp.json...');
    }
    if (!mcpConfig.mcpServers) {
        mcpConfig.mcpServers = {};
    }
    mcpConfig.mcpServers['crafted-context'] = {
        type: 'stdio',
        command: 'node',
        args: [contextMcpPath],
        env: {},
    };
    try {
        fs.writeFileSync(configPath, JSON.stringify(mcpConfig, null, 2));
        logger_js_1.logger.success('Context Manager MCP installed successfully!\n');
        logger_js_1.logger.info('Configuration:');
        console.log(`  Config file:   ${configPath}`);
        console.log(`  Context MCP:   ${contextMcpPath}\n`);
        logger_js_1.logger.info('Next steps:');
        console.log('  1. Type `claude mcp list` to verify the MCP is configured');
        console.log('  2. The Context Manager will be automatically loaded\n');
        logger_js_1.logger.info('Context Manager tools:');
        console.log('  • get_full_context      - Get complete project with all files');
        console.log('  • search_in_context     - Search for files or content patterns');
        console.log('  • get_architecture_info - Get architecture (level, platform, packages)');
        console.log('  • get_file_content      - Get specific file content');
        console.log('  • get_context_summary   - Get project summary\n');
    }
    catch (error) {
        logger_js_1.logger.error(`Failed to write config: ${error}`);
        logger_js_1.logger.info('\nManual configuration:');
        console.log(JSON.stringify(mcpConfig, null, 2));
    }
}
//# sourceMappingURL=install-mcp.js.map