"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommunityFull = void 0;
const community_prior_1 = require("./community-prior");
class CommunityFull {
    prior;
    crossLanguageMaps = [];
    constructor(cellDir) {
        this.prior = new community_prior_1.CommunityPrior(cellDir);
        this.initCrossLanguageMaps();
    }
    guaranteeAllReal() {
        const synthetic = this.prior.getSyntheticPatterns();
        if (synthetic.length > 0)
            return false;
        const live = this.prior.getLivePatterns();
        return live.length >= 200;
    }
    getCrossLanguageMapping(from, to) {
        return this.crossLanguageMaps.find((m) => m.from === from && m.to === to) || null;
    }
    translatePattern(pattern, from, to) {
        const mapping = this.getCrossLanguageMapping(from, to);
        if (!mapping)
            return null;
        const translatedCategory = mapping.patternMap[pattern.category];
        if (!translatedCategory)
            return null;
        return {
            ...pattern,
            patternId: `xl_${pattern.patternId}`,
            category: translatedCategory,
            description: pattern.description.replace(new RegExp(`\\b${from}\\b`, 'gi'), to),
            confidence: {
                ...pattern.confidence,
                mean: Math.round(pattern.confidence.mean * mapping.confidence * 1000) / 1000,
            },
        };
    }
    aggregateCalibration() {
        const livePatterns = this.prior.getLivePatterns();
        if (livePatterns.length < 100) {
            return { communityA: 0, communityB: 1 };
        }
        const meanConfidences = livePatterns.map((p) => p.confidence.mean);
        const avgConfidence = meanConfidences.reduce((a, b) => a + b, 0) / meanConfidences.length;
        return {
            communityA: Math.round(avgConfidence * 10) / 10,
            communityB: Math.round((1 - avgConfidence) * 10) / 10,
        };
    }
    getDisplayLabel() {
        const liveCount = this.prior.getLivePatterns().length;
        if (liveCount >= 10000) {
            return `Community patterns (learned from ${this.formatLarge(liveCount)}+ developers)`;
        }
        if (liveCount >= 1000) {
            return `Community patterns (learned from ${this.formatLarge(liveCount)} developers)`;
        }
        return this.prior.getDisplayLabel();
    }
    getCommunityStats() {
        const patterns = this.prior.getPatterns();
        const avgConf = patterns.length > 0
            ? Math.round(patterns.reduce((a, p) => a + p.confidence.mean, 0) / patterns.length * 1000) / 1000
            : 0;
        return {
            totalPatterns: patterns.length,
            livePatterns: this.prior.getLivePatterns().length,
            syntheticPatterns: this.prior.getSyntheticPatterns().length,
            avgConfidence: avgConf,
            calibrationPhase: 'local',
            crossLanguageMappings: this.crossLanguageMaps.length,
        };
    }
    getStats() {
        return {
            isAllReal: this.guaranteeAllReal(),
            liveCount: this.prior.getLivePatterns().length,
            totalCount: this.prior.getPatternCount(),
        };
    }
    initCrossLanguageMaps() {
        this.crossLanguageMaps = [
            {
                from: 'typescript',
                to: 'python',
                patternMap: {
                    variable_declaration: 'variable_declaration',
                    function_style: 'function_style',
                    type_annotation: 'type_hints',
                    module_system: 'import_style',
                    error_handling: 'exception_handling',
                    null_safety: 'none_checks',
                },
                confidence: 0.75,
            },
            {
                from: 'typescript',
                to: 'rust',
                patternMap: {
                    variable_declaration: 'mutability',
                    function_style: 'function_style',
                    type_annotation: 'type_system',
                    module_system: 'module_system',
                    error_handling: 'result_type',
                    null_safety: 'option_type',
                },
                confidence: 0.70,
            },
        ];
    }
    formatLarge(n) {
        if (n >= 10000)
            return `${Math.floor(n / 1000)}K`;
        if (n >= 1000)
            return `${(n / 1000).toFixed(1)}K`;
        return String(n);
    }
}
exports.CommunityFull = CommunityFull;
//# sourceMappingURL=community-full.js.map