"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HybridMatcher = void 0;
const exact_matcher_1 = require("./exact.matcher");
const normalized_matcher_1 = require("./normalized.matcher");
const context_aware_matcher_1 = require("./context-aware.matcher");
class HybridMatcher {
    constructor() {
        this.exactMatcher = new exact_matcher_1.ExactMatcher();
        this.normalizedMatcher = new normalized_matcher_1.NormalizedMatcher();
        this.contextAwareMatcher = new context_aware_matcher_1.ContextAwareMatcher();
    }
    findBestMatch(searchBlock, content) {
        const exactResult = this.exactMatcher.match(searchBlock, content);
        if (exactResult.found && exactResult.unique) {
            return exactResult;
        }
        const normalizedResult = this.normalizedMatcher.match(searchBlock, content);
        if (normalizedResult.found && normalizedResult.unique) {
            return normalizedResult;
        }
        const contextResult = this.contextAwareMatcher.match(searchBlock, content);
        if (contextResult.found && contextResult.unique) {
            return contextResult;
        }
        return this.aggregateFailureCandidates(exactResult, normalizedResult, contextResult);
    }
    aggregateFailureCandidates(exactResult, normalizedResult, contextResult) {
        const allCandidates = [
            ...(exactResult.candidates || []),
            ...(normalizedResult.candidates || []),
            ...(contextResult.candidates || []),
        ];
        const deduplicated = this.deduplicateCandidates(allCandidates);
        deduplicated.sort((a, b) => b.confidence - a.confidence);
        const bestConfidence = deduplicated.length > 0 ? deduplicated[0].confidence : 0;
        return {
            found: deduplicated.length > 0,
            unique: false,
            confidence: bestConfidence,
            candidates: deduplicated,
        };
    }
    deduplicateCandidates(candidates) {
        const map = new Map();
        for (const candidate of candidates) {
            const key = `${candidate.startLine}-${candidate.endLine}`;
            const existing = map.get(key);
            if (!existing || candidate.confidence > existing.confidence) {
                map.set(key, candidate);
            }
        }
        return Array.from(map.values());
    }
}
exports.HybridMatcher = HybridMatcher;
//# sourceMappingURL=hybrid.matcher.js.map