/**
 * Per-Sender Policies & Agents List
 * Tool access control per sender identity and agent registry.
 */
import { logger } from '../utils/logger.js';
export class SenderPolicyManager {
    static instance = null;
    policies = [];
    static getInstance() {
        if (!SenderPolicyManager.instance) {
            SenderPolicyManager.instance = new SenderPolicyManager();
        }
        return SenderPolicyManager.instance;
    }
    static resetInstance() {
        SenderPolicyManager.instance = null;
    }
    addPolicy(policy) {
        // Remove existing policy for same identity
        this.policies = this.policies.filter(p => !this.matchIdentity(policy.identity, p));
        this.policies.push(policy);
        logger.debug(`Added policy for sender: ${JSON.stringify(policy.identity)}`);
    }
    removePolicy(identity) {
        const before = this.policies.length;
        this.policies = this.policies.filter(p => !this.matchIdentity(identity, p));
        return this.policies.length < before;
    }
    getPolicy(identity) {
        return this.policies.find(p => this.matchIdentity(identity, p));
    }
    isToolAllowed(identity, tool) {
        const policy = this.getPolicy(identity);
        if (!policy)
            return true; // No policy means no restrictions
        if (policy.deniedTools.includes(tool))
            return false;
        if (policy.allowedTools.length > 0 && !policy.allowedTools.includes(tool))
            return false;
        return true;
    }
    matchIdentity(sender, policy) {
        const pi = policy.identity;
        if (sender.username && pi.username && sender.username === pi.username)
            return true;
        if (sender.userId && pi.userId && sender.userId === pi.userId)
            return true;
        if (sender.phone && pi.phone && sender.phone === pi.phone)
            return true;
        if (sender.displayName && pi.displayName && sender.displayName === pi.displayName)
            return true;
        return false;
    }
    listPolicies() {
        return [...this.policies];
    }
    clearPolicies() {
        this.policies = [];
    }
}
export class AgentListTool {
    static instance = null;
    agents = new Map();
    static getInstance() {
        if (!AgentListTool.instance) {
            AgentListTool.instance = new AgentListTool();
        }
        return AgentListTool.instance;
    }
    static resetInstance() {
        AgentListTool.instance = null;
    }
    listAgents() {
        return Array.from(this.agents.values());
    }
    addAgent(name, description) {
        this.agents.set(name, { name, description, status: 'active' });
        logger.debug(`Registered agent: ${name}`);
    }
    removeAgent(name) {
        return this.agents.delete(name);
    }
    getAgent(name) {
        return this.agents.get(name);
    }
    getAgentCount() {
        return this.agents.size;
    }
}
//# sourceMappingURL=sender-policies.js.map