/**
 * Governance Policy Types and Management
 *
 * Defines policy structures for governance engine evaluation.
 *
 * @packageDocumentation
 */
// =============================================================================
// RULE OPERATORS
// =============================================================================
/**
 * Comparison operators for rule evaluation
 */
export const RULE_OPERATORS = [
    'eq', // equals
    'ne', // not equals
    'gt', // greater than
    'lt', // less than
    'gte', // greater than or equal
    'lte', // less than or equal
    'in', // value in array
    'contains', // string contains
    'matches', // regex match
];
/**
 * Resolve policy conflicts
 */
export function resolveConflicts(effects, strategy) {
    if (effects.length === 0) {
        return 'allow'; // Default to allow if no policies
    }
    switch (strategy) {
        case 'deny-overrides':
            // Any deny wins
            return effects.some(e => e.effect === 'deny') ? 'deny' : 'allow';
        case 'allow-overrides':
            // Any allow wins
            return effects.some(e => e.effect === 'allow') ? 'allow' : 'deny';
        case 'first-match':
            // First effect in the list
            return effects[0].effect;
        case 'priority-based':
            // Sort by priority (lower = higher priority) and take first
            const sorted = [...effects].sort((a, b) => a.priority - b.priority);
            return sorted[0].effect;
        default:
            return 'deny'; // Default to deny for safety
    }
}
// =============================================================================
// POLICY MANAGEMENT
// =============================================================================
/**
 * PolicySet manager for organizing and querying policies
 */
export class PolicySetManager {
    policySets = new Map();
    /**
     * Add a policy set
     */
    addPolicySet(policySet) {
        this.policySets.set(policySet.id, policySet);
    }
    /**
     * Remove a policy set
     */
    removePolicySet(id) {
        return this.policySets.delete(id);
    }
    /**
     * Get a policy set by ID
     */
    getPolicySet(id) {
        return this.policySets.get(id);
    }
    /**
     * Get all policy sets
     */
    getAllPolicySets() {
        return Array.from(this.policySets.values());
    }
    /**
     * Get all enabled policies from all sets, sorted by priority
     */
    getAllEnabledPolicies() {
        const allPolicies = [];
        for (const policySet of this.policySets.values()) {
            for (const policy of policySet.policies) {
                if (policy.enabled) {
                    allPolicies.push(policy);
                }
            }
        }
        return allPolicies.sort((a, b) => a.priority - b.priority);
    }
    /**
     * Find policies matching conditions
     */
    findMatchingPolicies(action, resource, intentType) {
        const policies = this.getAllEnabledPolicies();
        return policies.filter(policy => {
            if (!policy.conditions)
                return true;
            // Check action match
            if (policy.conditions.actions && action) {
                const matches = policy.conditions.actions.some(a => a === '*' || a === action || (a.endsWith('*') && action.startsWith(a.slice(0, -1))));
                if (!matches)
                    return false;
            }
            // Check resource match
            if (policy.conditions.resources && resource) {
                const matches = policy.conditions.resources.some(r => r === '*' || r === resource || (r.endsWith('*') && resource.startsWith(r.slice(0, -1))));
                if (!matches)
                    return false;
            }
            // Check intent type match
            if (policy.conditions.intentTypes && intentType) {
                const matches = policy.conditions.intentTypes.some(t => t === '*' || t === intentType || (t.endsWith('*') && intentType.startsWith(t.slice(0, -1))));
                if (!matches)
                    return false;
            }
            return true;
        });
    }
    /**
     * Clear all policy sets
     */
    clear() {
        this.policySets.clear();
    }
}
/**
 * Create a new policy with defaults
 */
export function createPolicy(partial) {
    return {
        priority: 100,
        enabled: true,
        ...partial,
    };
}
/**
 * Create a new policy set with defaults
 */
export function createPolicySet(partial) {
    return {
        defaultEffect: 'deny',
        evaluationStrategy: 'deny-overrides',
        ...partial,
    };
}
//# sourceMappingURL=policy.js.map