"use strict";
/**
 * 😽 TURBO-CAT™ (Turbo Catalytic Analysis Technology) v1.0.0
 * 134-Format Intelligence Catalyst with F1-Inspired Performance
 *
 * Like a catalytic converter for code - transforms raw file discoveries into pure intelligence!
 * "From dirty exhaust to clean boost" - 134 formats, 2-layer scan, <50ms delivery
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TurboCat = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const native_file_finder_1 = require("./native-file-finder");
// 😽 Import TURBO-CAT Knowledge Chamber - 134 Format Catalyst
const turbo_cat_knowledge_1 = require("./turbo-cat-knowledge");
/**
 * 🔍 TWO-LAYERED DISCOVERY ENGINE
 * Layer 1: Direct format scanning
 * Layer 2: Content confirmation & usage validation
 */
class TurboCat {
    knowledgeBase = turbo_cat_knowledge_1.KNOWLEDGE_BASE;
    /**
     * 😽 TURBO-CAT DUAL-STAGE CATALYST
     * Stage 1: Raw format intake (like air intake)
     * Stage 2: Catalytic conversion to intelligence (like exhaust treatment)
     */
    async discoverFormats(projectDir = process.cwd()) {
        const discoveredFormats = [];
        // LAYER 1: Format Discovery (based on proven two-layered file search)
        const foundFormats = await this.layerOneFormatScan(projectDir);
        // LAYER 2: F1-OPTIMIZED Content Confirmation (parallel validation)
        const confirmationPromises = foundFormats.map(async (formatResult) => {
            const confirmed = await this.layerTwoContentConfirmation(formatResult);
            return {
                ...formatResult,
                confirmed
            };
        });
        // 🏎️ F1-OPTIMIZATION: Parallel confirmation with timeout
        const discoveredFormatsArray = await Promise.allSettled(confirmationPromises);
        discoveredFormats.push(...discoveredFormatsArray
            .filter(result => result.status === 'fulfilled')
            .map(result => result.value));
        // Analyze results
        return this.analyzeFormats(discoveredFormats);
    }
    /**
     * LAYER 1: Hybrid format scanning (fs.readdir + glob for optimal performance)
     */
    async layerOneFormatScan(projectDir) {
        const results = [];
        // PHASE 1A: fs.readdir for known config files (fast, precise)
        await this.scanConfigFiles(projectDir, results);
        // PHASE 1B: glob for pattern-based discovery (broader, efficient)
        await this.scanPatternFiles(projectDir, results);
        return results;
    }
    /**
     * PHASE 1A: Use fs.readdir for known config files (optimal for specific files)
     */
    async scanConfigFiles(projectDir, results) {
        let currentDir = path_1.default.resolve(projectDir);
        // Check up to 10 parent directories (same as .faf search)
        for (let i = 0; i < 10; i++) {
            try {
                // Fast directory scan using fs.readdir (optimal for specific files)
                const files = await fs_1.promises.readdir(currentDir);
                // Check each file against known config files
                for (const file of files) {
                    const formatInfo = this.knowledgeBase[file];
                    if (formatInfo) {
                        const fullPath = path_1.default.join(currentDir, file);
                        let fileStats = {};
                        try {
                            const stats = await fs_1.promises.stat(fullPath);
                            fileStats = {
                                fileSize: stats.size,
                                lastModified: stats.mtime,
                                discovered: new Date()
                            };
                        }
                        catch {
                            // Continue without stats
                        }
                        results.push({
                            fileName: file,
                            formatType: file,
                            frameworks: formatInfo.frameworks,
                            slotMappings: formatInfo.slots || {},
                            priority: formatInfo.priority,
                            intelligence: formatInfo.intelligence,
                            confirmed: false, // Will be confirmed in Layer 2
                            filePath: fullPath,
                            ...fileStats
                        });
                    }
                }
                // Move to parent directory (proven .faf technique)
                const parentDir = path_1.default.dirname(currentDir);
                if (parentDir === currentDir) {
                    break; // Reached filesystem root
                }
                currentDir = parentDir;
            }
            catch {
                // Skip this directory if we can't read it (robust error handling)
                const parentDir = path_1.default.dirname(currentDir);
                if (parentDir === currentDir) {
                    break;
                }
                currentDir = parentDir;
            }
        }
    }
    /**
     * PHASE 1B: F1-OPTIMIZED pattern scanning (performance-first approach)
     */
    async scanPatternFiles(projectDir, results) {
        try {
            // 🏎️ F1-OPTIMIZATION: Native file finder - ZERO dependencies!
            const files = await (0, native_file_finder_1.findFiles)(projectDir, {
                extensions: ['.py', '.ts', '.tsx', '.js', '.jsx', '.svelte', '.vue'],
                ignore: ['node_modules', '.git', 'dist', 'build', 'venv', '__pycache__'],
                maxFiles: 10, // Limit total files for speed
                absolute: true
            });
            // 🏎️ F1-OPTIMIZATION: Process files in parallel batches
            const batchSize = 3;
            for (let i = 0; i < files.length; i += batchSize) {
                const batch = files.slice(i, i + batchSize);
                // Process batch in parallel
                await Promise.all(batch.map(async (filePath) => {
                    const ext = path_1.default.extname(filePath);
                    const formatInfo = this.knowledgeBase[`*${ext}`];
                    if (formatInfo && !results.some(r => r.filePath === filePath)) {
                        results.push({
                            fileName: path_1.default.basename(filePath),
                            formatType: ext,
                            frameworks: formatInfo.frameworks,
                            slotMappings: formatInfo.slots || {},
                            priority: formatInfo.priority,
                            intelligence: formatInfo.intelligence,
                            confirmed: false,
                            filePath
                        });
                    }
                }));
            }
        }
        catch {
            // Graceful fallback if glob fails
        }
    }
    /**
     * Extract deep intelligence from package.json - THE KILLER FEATURE!
     * This is what makes FAB-FORMATS so powerful!
     */
    async extractPackageJsonIntelligence(content) {
        try {
            const pkg = JSON.parse(content);
            const allDeps = { ...pkg.dependencies, ...pkg.devDependencies, ...pkg.peerDependencies };
            // Calculate quality score like FAB-FORMATS does!
            let score = 0;
            const detectedFrameworks = [];
            // TIER 1: Essential metadata (30 points)
            if (pkg.name)
                score += 10;
            if (pkg.description)
                score += 10;
            if (pkg.version)
                score += 5;
            if (pkg.author || pkg.license)
                score += 5;
            // TIER 2: Scripts showing development maturity (30 points)
            const scripts = pkg.scripts || {};
            if (scripts.dev || scripts.start)
                score += 10;
            if (scripts.build)
                score += 10;
            if (scripts.test || scripts.check)
                score += 10;
            // TIER 3: Dependencies showing tech stack (40 points)
            const depCount = Object.keys(allDeps).length;
            if (depCount >= 15)
                score += 20;
            else if (depCount >= 8)
                score += 15;
            else if (depCount >= 3)
                score += 10;
            else if (depCount >= 1)
                score += 5;
            // TIER 4: Modern toolchain detection (50 points)
            if (allDeps['typescript'] || allDeps['@types/node'])
                score += 10;
            if (allDeps['vite'] || allDeps['webpack'] || allDeps['rollup'])
                score += 10;
            if (allDeps['vitest'] || allDeps['jest'] || allDeps['playwright'])
                score += 10;
            if (allDeps['eslint'] || allDeps['prettier'])
                score += 10;
            if (allDeps['tailwindcss'] || allDeps['@emotion/styled'])
                score += 10;
            // TIER 5: Framework detection (MASSIVE BONUS: 50 points)
            if (allDeps['svelte'] || allDeps['@sveltejs/kit']) {
                score += 25;
                detectedFrameworks.push('Svelte');
            }
            if (allDeps['react'] || allDeps['next']) {
                score += 20;
                detectedFrameworks.push('React');
            }
            if (allDeps['vue'] || allDeps['nuxt']) {
                score += 20;
                detectedFrameworks.push('Vue');
            }
            if (allDeps['@angular/core']) {
                score += 15;
                detectedFrameworks.push('Angular');
            }
            if (allDeps['astro']) {
                score += 20;
                detectedFrameworks.push('Astro');
            }
            // Return the intelligence object that score-calculator expects!
            return {
                name: pkg.name,
                description: pkg.description,
                version: pkg.version,
                scripts: pkg.scripts,
                dependencies: allDeps,
                author: pkg.author,
                license: pkg.license,
                repository: pkg.repository,
                frameworks: detectedFrameworks,
                score: Math.min(score, 150), // Cap at 150
                depCount,
                hasTests: !!(scripts.test || scripts.check),
                hasBuild: !!scripts.build,
                hasTypeScript: !!(allDeps['typescript'] || allDeps['@types/node']),
                hasLinting: !!(allDeps['eslint'] || allDeps['prettier']),
                hasStyling: !!(allDeps['tailwindcss'] || allDeps['@emotion/styled'] || allDeps['styled-components'])
            };
        }
        catch {
            return null;
        }
    }
    /**
     * LAYER 2: F1-OPTIMIZED Content confirmation WITH INTELLIGENCE EXTRACTION!
     */
    async layerTwoContentConfirmation(format) {
        try {
            // 🏎️ F1-OPTIMIZATION: Skip content reading for low-priority formats
            if (format.priority < 15 && Math.random() > 0.5) {
                return true; // Assume valid for speed on low-priority formats
            }
            // 🏎️ F1-OPTIMIZATION: Read only first 1KB for most files (10x faster)
            const maxBytes = format.formatType.endsWith('.json') || format.formatType === 'Dockerfile' ? 2048 : 1024;
            const buffer = Buffer.alloc(maxBytes);
            const fd = await fs_1.promises.open(format.filePath, 'r');
            const { bytesRead } = await fd.read(buffer, 0, maxBytes, 0);
            await fd.close();
            const content = buffer.slice(0, bytesRead).toString('utf-8');
            // For package.json, we ALWAYS read and extract intelligence
            if (format.formatType === 'package.json') {
                const fullContent = await fs_1.promises.readFile(format.filePath, 'utf-8');
                const intelligence = await this.extractPackageJsonIntelligence(fullContent);
                if (intelligence) {
                    format.intelligence = intelligence; // Store extracted data
                    return true;
                }
                return false;
            }
            // 🏎️ F1-OPTIMIZATION: Fast validation with early exit
            switch (format.formatType) {
                case 'requirements.txt':
                    return content.split('\n').some(line => line.trim() && !line.startsWith('#'));
                case 'svelte.config.js':
                    return content.includes('svelte') || content.includes('@sveltejs');
                case 'tsconfig.json':
                    return content.includes('compilerOptions');
                case 'Dockerfile':
                    return content.includes('FROM');
                case '.py':
                    return content.length > 10; // Quick length check
                case '.ts':
                    return content.includes('import') || content.includes('export') || content.includes('interface');
                case '.svelte':
                    return content.includes('<script') || content.includes('<template');
                case '.js':
                    return content.includes('function') || content.includes('const') || content.includes('import');
                default:
                    return content.length > 0;
            }
        }
        catch {
            return false; // File issues = not confirmed
        }
    }
    /**
     * Confirm package.json contains actual dependencies
     */
    confirmPackageJsonUsage(content) {
        try {
            const pkg = JSON.parse(content);
            return !!(pkg.dependencies || pkg.devDependencies || pkg.scripts);
        }
        catch {
            return false;
        }
    }
    /**
     * Confirm requirements.txt has actual Python packages
     */
    confirmRequirementsUsage(content) {
        const lines = content.split('\n').filter(line => line.trim() && !line.startsWith('#'));
        return lines.length > 0;
    }
    /**
     * Analyze discovered formats and generate intelligence
     */
    analyzeFormats(formats) {
        const confirmedFormats = formats.filter(f => f.confirmed);
        const totalIntelligenceScore = confirmedFormats.reduce((sum, f) => sum + f.priority, 0);
        // Calculate framework confidence
        const frameworkConfidence = {};
        confirmedFormats.forEach(format => {
            format.frameworks.forEach(framework => {
                frameworkConfidence[framework] = (frameworkConfidence[framework] || 0) + format.priority;
            });
        });
        // Generate slot fill recommendations
        const slotFillRecommendations = {};
        confirmedFormats.forEach(format => {
            Object.entries(format.slotMappings).forEach(([slot, value]) => {
                if (!slotFillRecommendations[slot] || format.priority > 20) {
                    slotFillRecommendations[slot] = value;
                }
            });
        });
        // 📊 STACKTISTICS: Generate stack signature from detected frameworks
        const stackSignature = this.generateStackSignature(confirmedFormats, frameworkConfidence);
        return {
            discoveredFormats: formats,
            totalIntelligenceScore,
            confirmedFormats,
            frameworkConfidence,
            slotFillRecommendations,
            stackSignature
        };
    }
    /**
     * 📊 STACKTISTICS: Generate stack signature from detected formats
     * Simple, fast, and extends existing fab-formats intelligence
     */
    generateStackSignature(confirmedFormats, frameworkConfidence) {
        // Get top 3 frameworks by confidence (simple approach)
        const topFrameworks = Object.entries(frameworkConfidence)
            .sort(([, a], [, b]) => b - a)
            .slice(0, 3)
            .map(([framework]) => framework.toLowerCase().replace(/[^a-z0-9]/g, ''));
        // Special case mappings for common stacks
        const signature = topFrameworks.join('-');
        // Known stack patterns (simple lookup - no complexity added)
        const knownStacks = {
            'nextjs-tailwind-vercel': 'next-tailwind-vercel',
            'nextjs-tailwindcss-vercel': 'next-tailwind-vercel',
            'svelte-sveltekit-tailwind': 'svelte5-tailwind',
            'react-nextjs-tailwind': 'next-tailwind',
            'python-fastapi-postgresql': 'fastapi-postgres',
            'python-fastapi-sqlite': 'fastapi-sqlite',
            'typescript-nodejs-express': 'node-express-ts'
        };
        return knownStacks[signature] || signature || 'unknown-stack';
    }
    /**
     * Get top framework recommendation
     */
    getTopFramework(analysis) {
        const frameworks = Object.entries(analysis.frameworkConfidence);
        if (frameworks.length === 0) {
            return null;
        }
        const [framework, confidence] = frameworks.reduce((max, current) => current[1] > max[1] ? current : max);
        return { framework, confidence };
    }
    /**
     * Generate human-readable summary
     */
    generateSummary(analysis) {
        const topFramework = this.getTopFramework(analysis);
        const confirmedCount = analysis.confirmedFormats.length;
        const totalScore = analysis.totalIntelligenceScore;
        return `😽 TURBO-CAT Analysis: ${confirmedCount} confirmed formats, ${totalScore} intelligence boost. ` +
            `Top framework: ${topFramework?.framework || 'Unknown'} (${topFramework?.confidence || 0}% confidence)`;
    }
}
exports.TurboCat = TurboCat;
//# sourceMappingURL=turbo-cat.js.map