"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QualityEngine = void 0;
class QualityEngine {
    compute(signals, firstSeen, lastSeen, version) {
        const now = Date.now();
        const ageDays = Math.max((now - firstSeen) / (1000 * 60 * 60 * 24), 1);
        const daysSinceLastSignal = Math.max((now - lastSeen) / (1000 * 60 * 60 * 24), 0);
        const total = Math.max(signals.total, 1);
        const successRate = total > 0 ? signals.positive / total : 0;
        const failureRate = total > 0 ? signals.negative / total : 0;
        const acceptanceRate = total > 0 ? signals.positive / total : 0;
        const reuseRate = Math.min(version / 10, 1);
        const recencyFactor = Math.max(0, 1 - daysSinceLastSignal / 90);
        const ageFactor = Math.min(ageDays / 30, 1);
        const signalFactor = Math.min(total / 50, 1);
        const score = Math.round(successRate * 30 +
            reuseRate * 15 +
            recencyFactor * 25 +
            signalFactor * 15 +
            acceptanceRate * 15);
        return {
            score: Math.min(score, 100),
            reuseRate: Math.round(reuseRate * 1000) / 1000,
            successRate: Math.round(successRate * 1000) / 1000,
            failureRate: Math.round(failureRate * 1000) / 1000,
            acceptanceRate: Math.round(acceptanceRate * 1000) / 1000,
            daysSinceLastSignal: Math.round(daysSinceLastSignal * 10) / 10,
            age: Math.round(ageDays),
        };
    }
    decay(quality, daysSinceLastUpdate) {
        if (daysSinceLastUpdate <= 0)
            return quality;
        const decayFactor = Math.max(0, 1 - daysSinceLastUpdate * 0.01);
        return {
            ...quality,
            score: Math.round(quality.score * decayFactor),
            daysSinceLastSignal: quality.daysSinceLastSignal + daysSinceLastUpdate,
            age: quality.age + daysSinceLastUpdate,
        };
    }
}
exports.QualityEngine = QualityEngine;
//# sourceMappingURL=pattern-quality.js.map