// P1: Unchecked cast — HIGH
// Matches: (SomeType) variable but no instanceof check
const UNCHECKED_CAST = /\([A-Z]\w*\)\s+\w+/;
const INSTANCEOF_PATTERN = /\binstanceof\b/;
// P2: Method chain 3+ deep — HIGH
// Matches: obj.method().method().method()
const DEEP_CHAIN = /\w+\.\w+\(\)\.\w+\(\)\.\w+/;
function splitLines(content) {
    return content.split('\n');
}
function hasInstanceofInBlock(content, lineIndex) {
    const lines = splitLines(content);
    const windowStart = Math.max(0, lineIndex - 5);
    const windowEnd = Math.min(lines.length - 1, lineIndex + 2);
    for (let i = windowStart; i <= windowEnd; i++) {
        if (INSTANCEOF_PATTERN.test(lines[i] ?? '')) {
            return true;
        }
    }
    return false;
}
function detectP1(filePath, content) {
    const risks = [];
    const lines = splitLines(content);
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i] ?? '';
        if (UNCHECKED_CAST.test(line) && !hasInstanceofInBlock(content, i)) {
            risks.push({
                file: filePath,
                line: i + 1,
                pattern: 'JAVA_UNCHECKED_CAST',
                severity: 'HIGH',
                description: 'Unchecked type cast without preceding instanceof check. Can throw ClassCastException at runtime.',
                suggestedFix: 'Add instanceof check before casting: if (obj instanceof SomeType s) { ... }',
                autoFixable: false,
            });
        }
    }
    return risks;
}
function detectP2(filePath, content) {
    const risks = [];
    const lines = splitLines(content);
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i] ?? '';
        if (DEEP_CHAIN.test(line)) {
            risks.push({
                file: filePath,
                line: i + 1,
                pattern: 'JAVA_DEEP_METHOD_CHAIN',
                severity: 'HIGH',
                description: 'Method chain 3+ levels deep without null checks. Any intermediate method returning null will cause NullPointerException.',
                suggestedFix: 'Break the chain into intermediate variables with null checks, or use Optional.ofNullable().',
                autoFixable: false,
            });
        }
    }
    return risks;
}
export const javaDetector = {
    language: 'Java',
    frameworks: ['Java', 'PHP', 'Ruby'],
    fileExtensions: ['.java', '.php', '.rb'],
    detect(filePath, content) {
        return [...detectP1(filePath, content), ...detectP2(filePath, content)];
    },
};
//# sourceMappingURL=java-detector.js.map