"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PatternSearch = void 0;
class PatternSearch {
    search(patterns, query) {
        const startTime = performance.now();
        let filtered = [...patterns];
        if (query.language) {
            const lang = query.language.toLowerCase();
            filtered = filtered.filter((p) => p.meta?.language?.toLowerCase() === lang);
        }
        if (query.category) {
            filtered = filtered.filter((p) => p.category === query.category);
        }
        if (query.minConfidence !== undefined) {
            filtered = filtered.filter((p) => (p.confidence?.mean || 0) >= query.minConfidence / 100);
        }
        if (query.minQuality !== undefined) {
            filtered = filtered.filter((p) => (p.quality?.score || 0) >= query.minQuality);
        }
        if (query.maxRisk) {
            const riskOrder = ['low', 'medium', 'high'];
            const maxIdx = riskOrder.indexOf(query.maxRisk);
            filtered = filtered.filter((p) => riskOrder.indexOf(p.meta?.risk || 'low') <= maxIdx);
        }
        if (query.complexity) {
            filtered = filtered.filter((p) => p.meta?.complexity === query.complexity);
        }
        if (query.minOutcome !== undefined) {
            filtered = filtered.filter((p) => (p.signals?.positive || 0) >= query.minOutcome);
        }
        if (query.text) {
            const terms = query.text.toLowerCase().split(/\s+/);
            filtered = filtered.filter((p) => terms.some((t) => (p.description || '').toLowerCase().includes(t) ||
                (p.category || '').toLowerCase().includes(t) ||
                (p.from || '').toLowerCase().includes(t) ||
                (p.to || '').toLowerCase().includes(t)));
        }
        const total = filtered.length;
        const sortBy = query.sortBy || 'ranking';
        const sortOrder = query.sortOrder || 'desc';
        filtered.sort((a, b) => {
            let valA = 0;
            let valB = 0;
            switch (sortBy) {
                case 'confidence':
                    valA = a.confidence?.mean || 0;
                    valB = b.confidence?.mean || 0;
                    break;
                case 'quality':
                    valA = a.quality?.score || 0;
                    valB = b.quality?.score || 0;
                    break;
                case 'usage':
                    valA = a.signals?.total || 0;
                    valB = b.signals?.total || 0;
                    break;
                case 'recency':
                    valA = a.signals?.lastSignal || 0;
                    valB = b.signals?.lastSignal || 0;
                    break;
                case 'ranking':
                    valA = a.ranking?.score || 0;
                    valB = b.ranking?.score || 0;
                    break;
            }
            return sortOrder === 'desc' ? valB - valA : valA - valB;
        });
        const limit = query.limit || 50;
        const offset = query.offset || 0;
        const paginated = filtered.slice(offset, offset + limit);
        const latencyMs = Math.round((performance.now() - startTime) * 100) / 100;
        return {
            patterns: paginated,
            total,
            query,
            latencyMs,
        };
    }
}
exports.PatternSearch = PatternSearch;
//# sourceMappingURL=pattern-search.js.map