"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MergeConflictPredictor = void 0;
class MergeConflictPredictor {
    predictConflicts(branchADiff, branchBDiff, userPreferences) {
        const predictions = [];
        const aLines = branchADiff.split('\n');
        const bLines = branchBDiff.split('\n');
        const aEdited = this.extractEditedLines(aLines);
        const bEdited = this.extractEditedLines(bLines);
        for (const aEdit of aEdited) {
            for (const bEdit of bEdited) {
                if (aEdit.file === bEdit.file) {
                    if (Math.abs(aEdit.line - bEdit.line) <= 1) {
                        predictions.push({
                            file: aEdit.file,
                            line: Math.min(aEdit.line, bEdit.line),
                            type: aEdit.line === bEdit.line ? 'same_line_edit' : 'adjacent_edit',
                            confidence: aEdit.line === bEdit.line ? 0.9 : 0.7,
                            suggestion: this.generateSuggestion(aEdit, bEdit, userPreferences),
                        });
                    }
                    if (aEdit.content.includes('import') && bEdit.content.includes('import') && aEdit.line === bEdit.line) {
                        predictions.push({
                            file: aEdit.file,
                            line: aEdit.line,
                            type: 'import_conflict',
                            confidence: 0.85,
                            suggestion: 'Merge imports: combine both import statements',
                        });
                    }
                    if ((aEdit.content.includes('function') || aEdit.content.includes('=>')) &&
                        (bEdit.content.includes('function') || bEdit.content.includes('=>'))) {
                        predictions.push({
                            file: aEdit.file,
                            line: aEdit.line,
                            type: 'signature_change',
                            confidence: 0.75,
                            suggestion: 'Both branches modified function signature. Manual review required.',
                        });
                    }
                }
            }
        }
        return predictions;
    }
    getUserPreferenceConfidence(predictions, userPreferences, observationCount) {
        if (observationCount < 8) {
            return predictions.map((p) => ({ ...p, confidence: p.confidence * 0.7 }));
        }
        return predictions.map((p) => ({
            ...p,
            suggestion: userPreferences.preferBranchA
                ? `${p.suggestion} (Based on your preference for Branch A)`
                : `${p.suggestion} (Based on your preference for Branch B)`,
        }));
    }
    extractEditedLines(lines) {
        const edits = [];
        let currentFile = '';
        let currentLineNum = 0;
        for (const line of lines) {
            const fileMatch = line.match(/^\+\+\+\s+b\/(.+)/);
            if (fileMatch) {
                currentFile = fileMatch[1];
                continue;
            }
            const addMatch = line.match(/^@@\s+-(\d+),?\d*\s+\+(\d+),?\d*\s+@@/);
            if (addMatch) {
                currentLineNum = parseInt(addMatch[2], 10);
                continue;
            }
            if (line.startsWith('+') && !line.startsWith('+++')) {
                edits.push({
                    file: currentFile,
                    line: currentLineNum,
                    content: line.substring(1),
                });
                currentLineNum++;
            }
            else if (!line.startsWith('-') && !line.startsWith('@@')) {
                currentLineNum++;
            }
        }
        return edits;
    }
    generateSuggestion(aEdit, bEdit, userPreferences) {
        if (userPreferences?.preferBranchA) {
            return `Keep Branch A change: "${aEdit.content.trim()}"`;
        }
        if (userPreferences?.preferBranchA === false) {
            return `Keep Branch B change: "${bEdit.content.trim()}"`;
        }
        return `Manual merge needed at ${aEdit.file}:${aEdit.line}`;
    }
}
exports.MergeConflictPredictor = MergeConflictPredictor;
//# sourceMappingURL=merge-conflict-predictor.js.map