"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrossProjectLearningEngine = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
class CrossProjectLearningEngine {
    constructor(projectPath = process.cwd()) {
        this.patterns = [];
        this.bestPractices = [];
        this.commonIssues = [];
        this.recommendations = [];
        this.learningDatabase = path_1.default.join(process.cwd(), ".mycontext", "learning-network.json");
        this.projectId = this.generateProjectId(projectPath);
    }
    async learnFromCommunity() {
        console.log("🌐 Learning from community patterns...");
        // Load existing learning data
        await this.loadLearningDatabase();
        // Analyze and update patterns
        await this.analyzePatterns();
        // Generate best practices
        await this.generateBestPractices();
        // Identify common issues
        await this.identifyCommonIssues();
        // Generate recommendations
        await this.generateRecommendations();
        // Save updated learning
        await this.saveLearningDatabase();
        return this;
    }
    async contributeToNetwork(projectIntelligence) {
        console.log("📤 Contributing to learning network...");
        // Extract learnable patterns from current project
        const newPatterns = await this.extractPatternsFromProject(projectIntelligence);
        // Update pattern frequency and confidence
        await this.updatePatternStatistics(newPatterns);
        // Record project outcomes
        await this.recordProjectOutcomes(projectIntelligence);
        console.log(`✅ Contributed ${newPatterns.length} patterns to the network`);
    }
    async loadLearningDatabase() {
        if (await fs_extra_1.default.pathExists(this.learningDatabase)) {
            try {
                const data = await fs_extra_1.default.readJson(this.learningDatabase);
                this.patterns = data.patterns || [];
                this.bestPractices = data.bestPractices || [];
                this.commonIssues = data.commonIssues || [];
                this.recommendations = data.recommendations || [];
            }
            catch (error) {
                console.warn("Failed to load learning database:", error);
                await this.initializeLearningDatabase();
            }
        }
        else {
            await this.initializeLearningDatabase();
        }
    }
    async initializeLearningDatabase() {
        // Initialize with common React/Next.js patterns
        this.patterns = [
            {
                id: "react-compound-components",
                name: "Compound Components",
                description: "Components that work together to form a cohesive UI element",
                frequency: 85,
                confidence: 0.9,
                projects: ["ecommerce", "dashboard", "cms"],
                context: ["complex-ui", "reusable-components"],
                benefits: ["Flexibility", "Composition", "API simplicity"],
                implementation: {
                    code: `const Card = ({ children }) => <div className="card">{children}</div>;
Card.Header = ({ children }) => <div className="card-header">{children}</div>;
Card.Body = ({ children }) => <div className="card-body">{children}</div>;
Card.Footer = ({ children }) => <div className="card-footer">{children}</div>;`,
                    dependencies: [],
                    setup: [
                        "Define main component",
                        "Attach sub-components as properties",
                    ],
                    examples: [
                        "<Card><Card.Header>Title</Card.Header><Card.Body>Content</Card.Body></Card>",
                    ],
                },
                related: ["render-props", "context-api"],
            },
            {
                id: "react-custom-hooks",
                name: "Custom Hooks",
                description: "Reusable stateful logic extracted into custom hooks",
                frequency: 95,
                confidence: 0.95,
                projects: ["all"],
                context: ["state-management", "logic-reuse"],
                benefits: ["Reusability", "Testability", "Separation of concerns"],
                implementation: {
                    code: `const useLocalStorage = (key, defaultValue) => {
  const [value, setValue] = useState(() => {
    const stored = localStorage.getItem(key);
    return stored ? JSON.parse(stored) : defaultValue;
  });

  useEffect(() => {
    localStorage.setItem(key, JSON.stringify(value));
  }, [key, value]);

  return [value, setValue];
};`,
                    dependencies: ["useState", "useEffect"],
                    setup: ["Extract logic", "Return state and handlers"],
                    examples: [
                        'const [theme, setTheme] = useLocalStorage("theme", "light");',
                    ],
                },
                related: ["react-hooks", "state-management"],
            },
        ];
        this.bestPractices = [
            {
                id: "typescript-strict-mode",
                category: "maintainability",
                title: "Enable TypeScript Strict Mode",
                description: "Use strict TypeScript configuration for better type safety",
                evidence: [
                    {
                        projectId: "sample-1",
                        metrics: { bugsReduced: 40, developmentTime: -10 },
                        outcome: "Significant reduction in runtime errors",
                        timestamp: new Date().toISOString(),
                    },
                ],
                adoptionRate: 78,
                impact: "high",
                difficulty: "easy",
            },
            {
                id: "component-testing",
                category: "testing",
                title: "Test Components in Isolation",
                description: "Write focused unit tests for each component",
                evidence: [
                    {
                        projectId: "sample-2",
                        metrics: { testCoverage: 85, bugDetection: 60 },
                        outcome: "Faster debugging and more confident deployments",
                        timestamp: new Date().toISOString(),
                    },
                ],
                adoptionRate: 65,
                impact: "high",
                difficulty: "medium",
            },
        ];
        this.commonIssues = [
            {
                id: "infinite-rerender-loop",
                type: "bug",
                pattern: "useEffect without proper dependencies",
                frequency: 45,
                severity: "medium",
                solutions: [
                    {
                        approach: "Add missing dependencies to dependency array",
                        successRate: 85,
                        timeToResolve: "15 minutes",
                        code: "useEffect(() => { /* effect */ }, [dependency1, dependency2]);",
                        notes: "Most common cause is missing dependencies",
                    },
                    {
                        approach: "Use useCallback for function dependencies",
                        successRate: 90,
                        timeToResolve: "30 minutes",
                        code: "const memoizedCallback = useCallback(() => { /* logic */ }, [dep]);",
                        notes: "When effect depends on functions",
                    },
                ],
                prevention: [
                    "Use ESLint exhaustive-deps rule",
                    "Consider useCallback for function dependencies",
                    "Review effect dependencies regularly",
                ],
            },
        ];
    }
    async analyzePatterns() {
        // Simulate pattern analysis from community data
        // In real implementation, this would analyze actual project data
        // Update pattern frequencies based on recent usage
        for (const pattern of this.patterns) {
            // Simulate natural evolution of pattern adoption
            const randomVariation = (Math.random() - 0.5) * 10;
            pattern.frequency = Math.max(0, Math.min(100, pattern.frequency + randomVariation));
            // Update confidence based on success rate
            if (pattern.frequency > 70) {
                pattern.confidence = Math.min(1, pattern.confidence + 0.05);
            }
        }
    }
    async generateBestPractices() {
        // Generate new best practices based on pattern success
        const highSuccessPatterns = this.patterns.filter((p) => p.confidence > 0.8 && p.frequency > 60);
        for (const pattern of highSuccessPatterns) {
            const existingPractice = this.bestPractices.find((bp) => bp.title.includes(pattern.name));
            if (!existingPractice) {
                this.bestPractices.push({
                    id: `practice-${pattern.id}`,
                    category: "maintainability",
                    title: `Adopt ${pattern.name} Pattern`,
                    description: `${pattern.description}. High success rate across projects.`,
                    evidence: [
                        {
                            projectId: "aggregated",
                            metrics: {
                                adoptionRate: pattern.frequency,
                                successRate: pattern.confidence * 100,
                            },
                            outcome: pattern.benefits.join(", "),
                            timestamp: new Date().toISOString(),
                        },
                    ],
                    adoptionRate: pattern.frequency,
                    impact: pattern.frequency > 80 ? "high" : "medium",
                    difficulty: pattern.implementation.dependencies.length > 2 ? "medium" : "easy",
                });
            }
        }
    }
    async identifyCommonIssues() {
        // Analyze patterns for potential issues
        const lowConfidencePatterns = this.patterns.filter((p) => p.confidence < 0.6);
        for (const pattern of lowConfidencePatterns) {
            const existingIssue = this.commonIssues.find((ci) => ci.pattern.includes(pattern.name));
            if (!existingIssue) {
                this.commonIssues.push({
                    id: `issue-${pattern.id}`,
                    type: "bug",
                    pattern: `Improper implementation of ${pattern.name}`,
                    frequency: Math.round((1 - pattern.confidence) * 100),
                    severity: pattern.confidence < 0.4 ? "high" : "medium",
                    solutions: [
                        {
                            approach: `Follow established ${pattern.name} pattern`,
                            successRate: 80,
                            timeToResolve: "1-2 hours",
                            code: pattern.implementation.code,
                            notes: `Review ${pattern.name} implementation guidelines`,
                        },
                    ],
                    prevention: [
                        `Use proven ${pattern.name} examples`,
                        "Test implementation thoroughly",
                        "Code review with experienced developers",
                    ],
                });
            }
        }
    }
    async generateRecommendations() {
        this.recommendations = [];
        // Recommend high-value patterns not yet adopted
        const currentProjectPatterns = await this.getCurrentProjectPatterns();
        const missingPatterns = this.patterns.filter((p) => p.confidence > 0.8 &&
            p.frequency > 70 &&
            !currentProjectPatterns.includes(p.name));
        for (const pattern of missingPatterns) {
            this.recommendations.push({
                id: `rec-pattern-${pattern.id}`,
                type: "pattern",
                title: `Adopt ${pattern.name}`,
                description: pattern.description,
                reasoning: `High success rate (${Math.round(pattern.confidence * 100)}%) and adoption (${pattern.frequency}%) across similar projects`,
                confidence: pattern.confidence,
                urgency: pattern.frequency > 85 ? "high" : "medium",
                context: pattern.context,
            });
        }
        // Recommend best practices
        const unimplementedPractices = this.bestPractices.filter((bp) => bp.impact === "high" && bp.difficulty === "easy");
        for (const practice of unimplementedPractices) {
            this.recommendations.push({
                id: `rec-practice-${practice.id}`,
                type: "practice",
                title: practice.title,
                description: practice.description,
                reasoning: `High impact with easy implementation. ${practice.adoptionRate}% adoption rate.`,
                confidence: practice.adoptionRate / 100,
                urgency: practice.impact === "high" ? "high" : "medium",
                context: [practice.category],
            });
        }
        // Recommend tools based on common patterns
        const toolRecommendations = this.generateToolRecommendations(currentProjectPatterns);
        this.recommendations.push(...toolRecommendations);
    }
    async getCurrentProjectPatterns() {
        // Analyze current project to identify existing patterns
        const patterns = [];
        try {
            const packagePath = path_1.default.join(process.cwd(), "package.json");
            if (await fs_extra_1.default.pathExists(packagePath)) {
                const pkg = await fs_extra_1.default.readJson(packagePath);
                // Infer patterns from dependencies
                if (pkg.dependencies?.["react-hook-form"])
                    patterns.push("Form Management");
                if (pkg.dependencies?.["react-query"])
                    patterns.push("Server State");
                if (pkg.dependencies?.["framer-motion"])
                    patterns.push("Animations");
                if (pkg.devDependencies?.["@testing-library/react"])
                    patterns.push("Component Testing");
            }
        }
        catch (error) {
            console.warn("Could not analyze current project patterns:", error);
        }
        return patterns;
    }
    generateToolRecommendations(currentPatterns) {
        const toolRecs = [];
        // Tool recommendations based on missing patterns
        if (!currentPatterns.includes("Component Testing")) {
            toolRecs.push({
                id: "rec-tool-testing",
                type: "tool",
                title: "Add React Testing Library",
                description: "Comprehensive testing solution for React components",
                reasoning: "Essential for maintainable React applications. Used by 89% of successful projects.",
                confidence: 0.89,
                urgency: "high",
                context: ["testing", "quality-assurance"],
            });
        }
        if (!currentPatterns.includes("State Management")) {
            toolRecs.push({
                id: "rec-tool-state",
                type: "tool",
                title: "Consider Zustand for State Management",
                description: "Lightweight state management with TypeScript support",
                reasoning: "Simpler than Redux, growing adoption rate of 67% in new projects.",
                confidence: 0.67,
                urgency: "medium",
                context: ["state-management", "scalability"],
            });
        }
        return toolRecs;
    }
    async extractPatternsFromProject(intelligence) {
        const extractedPatterns = [];
        // Extract patterns from architectural analysis
        for (const pattern of intelligence.architecturalPatterns) {
            const existing = this.patterns.find((p) => p.name === pattern.name);
            if (existing) {
                // Update existing pattern
                existing.frequency += 1;
                existing.projects.push(this.projectId);
            }
            else {
                // Create new pattern
                extractedPatterns.push({
                    id: `pattern-${Date.now()}-${pattern.name}`,
                    name: pattern.name,
                    description: pattern.context,
                    frequency: 1,
                    confidence: pattern.confidence,
                    projects: [this.projectId],
                    context: [pattern.context],
                    benefits: ["Identified in successful project"],
                    implementation: {
                        code: "",
                        dependencies: [],
                        setup: [],
                        examples: pattern.examples,
                    },
                    related: [],
                });
            }
        }
        return extractedPatterns;
    }
    async updatePatternStatistics(newPatterns) {
        // Add new patterns to the collection
        for (const pattern of newPatterns) {
            const existing = this.patterns.find((p) => p.name === pattern.name);
            if (!existing) {
                this.patterns.push(pattern);
            }
        }
        // Update overall statistics
        const totalProjects = new Set(this.patterns.flatMap((p) => p.projects))
            .size;
        for (const pattern of this.patterns) {
            // Recalculate frequency as percentage of total projects
            pattern.frequency = (pattern.projects.length / totalProjects) * 100;
            // Update confidence based on recent successes
            if (pattern.projects.length > 5) {
                pattern.confidence = Math.min(1, pattern.confidence + 0.1);
            }
        }
    }
    async recordProjectOutcomes(intelligence) {
        // Record project success metrics for learning
        const projectMetrics = {
            componentCount: intelligence.codebaseAwareness.files.filter((f) => f.type === "component").length,
            averageComplexity: this.calculateAverageComplexity(intelligence.codebaseAwareness.files),
            patternDiversity: intelligence.architecturalPatterns.length,
        };
        // Update best practice evidence
        for (const practice of this.bestPractices) {
            if (this.projectImplementsPractice(intelligence, practice)) {
                practice.evidence.push({
                    projectId: this.projectId,
                    metrics: { ...projectMetrics, timestamp: Date.now() },
                    outcome: "Successful implementation",
                    timestamp: new Date().toISOString(),
                });
                practice.adoptionRate += 1;
            }
        }
    }
    calculateAverageComplexity(files) {
        if (files.length === 0)
            return 0;
        const totalComplexity = files.reduce((sum, file) => sum + (file.complexity || 0), 0);
        return totalComplexity / files.length;
    }
    projectImplementsPractice(intelligence, practice) {
        // Simplified check - in real implementation, this would be more sophisticated
        switch (practice.id) {
            case "typescript-strict-mode":
                return intelligence.codebaseAwareness.files.some((f) => f.language === "typescript");
            case "component-testing":
                return intelligence.codebaseAwareness.files.some((f) => f.type === "test");
            default:
                return false;
        }
    }
    generateProjectId(projectPath) {
        // Generate a unique but consistent ID for this project
        const projectName = path_1.default.basename(projectPath);
        const hash = projectName
            .split("")
            .reduce((acc, char) => ((acc << 5) - acc + char.charCodeAt(0)) & 0xfffff, 0);
        return `${projectName}-${hash.toString(16)}`;
    }
    async saveLearningDatabase() {
        const data = {
            patterns: this.patterns,
            bestPractices: this.bestPractices,
            commonIssues: this.commonIssues,
            recommendations: this.recommendations,
            lastUpdated: new Date().toISOString(),
            version: "1.0.0",
        };
        await fs_extra_1.default.ensureDir(path_1.default.dirname(this.learningDatabase));
        await fs_extra_1.default.writeJson(this.learningDatabase, data, { spaces: 2 });
    }
    // Public API methods
    async getRecommendationsForProject() {
        return this.recommendations.filter((r) => r.urgency === "high").slice(0, 5);
    }
    async getPatternSuggestions(category) {
        let patterns = this.patterns.filter((p) => p.confidence > 0.7);
        if (category) {
            patterns = patterns.filter((p) => p.context.includes(category));
        }
        return patterns.sort((a, b) => b.frequency - a.frequency).slice(0, 10);
    }
    async getBestPracticesFor(category) {
        return this.bestPractices
            .filter((bp) => bp.category === category && bp.impact === "high")
            .sort((a, b) => b.adoptionRate - a.adoptionRate);
    }
    async getCommonIssuesPreventionList() {
        return this.commonIssues
            .filter((issue) => issue.severity === "high" || issue.severity === "critical")
            .flatMap((issue) => issue.prevention)
            .slice(0, 10);
    }
    async generateLearningReport() {
        const report = `
# Cross-Project Learning Report

## Pattern Analysis
- **Total Patterns Tracked**: ${this.patterns.length}
- **High-Confidence Patterns**: ${this.patterns.filter((p) => p.confidence > 0.8).length}
- **Emerging Patterns**: ${this.patterns.filter((p) => p.frequency > 70).length}

## Recommendations Summary
- **High Priority**: ${this.recommendations.filter((r) => r.urgency === "high").length}
- **Pattern Adoptions**: ${this.recommendations.filter((r) => r.type === "pattern").length}
- **Tool Suggestions**: ${this.recommendations.filter((r) => r.type === "tool").length}

## Top Patterns
${this.patterns
            .sort((a, b) => b.frequency - a.frequency)
            .slice(0, 5)
            .map((p, i) => `${i + 1}. ${p.name} (${p.frequency}% adoption, ${Math.round(p.confidence * 100)}% confidence)`)
            .join("\n")}

## Critical Issues to Avoid
${this.commonIssues
            .filter((i) => i.severity === "critical")
            .map((i, idx) => `${idx + 1}. ${i.pattern} (${i.frequency}% occurrence rate)`)
            .join("\n")}

Generated: ${new Date().toISOString()}
`;
        const reportPath = path_1.default.join(process.cwd(), ".mycontext", "learning-report.md");
        await fs_extra_1.default.writeFile(reportPath, report);
        return reportPath;
    }
}
exports.CrossProjectLearningEngine = CrossProjectLearningEngine;
//# sourceMappingURL=CrossProjectLearning.js.map