"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PatternUtils = void 0;
const crypto_1 = require("crypto");
class PatternUtils {
    indexCache = new Map();
    obfuscateProjectName(name) {
        return (0, crypto_1.createHash)('sha256').update(name).digest('hex').substring(0, 12);
    }
    createObfuscatedIndex(projects) {
        return projects.map((p) => ({
            hash: this.obfuscateProjectName(p.name),
            patternCount: p.patterns.length,
            lastActive: new Date().toISOString(),
        }));
    }
    paginate(items, page, pageSize) {
        const total = items.length;
        const totalPages = Math.ceil(total / pageSize);
        const safePage = Math.max(1, Math.min(page, totalPages));
        const start = (safePage - 1) * pageSize;
        const paged = items.slice(start, start + pageSize);
        return {
            items: paged,
            total,
            page: safePage,
            pageSize,
            totalPages,
        };
    }
    buildIndex(patterns) {
        this.indexCache.clear();
        for (const pattern of patterns) {
            const key = pattern.category;
            if (!this.indexCache.has(key)) {
                this.indexCache.set(key, []);
            }
            this.indexCache.get(key).push(pattern);
        }
    }
    queryByCategory(category) {
        return this.indexCache.get(category) || [];
    }
    queryByConfidence(minConfidence) {
        const results = [];
        for (const patterns of this.indexCache.values()) {
            for (const p of patterns) {
                if (p.confidence.mean >= minConfidence) {
                    results.push(p);
                }
            }
        }
        return results;
    }
    getIndexStats() {
        let totalIndexed = 0;
        for (const patterns of this.indexCache.values()) {
            totalIndexed += patterns.length;
        }
        return {
            categories: this.indexCache.size,
            totalIndexed,
        };
    }
}
exports.PatternUtils = PatternUtils;
//# sourceMappingURL=pattern-utils.js.map