"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlattCalibration = void 0;
class PlattCalibration {
    communityA;
    communityB;
    personalA;
    personalB;
    predictionCount;
    constructor(communityA = 0, communityB = 1) {
        this.communityA = communityA;
        this.communityB = communityB;
        this.personalA = 0;
        this.personalB = 1;
        this.predictionCount = 0;
    }
    calibrate(rawConfidence) {
        if (this.predictionCount < 100) {
            const calibrated = this.plattScale(rawConfidence, this.communityA, this.communityB);
            return Math.round(calibrated * 1000) / 1000;
        }
        if (this.predictionCount < 1000) {
            const weight = Math.min(1.0, (this.predictionCount - 100) / 900);
            const aBlended = (1 - weight) * this.communityA + weight * this.personalA;
            const bBlended = (1 - weight) * this.communityB + weight * this.personalB;
            const calibrated = this.plattScale(rawConfidence, aBlended, bBlended);
            return Math.round(calibrated * 1000) / 1000;
        }
        const calibrated = this.plattScale(rawConfidence, this.personalA, this.personalB);
        return Math.round(calibrated * 1000) / 1000;
    }
    getCalibrationPhase() {
        if (this.predictionCount < 100)
            return 'early';
        if (this.predictionCount < 1000)
            return 'blending';
        return 'calibrated';
    }
    getCalibrationDisplay() {
        const phase = this.getCalibrationPhase();
        switch (phase) {
            case 'early':
                return '(early estimate, calibrating...)';
            case 'blending':
                const pct = Math.round(((this.predictionCount - 100) / 900) * 100);
                return `(calibrating: ${pct}% complete)`;
            case 'calibrated':
                return '';
            default:
                return '';
        }
    }
    getDowngradedTier(tier) {
        if (this.predictionCount >= 100)
            return tier;
        const downgrade = {
            strong: 'moderate',
            moderate: 'weak',
            weak: 'uncertain',
            uncertain: 'uncertain',
        };
        return downgrade[tier] || tier;
    }
    recordPrediction(rawConfidence, wasCorrect) {
        this.predictionCount++;
        if (this.predictionCount >= 100) {
            this.updatePersonalParams(rawConfidence, wasCorrect);
        }
    }
    getPredictionCount() {
        return this.predictionCount;
    }
    setCommunityParams(a, b) {
        this.communityA = a;
        this.communityB = b;
    }
    plattScale(x, a, b) {
        return 1 / (1 + Math.exp(-(a * x + b)));
    }
    updatePersonalParams(rawConfidence, wasCorrect) {
        const target = wasCorrect ? 1 : 0;
        const lr = 0.01;
        const logit = Math.log(Math.max(rawConfidence, 0.001) / Math.max(1 - rawConfidence, 0.001));
        const oldA = this.personalA;
        const oldB = this.personalB;
        this.personalA += lr * (target - this.plattScale(rawConfidence, oldA, oldB)) * logit;
        this.personalB += lr * (target - this.plattScale(rawConfidence, oldA, oldB));
    }
}
exports.PlattCalibration = PlattCalibration;
//# sourceMappingURL=platt-calibration.js.map