#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.enhance = exports.EnhanceCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const spinner_1 = require("../utils/spinner");
const EnhancementAgent_1 = require("../agents/implementations/EnhancementAgent");
const contextLoader_1 = require("../utils/contextLoader");
class EnhanceCommand {
    constructor() {
        this.useAgentSDK = true; // NEW: Use Agent SDK by default
        this.enhancementAgent = new EnhancementAgent_1.EnhancementAgent();
    }
    async execute(target, options) {
        const spinner = new spinner_1.EnhancedSpinner("Initializing enhancement...");
        try {
            spinner.start();
            // Load project context for better enhancements
            const contextLoader = new contextLoader_1.ContextLoader();
            const projectContext = await contextLoader.loadProjectContext({
                verbose: options.verbose || false,
                required: false,
            });
            if (options.verbose) {
                const summary = contextLoader.getContextSummary(projectContext);
                console.log(chalk_1.default.gray(`📁 Context: ${summary}`));
            }
            // Validate target
            if (!target) {
                throw new Error("Target component or group is required");
            }
            // Check if target is a file or group
            const isFile = target.endsWith(".tsx") || target.endsWith(".jsx");
            const isGroup = !isFile && !target.includes(".");
            if (isFile) {
                await this.enhanceSingleComponent(target, options, spinner);
            }
            else if (isGroup) {
                await this.enhanceComponentGroup(target, options, spinner);
            }
            else {
                throw new Error("Target must be a component file (.tsx/.jsx) or component group");
            }
            spinner.succeed("Enhancement completed successfully!");
            this.printNextCommands();
        }
        catch (error) {
            spinner.fail("Enhancement failed");
            console.error(chalk_1.default.red("❌ Error:"), error);
            process.exit(1);
        }
    }
    /**
     * NEW: Enhance component using Agent SDK with refactoring agent
     */
    async enhanceWithAgentSDK(componentCode, componentName, prompt, options, spinner) {
        // Get Agent SDK client
        // const agentClient = await getAgentClient();
        // DISABLED: Agent client is not available
        throw new Error("Agent client is disabled - use standard enhancement instead");
    }
    /**
     * Extract code from agent response
     */
    extractCodeFromResponse(response) {
        const codeBlockMatch = response.match(/```(?:tsx?|typescript|jsx?)\n([\s\S]*?)\n```/);
        return codeBlockMatch ? codeBlockMatch[1] || null : null;
    }
    /**
     * Parse changes from response
     */
    parseChangesFromResponse(response) {
        const changes = [];
        const changesSection = response.match(/changes made:?\n([\s\S]*?)(?:\n\n|quality score)/i);
        if (changesSection) {
            const lines = changesSection[1]?.split("\n") || [];
            lines.forEach((line) => {
                if (line.trim().startsWith("-") || line.trim().startsWith("•")) {
                    changes.push({
                        type: "modify",
                        description: line.trim().substring(1).trim(),
                    });
                }
            });
        }
        return changes;
    }
    /**
     * Parse quality score from response
     */
    parseScoreFromResponse(response) {
        const scoreMatch = response.match(/quality score:?\s*(\d+)/i);
        return scoreMatch ? parseInt(scoreMatch[1] || "85") : 85;
    }
    /**
     * Parse suggestions from response
     */
    parseSuggestionsFromResponse(response) {
        const accessibilityMatch = response.match(/accessibility suggestions?:?\n([\s\S]*?)(?:\n\n|performance)/i);
        const performanceMatch = response.match(/performance suggestions?:?\n([\s\S]*?)(?:\n\n|$)/i);
        const parseSuggestions = (text) => {
            if (!text)
                return [];
            return text
                .split("\n")
                .map((line) => line.trim())
                .filter((line) => line.startsWith("-") || line.startsWith("•"))
                .map((line) => line.substring(1).trim());
        };
        return {
            accessibility: parseSuggestions(accessibilityMatch?.[1]),
            performance: parseSuggestions(performanceMatch?.[1]),
        };
    }
    printNextCommands() {
        try {
            console.log("");
            console.log(chalk_1.default.blue("➡️ Next commands:"));
            console.log(chalk_1.default.gray("   Preview at: https://studio.mycontext.app"));
            console.log(chalk_1.default.gray("   mycontext refine <component> --apply"));
            console.log(chalk_1.default.gray("   mycontext enhance <group>"));
            console.log("");
        }
        catch { }
    }
    async enhanceSingleComponent(componentPath, options, spinner) {
        spinner.updateText("Reading component file...");
        // Read component file
        const fullPath = path_1.default.resolve(componentPath);
        if (!(await fs_extra_1.default.pathExists(fullPath))) {
            throw new Error(`Component file not found: ${componentPath}`);
        }
        const componentCode = await fs_extra_1.default.readFile(fullPath, "utf-8");
        const componentName = path_1.default.basename(fullPath, path_1.default.extname(fullPath));
        // Get enhancement prompt
        const prompt = await this.getEnhancementPrompt(options);
        spinner.updateText("Enhancing component with Agent SDK...");
        let result;
        // NEW: Try Agent SDK first
        if (this.useAgentSDK) {
            try {
                result = await this.enhanceWithAgentSDK(componentCode, componentName, prompt, options, spinner);
            }
            catch (error) {
                console.log(chalk_1.default.yellow("⚠️  Agent SDK failed, falling back to standard enhancement"));
                // Fallback to standard enhancement
                result = await this.enhancementAgent.run({
                    currentComponent: componentCode,
                    userPrompt: prompt,
                    designContext: this.extractDesignContext(componentCode),
                    constraints: {
                        maxDependencies: 5,
                        targetFramework: "react",
                        uiLibrary: "shadcn",
                    },
                });
            }
        }
        else {
            // Use standard enhancement agent
            result = await this.enhancementAgent.run({
                currentComponent: componentCode,
                userPrompt: prompt,
                designContext: this.extractDesignContext(componentCode),
                constraints: {
                    maxDependencies: 5,
                    targetFramework: "react",
                    uiLibrary: "shadcn",
                },
            });
        }
        // Track usage
        await this.trackEnhancementUsage(componentName, "single", result.validation.score);
        // Save enhanced component
        const outputPath = options.output || fullPath;
        await fs_extra_1.default.writeFile(outputPath, result.enhancedComponent);
        // Display results
        if (options.interactive || options.outputFormat === "structured") {
            // Return structured output for VS Code extension
            return this.generateStructuredOutput(result, componentName, options);
        }
        else {
            this.displayEnhancementResults(result, componentName);
        }
        spinner.updateText("Enhancement completed!");
    }
    async enhanceComponentGroup(groupName, options, spinner) {
        spinner.updateText("Reading component group...");
        // Find component group directory
        const groupDir = path_1.default.join("components", "generated", groupName);
        if (!(await fs_extra_1.default.pathExists(groupDir))) {
            throw new Error(`Component group not found: ${groupName}`);
        }
        // Get all component files in group
        const files = await fs_extra_1.default.readdir(groupDir);
        const componentFiles = files.filter((file) => file.endsWith(".tsx") && !file.endsWith(".d.ts"));
        if (componentFiles.length === 0) {
            throw new Error(`No component files found in group: ${groupName}`);
        }
        // Get enhancement prompt
        const prompt = await this.getEnhancementPrompt(options);
        spinner.updateText(`Enhancing ${componentFiles.length} components...`);
        let totalScore = 0;
        let enhancedCount = 0;
        // Enhance each component
        for (const file of componentFiles) {
            try {
                const filePath = path_1.default.join(groupDir, file);
                const componentCode = await fs_extra_1.default.readFile(filePath, "utf-8");
                const componentName = path_1.default.basename(file, ".tsx");
                spinner.updateText(`Enhancing ${componentName}...`);
                const result = await this.enhancementAgent.run({
                    currentComponent: componentCode,
                    userPrompt: prompt,
                    designContext: this.extractDesignContext(componentCode),
                    constraints: {
                        maxDependencies: 5,
                        targetFramework: "react",
                        uiLibrary: "shadcn",
                    },
                });
                // Save enhanced component
                await fs_extra_1.default.writeFile(filePath, result.enhancedComponent);
                totalScore += result.validation.score;
                enhancedCount++;
                // Track usage
                await this.trackEnhancementUsage(componentName, "group", result.validation.score);
            }
            catch (error) {
                console.warn(chalk_1.default.yellow(`⚠️  Failed to enhance ${file}:`), error);
            }
        }
        // Display group results
        const averageScore = enhancedCount > 0 ? totalScore / enhancedCount : 0;
        this.displayGroupResults(enhancedCount, componentFiles.length, averageScore);
        spinner.updateText(`Enhanced ${enhancedCount}/${componentFiles.length} components`);
    }
    async getEnhancementPrompt(options) {
        if (options.prompt) {
            return options.prompt;
        }
        // Interactive prompt
        const readline = require("readline");
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout,
        });
        return new Promise((resolve) => {
            rl.question(chalk_1.default.blue("Enter enhancement prompt: "), (answer) => {
                rl.close();
                resolve(answer.trim() ||
                    "Enhance this component with modern design patterns");
            });
        });
    }
    extractDesignContext(componentCode) {
        // Simple extraction of design context from component
        const colors = componentCode.match(/(?:bg-|text-|border-)([a-z]+-\d+|primary|secondary|accent)/g) || [];
        const spacing = componentCode.match(/(?:p-|m-|gap-)(\d+|xs|sm|md|lg|xl)/g) || [];
        return {
            colors: [...new Set(colors.map((c) => c.split("-")[1]))],
            spacing: spacing.length > 0 ? spacing[0]?.split("-")[1] || "md" : "md",
            typography: "inter",
            interactions: ["hover", "click"],
        };
    }
    async trackEnhancementUsage(componentName, type, qualityScore) {
        try {
            console.log(chalk_1.default.gray(`📊 Usage tracking disabled`));
        }
        catch (error) {
            console.warn(chalk_1.default.yellow("⚠️  Failed to track usage:"), error);
        }
    }
    displayEnhancementResults(result, componentName) {
        console.log(chalk_1.default.green(`\n✅ Enhanced ${componentName}`));
        console.log(chalk_1.default.gray(`Quality Score: ${result.validation.score}/100`));
        if (result.changes.totalChanges > 0) {
            console.log(chalk_1.default.gray(`Changes: ${result.changes.totalChanges} lines modified`));
        }
        if (result.suggestions.accessibility.length > 0) {
            console.log(chalk_1.default.yellow("\n🔍 Accessibility Suggestions:"));
            result.suggestions.accessibility.forEach((suggestion) => {
                console.log(chalk_1.default.gray(`  • ${suggestion}`));
            });
        }
        if (result.suggestions.performance.length > 0) {
            console.log(chalk_1.default.blue("\n⚡ Performance Suggestions:"));
            result.suggestions.performance.forEach((suggestion) => {
                console.log(chalk_1.default.gray(`  • ${suggestion}`));
            });
        }
        console.log(chalk_1.default.gray(`\n💳 Credits used: ${this.calculateCredits(result.validation.score)}`));
    }
    displayGroupResults(enhancedCount, totalCount, averageScore) {
        console.log(chalk_1.default.green(`\n✅ Enhanced ${enhancedCount}/${totalCount} components`));
        console.log(chalk_1.default.gray(`Average Quality Score: ${averageScore.toFixed(1)}/100`));
        if (enhancedCount < totalCount) {
            console.log(chalk_1.default.yellow(`⚠️  ${totalCount - enhancedCount} components failed to enhance`));
        }
        const totalCredits = this.calculateCredits(averageScore) * enhancedCount;
        console.log(chalk_1.default.gray(`\n💳 Total credits used: ${totalCredits}`));
    }
    calculateCredits(qualityScore) {
        // Credit calculation based on quality score
        if (qualityScore >= 90)
            return 3;
        if (qualityScore >= 80)
            return 2;
        if (qualityScore >= 70)
            return 1;
        return 1; // Minimum credit cost
    }
    /**
     * Generate structured output for VS Code extension
     */
    generateStructuredOutput(result, componentName, options) {
        const structuredOutput = {
            success: true,
            componentName,
            enhancedCode: result.enhancedComponent,
            originalCode: result.originalComponent || "",
            changes: {
                totalChanges: result.changes.totalChanges,
                addedLines: result.changes.addedLines,
                removedLines: result.changes.removedLines,
                modifiedLines: result.changes.modifiedLines,
            },
            validation: {
                score: result.validation.score,
                isValid: result.validation.isValid,
                errors: result.validation.errors,
                warnings: result.validation.warnings,
            },
            suggestions: result.suggestions,
            metadata: {
                executionTime: Date.now(),
                model: "mycontext-enhancement",
                temperature: options.temperature,
                maxTokens: options.maxTokens,
            },
        };
        // Output based on format
        switch (options.outputFormat) {
            case "diff":
                return this.generateDiffOutput(structuredOutput);
            case "preview":
                return this.generatePreviewOutput(structuredOutput);
            case "structured":
            default:
                return structuredOutput;
        }
    }
    /**
     * Generate diff output for side-by-side comparison
     */
    generateDiffOutput(structuredOutput) {
        return {
            ...structuredOutput,
            diff: {
                unified: this.generateUnifiedDiff(structuredOutput.originalCode, structuredOutput.enhancedCode),
                sideBySide: this.generateSideBySideDiff(structuredOutput.originalCode, structuredOutput.enhancedCode),
            },
        };
    }
    /**
     * Generate preview output for component rendering
     */
    generatePreviewOutput(structuredOutput) {
        return {
            ...structuredOutput,
            preview: {
                canRender: this.canRenderComponent(structuredOutput.enhancedCode),
                dependencies: this.extractDependencies(structuredOutput.enhancedCode),
                props: this.extractComponentProps(structuredOutput.enhancedCode),
                warnings: this.extractRenderWarnings(structuredOutput.enhancedCode),
            },
        };
    }
    /**
     * Generate unified diff format
     */
    generateUnifiedDiff(original, enhanced) {
        // Simple diff implementation - in production, use a proper diff library
        const originalLines = original.split("\n");
        const enhancedLines = enhanced.split("\n");
        let diff = "";
        let i = 0, j = 0;
        while (i < originalLines.length || j < enhancedLines.length) {
            if (i < originalLines.length &&
                j < enhancedLines.length &&
                originalLines[i] === enhancedLines[j]) {
                diff += ` ${originalLines[i]}\n`;
                i++;
                j++;
            }
            else if (i < originalLines.length) {
                diff += `-${originalLines[i]}\n`;
                i++;
            }
            else if (j < enhancedLines.length) {
                diff += `+${enhancedLines[j]}\n`;
                j++;
            }
        }
        return diff;
    }
    /**
     * Generate side-by-side diff format
     */
    generateSideBySideDiff(original, enhanced) {
        const originalLines = original.split("\n");
        const enhancedLines = enhanced.split("\n");
        const maxLines = Math.max(originalLines.length, enhancedLines.length);
        const sideBySide = [];
        for (let i = 0; i < maxLines; i++) {
            const originalLine = originalLines[i] || "";
            const enhancedLine = enhancedLines[i] || "";
            const isModified = originalLine !== enhancedLine;
            sideBySide.push({
                lineNumber: i + 1,
                original: originalLine,
                enhanced: enhancedLine,
                modified: isModified,
                type: isModified ? "modified" : "unchanged",
            });
        }
        return sideBySide;
    }
    /**
     * Check if component can be rendered
     */
    canRenderComponent(code) {
        // Basic validation - check for common React patterns
        const hasExport = /export\s+(default\s+)?function|export\s+(default\s+)?const|export\s+class/.test(code);
        const hasReturn = /return\s*\(/.test(code);
        const hasJSX = /<[A-Z][a-zA-Z]*/.test(code);
        return hasExport && hasReturn && hasJSX;
    }
    /**
     * Extract component dependencies
     */
    extractDependencies(code) {
        const imports = code.match(/import\s+.*?\s+from\s+['"]([^'"]+)['"]/g) || [];
        return imports
            .map((imp) => {
            const match = imp.match(/from\s+['"]([^'"]+)['"]/);
            return match ? match[1] : "";
        })
            .filter((item) => Boolean(item));
    }
    /**
     * Extract component props interface
     */
    extractComponentProps(code) {
        const propsMatch = code.match(/interface\s+(\w+)Props\s*\{([^}]+)\}/);
        if (!propsMatch)
            return null;
        const propsText = propsMatch[2];
        if (!propsText)
            return [];
        const props = propsText
            .split("\n")
            .map((line) => line.trim())
            .filter((line) => line && !line.startsWith("/") && !line.startsWith("*"))
            .map((line) => {
            const propMatch = line.match(/(\w+)\??:\s*(.+?)(?:;|$)/);
            if (propMatch) {
                return {
                    name: propMatch[1],
                    type: propMatch[2]?.trim() || "any",
                    required: !line.includes("?"),
                };
            }
            return null;
        })
            .filter(Boolean);
        return props;
    }
    /**
     * Extract render warnings
     */
    extractRenderWarnings(code) {
        const warnings = [];
        // Check for common issues
        if (!code.includes("useState") && code.includes("setState")) {
            warnings.push("Component uses setState but no useState hook found");
        }
        if (code.includes("useEffect") && !code.includes("dependencies")) {
            warnings.push("useEffect missing dependency array");
        }
        if (code.includes("onClick") && !code.includes("aria-label")) {
            warnings.push("Interactive element missing accessibility label");
        }
        return warnings;
    }
}
exports.EnhanceCommand = EnhanceCommand;
// CLI command setup
const enhanceCommand = new EnhanceCommand();
exports.enhance = new commander_1.Command("enhance")
    .description("Enhance React components with AI-powered improvements")
    .argument("<target>", "Component file (.tsx/.jsx) or component group name")
    .option("-i, --input <path>", "Input component file path")
    .option("-o, --output <path>", "Output file path (defaults to input)")
    .option("-p, --prompt <text>", "Enhancement prompt")
    .option("--temperature <number>", "Generation temperature (0-1)", "0.7")
    .option("--max-tokens <number>", "Maximum tokens", "4000")
    .option("--credits <number>", "Credit limit for enhancement")
    .option("--yes", "Skip prompts (non-interactive mode)")
    // NEW: Refinement-specific options
    .option("--interactive", "VS Code extension mode with structured output")
    .option("--output-format <format>", "Output format: default, structured, diff, preview", "default")
    .option("--show-changes", "Show detailed changes in output")
    .option("--preserve-history", "Keep enhancement history for rollback")
    .action(async (target, options) => {
    try {
        await enhanceCommand.execute(target, {
            ...options,
            temperature: parseFloat(options.temperature),
            maxTokens: parseInt(options.maxTokens),
            outputFormat: options.outputFormat || "default",
            interactive: options.interactive || false,
            showChanges: options.showChanges || false,
            preserveHistory: options.preserveHistory || false,
        });
    }
    catch (error) {
        console.error(chalk_1.default.red("❌ Enhancement failed:"), error);
        process.exit(1);
    }
});
//# sourceMappingURL=enhance.js.map