"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiffPatternMatcher = void 0;
class DiffPatternMatcher {
    extract(originalCode, editedCode) {
        const results = [];
        results.push(...this.detectTypeAnnotationAddition(originalCode, editedCode));
        results.push(...this.detectErrorHandlingWrapping(originalCode, editedCode));
        results.push(...this.detectImportReorganization(originalCode, editedCode));
        results.push(...this.detectFunctionReordering(originalCode, editedCode));
        return results;
    }
    detectTypeAnnotationAddition(original, edited) {
        const results = [];
        const typeAnnotations = [': string', ': number', ': boolean', ': void', ': Promise<', ': Response', ': Request'];
        let addedCount = 0;
        for (const annotation of typeAnnotations) {
            const inOriginal = new RegExp(`\\b${annotation.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`).test(original);
            const inEdited = new RegExp(`\\b${annotation.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`).test(edited);
            if (!inOriginal && inEdited) {
                addedCount++;
            }
        }
        if (addedCount > 0) {
            results.push({
                category: 'type_annotation',
                description: 'Adds TypeScript type annotations',
                from: 'no_types',
                to: 'typed',
                confidenceDelta: Math.min(0.12, addedCount * 0.03),
                conditions: [],
            });
        }
        return results;
    }
    detectErrorHandlingWrapping(original, edited) {
        const results = [];
        const hasTryCatchInOriginal = original.includes('try {') || original.includes('try{');
        const hasTryCatchInEdited = edited.includes('try {') || edited.includes('try{');
        if (!hasTryCatchInOriginal && hasTryCatchInEdited) {
            results.push({
                category: 'error_handling',
                description: 'Wraps code in try/catch for error handling',
                from: 'no_error_handling',
                to: 'try_catch',
                confidenceDelta: 0.1,
                conditions: [],
            });
        }
        const originalLines = original.split('\n');
        const editedLines = edited.split('\n');
        const hasNullCheck = (lines) => lines.some(line => line.includes('if (') && line.includes('null'));
        const nullCheckAdded = !hasNullCheck(originalLines) && hasNullCheck(editedLines);
        if (nullCheckAdded) {
            results.push({
                category: 'null_safety',
                description: 'Adds null checks before accessing values',
                from: 'no_null_check',
                to: 'null_check',
                confidenceDelta: 0.08,
                conditions: [],
            });
        }
        return results;
    }
    detectImportReorganization(original, edited) {
        const results = [];
        const origImportLines = original.split('\n').filter((l) => l.trim().startsWith('import'));
        const editImportLines = edited.split('\n').filter((l) => l.trim().startsWith('import'));
        if (origImportLines.length !== editImportLines.length ||
            !origImportLines.every((l, i) => l.trim() === editImportLines[i]?.trim())) {
            results.push({
                category: 'import_organization',
                description: 'Reorganizes import statements',
                from: 'scattered_imports',
                to: 'organized_imports',
                confidenceDelta: 0.05,
                conditions: [],
            });
        }
        return results;
    }
    detectFunctionReordering(original, edited) {
        const results = [];
        const origFunctions = this.extractFunctionNames(original);
        const editFunctions = this.extractFunctionNames(edited);
        if (origFunctions.length > 0 && editFunctions.length > 0) {
            const orderChanged = !origFunctions.every((name, i) => name === editFunctions[i]);
            if (orderChanged && origFunctions.length === editFunctions.length) {
                results.push({
                    category: 'function_ordering',
                    description: 'Reorders functions (hoisting preference)',
                    from: 'original_order',
                    to: 'reordered',
                    confidenceDelta: 0.06,
                    conditions: [],
                });
            }
        }
        return results;
    }
    extractFunctionNames(code) {
        const functionRegex = /(?:function|const)\s+(\w+)/g;
        const names = [];
        let match;
        while ((match = functionRegex.exec(code)) !== null) {
            names.push(match[1]);
        }
        return names;
    }
}
exports.DiffPatternMatcher = DiffPatternMatcher;
//# sourceMappingURL=ast-extractor.js.map