export function communicationRoleForTemplate(template) {
    switch (template) {
        case 'engineer':
            return 'engineer';
        case 'reviewer':
            return 'reviewer';
        case 'researcher':
            return 'researcher';
        case 'general':
            return 'general';
        default:
            return 'general';
    }
}
function isParentChild(from, to) {
    return from.parentAgentId === to.agentId || to.parentAgentId === from.agentId;
}
function sharesCohort(from, to) {
    return !!from.cohort && from.cohort === to.cohort;
}
function sharesWrfc(from, to) {
    return !!from.wrfcId && from.wrfcId === to.wrfcId;
}
export function evaluateCommunicationRoute(input) {
    const { from, to, kind, scope } = input;
    if (scope === 'broadcast' && from.role !== 'orchestrator' && from.role !== 'planner') {
        return { allowed: false, reason: 'broadcast is reserved for coordinators' };
    }
    if (from.role === 'orchestrator' || from.role === 'planner') {
        return { allowed: true };
    }
    if (to.role === 'orchestrator') {
        if (['status', 'question', 'finding', 'handoff', 'escalation', 'completion'].includes(kind)) {
            return { allowed: true };
        }
        return { allowed: false, reason: 'agents may only report structured updates upward to the orchestrator' };
    }
    if (isParentChild(from, to)) {
        if (['directive', 'status', 'question', 'finding', 'handoff', 'completion', 'escalation'].includes(kind)) {
            return { allowed: true };
        }
    }
    if (sharesWrfc(from, to)) {
        if ((from.role === 'reviewer' && ['review', 'finding', 'directive'].includes(kind)) ||
            (from.role === 'fixer' && ['status', 'question', 'handoff', 'completion'].includes(kind)) ||
            (from.role === 'engineer' && ['status', 'question', 'handoff', 'completion'].includes(kind)) ||
            (from.role === 'verifier' && ['finding', 'escalation', 'completion'].includes(kind))) {
            return { allowed: true };
        }
    }
    if (sharesCohort(from, to) && ['status', 'question', 'finding', 'handoff'].includes(kind)) {
        return { allowed: true };
    }
    return { allowed: false, reason: 'communication route is outside the allowed role and topology policy' };
}
