"use strict";
/**
 * Org Reputation Aggregator (extension of W3.4 trust-score rollup).
 *
 * Computes the org-level reputation signal that other orgs see when they
 * query the trust API about an agent issued by this org. Maps directly to
 * the credit-agency-for-AI-agents framing (Thomas Layer 0 2026-05-06):
 * each org has a reputation that aggregates the behaviour of every agent
 * they ever deployed.
 *
 * Aggregation rules:
 *   - Numeric: signal-volume-weighted mean across constituent agents
 *     (more-active agents have more weight, single-shot agents are
 *     diluted appropriately).
 *   - Confidence bands match the existing OrgReputation type:
 *       0   signals     -> low
 *       1-9 signals     -> low
 *       10-99 signals   -> medium
 *       100+ signals    -> high
 *
 * Pure math; same input -> same output. Auditor-reproducible.
 *
 * @connexum/ai-governance  Credit-agency org reputation surface
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeOrgReputation = computeOrgReputation;
function confidenceForSignals(n) {
    if (n >= 100)
        return 'high';
    if (n >= 10)
        return 'medium';
    return 'low';
}
function weightedMean(values) {
    const totalWeight = values.reduce((a, v) => a + v.weight, 0);
    if (totalWeight === 0)
        return 0;
    const weightedSum = values.reduce((a, v) => a + v.value * v.weight, 0);
    return Math.round(weightedSum / totalWeight);
}
function computeOrgReputation(input) {
    if (input.agents.length === 0) {
        return {
            org_id: input.org_id,
            score: 0,
            confidence: 'low',
            based_on_signal_count: 0,
            agent_count: 0,
            axis_breakdown: {
                agent_activity: 0,
                operator_activity: 0,
                agent_build_structure: 0,
            },
            computed_at: input.computed_at,
        };
    }
    // Each agent's weight = its signal_count + 1 (so an agent with zero signals
    // still contributes a small amount; prevents reputation gaming via
    // never-activating agents).
    const weightedAgent = input.agents.map((a) => ({
        value: a.score.agent_activity.composite,
        weight: a.signal_count + 1,
    }));
    const weightedOperator = input.agents.map((a) => ({
        value: a.score.operator_activity.composite,
        weight: a.signal_count + 1,
    }));
    const weightedBuild = input.agents.map((a) => ({
        value: a.score.agent_build_structure.composite,
        weight: a.signal_count + 1,
    }));
    const weightedComposite = input.agents.map((a) => ({
        value: a.score.composite,
        weight: a.signal_count + 1,
    }));
    const totalSignals = input.agents.reduce((a, x) => a + x.signal_count, 0);
    return {
        org_id: input.org_id,
        score: weightedMean(weightedComposite),
        confidence: confidenceForSignals(totalSignals),
        based_on_signal_count: totalSignals,
        agent_count: input.agents.length,
        axis_breakdown: {
            agent_activity: weightedMean(weightedAgent),
            operator_activity: weightedMean(weightedOperator),
            agent_build_structure: weightedMean(weightedBuild),
        },
        computed_at: input.computed_at,
    };
}
//# sourceMappingURL=org-reputation.js.map