// tools/reverse-engineer/analyzer.ts — Code structure analysis utilities
// SPEC-027: Language-specific import patterns for framework terminology detection
export const CODE_EXTENSIONS = new Set([
    '.ts',
    '.tsx',
    '.js',
    '.jsx',
    '.mjs',
    '.cjs',
    '.py',
    '.go',
    '.rs',
    '.java',
    '.kt',
    '.rb',
    '.php',
    '.cs',
    '.swift',
    '.dart',
    '.ex',
    '.exs',
]);
// SPEC-027: Language-specific import patterns for framework terminology detection
const LANGUAGE_STRATEGIES = {
    typescript: {
        importPatterns: [
            /^import\s+.*from\s+['"](.+)['"]/gm,
            /^import\s+type\s+.*from\s+['"](.+)['"]/gm,
        ],
        typePatterns: [/(?:interface|type|enum|class|abstract class)\s+(\w+)/g],
        frameworkTerms: ['Component', 'Hook', 'Provider', 'Context', 'Store', 'Service', 'Controller'],
    },
    javascript: {
        importPatterns: [/^(?:import|require)\s*.*['"](.+)['"]/gm],
        typePatterns: [/(?:class|function)\s+(\w+)/g],
        frameworkTerms: ['Component', 'Hook', 'Model', 'Route', 'Middleware'],
    },
    python: {
        importPatterns: [/^(?:import|from)\s+(\S+)/gm],
        typePatterns: [/^class\s+(\w+)/gm, /^def\s+(\w+)/gm],
        frameworkTerms: ['View', 'Model', 'Serializer', 'Form', 'Task', 'Signal', 'Middleware'],
    },
    go: {
        importPatterns: [/import\s+["'](.+)["']/g],
        typePatterns: [/^type\s+(\w+)\s+(?:struct|interface)/gm],
        frameworkTerms: ['Handler', 'Service', 'Repository', 'Entity', 'Controller'],
    },
    java: {
        importPatterns: [/^import\s+([\w.]+);/gm],
        typePatterns: [/^(?:public\s+)?(?:class|interface|enum|record)\s+(\w+)/gm],
        frameworkTerms: ['Controller', 'Service', 'Repository', 'Entity', 'Component', 'Bean'],
    },
    kotlin: {
        importPatterns: [/^import\s+([\w.]+)/gm],
        typePatterns: [/^(?:class|interface|data class|object)\s+(\w+)/gm],
        frameworkTerms: ['ViewModel', 'Repository', 'Entity', 'Fragment', 'Composable'],
    },
    rust: {
        importPatterns: [/^use\s+([\w:]+)/gm],
        typePatterns: [/^(?:pub\s+)?(?:struct|enum|trait|impl)\s+(\w+)/gm],
        frameworkTerms: ['Handler', 'Service', 'Error', 'State', 'Event'],
    },
};
/**
 * SPEC-027: Detect language from file extension for multi-language analysis strategy.
 */
export function detectLanguageFromExtension(ext) {
    const map = {
        '.ts': 'typescript',
        '.tsx': 'typescript',
        '.mts': 'typescript',
        '.js': 'javascript',
        '.jsx': 'javascript',
        '.mjs': 'javascript',
        '.cjs': 'javascript',
        '.py': 'python',
        '.go': 'go',
        '.rs': 'rust',
        '.java': 'java',
        '.kt': 'kotlin',
        '.kts': 'kotlin',
        '.swift': 'swift',
        '.dart': 'dart',
        '.rb': 'ruby',
        '.php': 'php',
        '.cs': 'csharp',
        '.ex': 'elixir',
        '.exs': 'elixir',
    };
    return map[ext] ?? 'unknown';
}
/**
 * SPEC-027: Get framework terminology for spec draft based on detected language/framework.
 * Returns terminology hints like "Component/Hook" for Next.js, "View/Model" for Django.
 */
export function getFrameworkTerminology(language, dependencies) {
    const depsLower = dependencies.map((d) => d.toLowerCase());
    // Next.js / React
    if (depsLower.some((d) => d.includes('next') || d.includes('react'))) {
        return 'Component / Hook / Provider / Server Action';
    }
    // Django / Flask
    if (depsLower.some((d) => d.includes('django'))) {
        return 'View / Model / Serializer / Form / Middleware';
    }
    if (depsLower.some((d) => d.includes('flask'))) {
        return 'Blueprint / Route / Model / Schema';
    }
    // Spring Boot
    if (depsLower.some((d) => d.includes('spring'))) {
        return 'Controller / Service / Repository / Entity / Bean';
    }
    // NestJS
    if (depsLower.some((d) => d.includes('nestjs') || d.includes('@nestjs'))) {
        return 'Module / Controller / Service / Guard / Interceptor';
    }
    // Laravel
    if (depsLower.some((d) => d.includes('laravel'))) {
        return 'Controller / Model / Request / Resource / Policy';
    }
    // Rails
    if (depsLower.some((d) => d.includes('rails') || d.includes('activerecord'))) {
        return 'Controller / Model / View / Concern / Job';
    }
    // Go frameworks
    if (depsLower.some((d) => d.includes('gin') || d.includes('echo') || d.includes('fiber'))) {
        return 'Handler / Middleware / Service / Repository';
    }
    // Fallback by language
    const strategy = LANGUAGE_STRATEGIES[language];
    return strategy?.frameworkTerms.join(' / ') ?? 'Module / Service / Handler';
}
export function detectTechDebt(files, totalLines, patterns) {
    const debt = [];
    const avgLines = files.length > 0 ? totalLines / files.length : 0;
    if (avgLines > 300) {
        debt.push({
            file: 'multiple files',
            type: 'code-smell',
            description: `Average file length is ${Math.round(avgLines)} lines — consider splitting large files`,
            severity: avgLines > 500 ? 'high' : 'medium',
            effort: 'medium',
            suggestion: 'Split large files into smaller, focused modules (target <300 lines each)',
        });
    }
    if (files.length > 0 && !patterns.includes('layered-architecture')) {
        debt.push({
            file: 'project structure',
            type: 'anti-pattern',
            description: 'No clear layered architecture detected — may mix concerns',
            severity: 'medium',
            effort: 'large',
            suggestion: 'Introduce clear layer separation: presentation, application, domain, infrastructure',
        });
    }
    const testFiles = files.filter((f) => f.includes('.test.') || f.includes('.spec.'));
    const hasTests = testFiles.length > 0;
    if (!hasTests && files.length > 5) {
        debt.push({
            file: 'tests/',
            type: 'missing-tests',
            description: 'No test files detected in the analyzed path',
            severity: 'high',
            effort: 'large',
            suggestion: 'Add unit tests and integration tests to improve confidence in refactoring',
        });
    }
    return debt;
}
export const IGNORE_PATTERNS = [
    'node_modules/**',
    'dist/**',
    'build/**',
    '.git/**',
    'vendor/**',
    '__pycache__/**',
    '.next/**',
    'coverage/**',
];
//# sourceMappingURL=analyzer.js.map