"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildFailurePredictor = void 0;
const typescript_1 = __importDefault(require("typescript"));
class BuildFailurePredictor {
    predictTypeScript(code) {
        const predictions = [];
        const astPredictions = this.analyzeTypeScriptAst(code);
        predictions.push(...astPredictions);
        const lines = code.split('\n');
        for (let i = 0; i < lines.length; i++) {
            const lineText = lines[i];
            const lineNum = i + 1;
            const alreadyReported = astPredictions.some((p) => p.line === lineNum);
            if (!alreadyReported) {
                if (/\b(\w+)\s*\.\s*(\w+)\s*\(/.test(lineText) && !/:\s*\w+/.test(lineText) && /\bfunction\b/.test(lineText)) {
                    predictions.push({
                        type: 'ts_error',
                        line: lineNum,
                        message: 'Missing return type annotation',
                        confidence: 0.78,
                        autoFixable: false,
                    });
                }
            }
        }
        return predictions;
    }
    predictESLint(code) {
        const predictions = [];
        const astPredictions = this.analyzeESLintAst(code);
        predictions.push(...astPredictions);
        const lines = code.split('\n');
        for (let i = 0; i < lines.length; i++) {
            const lineText = lines[i];
            const lineNum = i + 1;
            const alreadyReported = astPredictions.some((p) => p.line === lineNum);
            if (!alreadyReported) {
                if (/==(?!=)/.test(lineText) && !/===/.test(lineText)) {
                    predictions.push({
                        type: 'eslint_violation',
                        line: lineNum,
                        message: 'eqeqeq: Use === instead of ==',
                        confidence: 0.94,
                        autoFixable: true,
                        fix: lineText.replace(/==(?!=)/g, '==='),
                    });
                }
                if (/console\.(log|warn|error)/.test(lineText)) {
                    predictions.push({
                        type: 'eslint_violation',
                        line: lineNum,
                        message: 'no-console: Unexpected console statement',
                        confidence: 0.88,
                        autoFixable: true,
                        fix: `// TODO: Replace with proper logger\n${lineText}`,
                    });
                }
            }
        }
        return predictions;
    }
    predictTestFailures(code) {
        const predictions = [];
        const lines = code.split('\n');
        for (let i = 0; i < lines.length; i++) {
            if (/\bfunction\s+\w+/.test(lines[i]) && !/test|spec|describe|it\(/.test(lines[i])) {
                predictions.push({
                    type: 'test_failure',
                    line: i + 1,
                    message: 'New function without corresponding test',
                    confidence: 0.76,
                    autoFixable: false,
                });
            }
        }
        return predictions;
    }
    predictAll(code) {
        return {
            tsErrors: this.predictTypeScript(code),
            eslintViolations: this.predictESLint(code),
            testFailures: this.predictTestFailures(code),
        };
    }
    getAutoFixable(predictions) {
        return predictions.filter((p) => p.autoFixable);
    }
    getStats(predictions) {
        const byType = {};
        for (const p of predictions) {
            byType[p.type] = (byType[p.type] || 0) + 1;
        }
        return {
            total: predictions.length,
            autoFixable: predictions.filter((p) => p.autoFixable).length,
            byType,
        };
    }
    analyzeTypeScriptAst(code) {
        const results = [];
        let sourceFile;
        try {
            sourceFile = typescript_1.default.createSourceFile('analyze.tsx', code, typescript_1.default.ScriptTarget.Latest, true, typescript_1.default.ScriptKind.TSX);
        }
        catch {
            return results;
        }
        this.visitForTsErrors(sourceFile, code, results);
        return results;
    }
    visitForTsErrors(node, code, results) {
        if (typescript_1.default.isTypeNode(node) && node.kind === typescript_1.default.SyntaxKind.AnyKeyword) {
            const line = typescript_1.default.getLineAndCharacterOfPosition(node.getSourceFile(), node.getStart()).line + 1;
            if (!results.some((r) => r.line === line && r.message.includes('any'))) {
                results.push({
                    type: 'ts_error',
                    line,
                    message: 'Implicit any type detected',
                    confidence: 0.92,
                    autoFixable: false,
                });
            }
        }
        if (typescript_1.default.isVariableDeclaration(node) && node.type && node.initializer) {
            const declaredType = node.type.getText();
            const initText = node.initializer.getText();
            if (declaredType && !isNaN(Number(initText)) && declaredType !== 'number') {
                const line = typescript_1.default.getLineAndCharacterOfPosition(node.getSourceFile(), node.getStart()).line + 1;
                if (!results.some((r) => r.line === line && r.message.includes('Type'))) {
                    results.push({
                        type: 'ts_error',
                        line,
                        message: `Type '${declaredType}' may not match assigned value`,
                        confidence: 0.85,
                        autoFixable: true,
                        fix: `const ${node.name.getText()}: ${declaredType} = ${declaredType}(${initText})`,
                    });
                }
            }
        }
        typescript_1.default.forEachChild(node, (child) => this.visitForTsErrors(child, code, results));
    }
    analyzeESLintAst(code) {
        const results = [];
        let sourceFile;
        try {
            sourceFile = typescript_1.default.createSourceFile('analyze.tsx', code, typescript_1.default.ScriptTarget.Latest, true, typescript_1.default.ScriptKind.TSX);
        }
        catch {
            return results;
        }
        this.visitForESLint(sourceFile, code, results);
        return results;
    }
    visitForESLint(node, code, results) {
        if (typescript_1.default.isVariableDeclarationList(node) && node.getFirstToken()?.kind === typescript_1.default.SyntaxKind.VarKeyword) {
            const line = typescript_1.default.getLineAndCharacterOfPosition(node.getSourceFile(), node.getStart()).line + 1;
            if (!results.some((r) => r.line === line && r.message.includes('var'))) {
                results.push({
                    type: 'eslint_violation',
                    line,
                    message: 'no-var: Use const or let instead of var',
                    confidence: 0.96,
                    autoFixable: true,
                    fix: `const ${code.substring(node.getStart() + 4, node.getEnd())}`,
                });
            }
        }
        typescript_1.default.forEachChild(node, (child) => this.visitForESLint(child, code, results));
    }
}
exports.BuildFailurePredictor = BuildFailurePredictor;
//# sourceMappingURL=build-failure-predictor.js.map