"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeadCodeDetector = void 0;
class DeadCodeDetector {
    constructor() { }
    detectUnusedFunctions(code, file) {
        const reports = [];
        const lines = code.split('\n');
        for (let i = 0; i < lines.length; i++) {
            const line = lines[i];
            const funcMatch = line.match(/(?:export\s+)?(?:function|const)\s+(\w+)/);
            if (funcMatch) {
                const name = funcMatch[1];
                const isExported = line.includes('export');
                const isUsedElsewhere = this.checkUsage(name, code, line);
                if (!isExported && !isUsedElsewhere) {
                    reports.push({
                        type: 'unused_function',
                        name,
                        file,
                        line: i + 1,
                        suggestion: `Remove unused function '${name}' or export it if needed`,
                    });
                }
            }
        }
        return reports;
    }
    detectUnusedImports(code, file) {
        const reports = [];
        const lines = code.split('\n');
        for (let i = 0; i < lines.length; i++) {
            const line = lines[i];
            const importMatch = line.match(/import\s+\{?\s*(\w+)/);
            if (importMatch && !line.includes('import type')) {
                const name = importMatch[1];
                const namedImports = line.match(/\{\s*([^}]+)\s*\}/);
                const names = namedImports ? namedImports[1].split(',').map(s => s.trim().split(' as ')[0].trim()) : [name];
                const restOfFile = lines.slice(i + 1).join('\n');
                for (const n of names) {
                    if (!restOfFile.includes(n)) {
                        reports.push({
                            type: 'unused_import',
                            name: n,
                            file,
                            line: i + 1,
                            suggestion: `Remove unused import '${n}'`,
                        });
                    }
                }
            }
        }
        return reports;
    }
    detectUnusedDependencies(dependencies, usedImports) {
        const reports = [];
        for (const [dep] of Object.entries(dependencies)) {
            const isUsed = Array.from(usedImports).some((imp) => imp === dep || imp.startsWith(dep + '/'));
            if (!isUsed) {
                reports.push({
                    type: 'unused_dependency',
                    name: dep,
                    file: 'package.json',
                    line: 0,
                    suggestion: `Remove unused dependency '${dep}'`,
                });
            }
        }
        return reports;
    }
    detectAll(code, file, dependencies) {
        const reports = [
            ...this.detectUnusedFunctions(code, file),
            ...this.detectUnusedImports(code, file),
        ];
        if (dependencies) {
            const usedImports = new Set();
            const lines = code.split('\n');
            for (const line of lines) {
                const match = line.match(/import\s+.*from\s+['"]([^'"]+)['"]/);
                if (match)
                    usedImports.add(match[1]);
            }
            reports.push(...this.detectUnusedDependencies(dependencies, usedImports));
        }
        return reports;
    }
    getCleanAllAction(reports) {
        return `Remove ${reports.length} dead code items: ${reports.map((r) => r.name).join(', ')}`;
    }
    getShowListAction(reports) {
        return reports
            .map((r) => `[${r.type}] ${r.file}:${r.line} — ${r.name}: ${r.suggestion}`)
            .join('\n');
    }
    getKeepAction(report) {
        return `Keep '${report.name}' — mark as intentionally unused`;
    }
    checkUsage(name, code, excludeLine) {
        const usagePattern = new RegExp(`\\b${name}\\b`);
        const linesWithoutDef = code.split('\n').filter((l) => l !== excludeLine);
        return linesWithoutDef.some((l) => usagePattern.test(l) && !l.includes(`function ${name}`) && !l.includes(`const ${name}`));
    }
}
exports.DeadCodeDetector = DeadCodeDetector;
//# sourceMappingURL=dead-code-detector.js.map