import type { Intent, ControlAction, ID, Timestamp, TrustLevel, TrustScore } from '../common/types.js';
/**
 * Supported constraint types
 */
export type ConstraintType = 'rate-limit' | 'time-window' | 'resource-cap' | 'dependency' | 'custom';
/**
 * Base constraint interface
 */
export interface BaseConstraint {
    /** Unique constraint identifier */
    id: ID;
    /** Constraint name */
    name: string;
    /** Constraint description */
    description?: string;
    /** Constraint type */
    type: ConstraintType;
    /** Whether constraint is enabled */
    enabled: boolean;
    /** Action when constraint is violated */
    violationAction: ControlAction;
    /** Priority for evaluation order */
    priority: number;
    /** Metadata */
    metadata?: Record<string, unknown>;
}
/**
 * Rate limit constraint
 */
export interface RateLimitConstraint extends BaseConstraint {
    type: 'rate-limit';
    /** Rate limit configuration */
    config: {
        /** Maximum requests allowed */
        limit: number;
        /** Window duration in milliseconds */
        windowMs: number;
        /** Key for rate limiting (e.g., 'entityId', 'tenantId', 'action') */
        keyBy: string | string[];
        /** Whether to count successful requests only */
        successOnly?: boolean;
        /** Burst allowance (temporary spike over limit) */
        burstAllowance?: number;
    };
}
/**
 * Time window constraint
 */
export interface TimeWindowConstraint extends BaseConstraint {
    type: 'time-window';
    /** Time window configuration */
    config: {
        /** Allowed start time (HH:MM format or ISO) */
        allowedStart?: string;
        /** Allowed end time (HH:MM format or ISO) */
        allowedEnd?: string;
        /** Allowed days of week (0 = Sunday) */
        allowedDays?: number[];
        /** Timezone */
        timezone?: string;
        /** Blackout periods (deny during these times) */
        blackoutPeriods?: Array<{
            start: string;
            end: string;
            reason?: string;
        }>;
    };
}
/**
 * Resource cap constraint
 */
export interface ResourceCapConstraint extends BaseConstraint {
    type: 'resource-cap';
    /** Resource cap configuration */
    config: {
        /** Resource identifier */
        resourceId: string;
        /** Maximum concurrent usage */
        maxConcurrent?: number;
        /** Maximum total usage */
        maxTotal?: number;
        /** Current usage tracking key */
        usageKey: string;
        /** Reset period in milliseconds (for total) */
        resetPeriodMs?: number;
        /** Resource allocation strategy */
        allocationStrategy?: 'fifo' | 'priority' | 'fair';
    };
}
/**
 * Dependency constraint
 */
export interface DependencyConstraint extends BaseConstraint {
    type: 'dependency';
    /** Dependency configuration */
    config: {
        /** Required intent types that must complete first */
        requiredIntents?: string[];
        /** Required approval from */
        requiredApprovals?: string[];
        /** Maximum age of dependency fulfillment in ms */
        maxAgeMs?: number;
        /** Whether dependencies must be from same entity */
        sameEntity?: boolean;
        /** Dependency relationship */
        relationship: 'all' | 'any';
    };
}
/**
 * Custom constraint
 */
export interface CustomConstraint extends BaseConstraint {
    type: 'custom';
    /** Custom constraint configuration */
    config: {
        /** Expression to evaluate */
        expression: string;
        /** Custom function name */
        evaluatorFn?: string;
        /** Additional parameters */
        params?: Record<string, unknown>;
    };
}
/**
 * Union type for all constraints
 */
export type Constraint = RateLimitConstraint | TimeWindowConstraint | ResourceCapConstraint | DependencyConstraint | CustomConstraint;
/**
 * Composite constraint logic
 */
export type CompositeLogic = 'AND' | 'OR' | 'NOT';
/**
 * Composite constraint for combining multiple constraints
 */
export interface CompositeConstraint {
    /** Unique identifier */
    id: ID;
    /** Constraint name */
    name: string;
    /** Logic to apply */
    logic: CompositeLogic;
    /** Child constraints */
    constraints: (Constraint | CompositeConstraint)[];
    /** Whether enabled */
    enabled: boolean;
    /** Action when composite fails */
    violationAction: ControlAction;
}
/**
 * Context for constraint evaluation
 */
export interface ConstraintEvaluationContext {
    /** The intent being evaluated */
    intent: Intent;
    /** Trust score */
    trustScore: TrustScore;
    /** Trust level */
    trustLevel: TrustLevel;
    /** Additional context */
    context?: Record<string, unknown>;
    /** Environment data */
    environment?: {
        timestamp: Timestamp;
        timezone: string;
    };
}
/**
 * State provider interface for external state lookup
 */
export interface StateProvider {
    /** Get current rate count */
    getRateCount(key: string, windowMs: number): Promise<number>;
    /** Increment rate count */
    incrementRateCount(key: string, windowMs: number): Promise<number>;
    /** Get current resource usage */
    getResourceUsage(resourceId: string): Promise<{
        concurrent: number;
        total: number;
    }>;
    /** Check if dependency is fulfilled */
    checkDependency(entityId: ID, intentTypes: string[], maxAgeMs?: number): Promise<boolean>;
    /** Check if approval exists */
    checkApproval(intentId: ID, approvers: string[]): Promise<boolean>;
}
/**
 * Result of evaluating a constraint
 */
export interface ConstraintEvaluationResult {
    /** Constraint that was evaluated */
    constraintId: ID;
    /** Constraint name */
    constraintName: string;
    /** Whether constraint passed */
    passed: boolean;
    /** Resulting action */
    action: ControlAction;
    /** Reason for result */
    reason: string;
    /** Additional details */
    details: Record<string, unknown>;
    /** Evaluation duration in ms */
    durationMs: number;
    /** Evaluation timestamp */
    evaluatedAt: Timestamp;
}
/**
 * Constraint evaluator options
 */
export interface ConstraintEvaluatorOptions {
    /** Enable OpenTelemetry tracing */
    enableTracing?: boolean;
    /** Default violation action */
    defaultViolationAction?: ControlAction;
    /** Custom evaluator functions */
    customEvaluators?: Map<string, (constraint: CustomConstraint, context: ConstraintEvaluationContext) => Promise<boolean>>;
}
/**
 * In-memory state provider for testing/development
 */
export declare class InMemoryStateProvider implements StateProvider {
    private rateCounts;
    private resourceUsage;
    private dependencies;
    private approvals;
    getRateCount(key: string, windowMs: number): Promise<number>;
    incrementRateCount(key: string, windowMs: number): Promise<number>;
    getResourceUsage(resourceId: string): Promise<{
        concurrent: number;
        total: number;
    }>;
    checkDependency(entityId: ID, intentTypes: string[], maxAgeMs?: number): Promise<boolean>;
    checkApproval(intentId: ID, approvers: string[]): Promise<boolean>;
    setRateCount(key: string, count: number, windowMs: number): void;
    setResourceUsage(resourceId: string, concurrent: number, total: number): void;
    addDependency(entityId: ID, intentType: string): void;
    addApproval(intentId: ID, approver: string): void;
    clear(): void;
}
/**
 * ConstraintEvaluator class for evaluating constraints
 */
export declare class ConstraintEvaluator {
    private constraints;
    private stateProvider;
    private options;
    private customEvaluators;
    constructor(stateProvider?: StateProvider, options?: ConstraintEvaluatorOptions);
    /**
     * Evaluate all applicable constraints
     */
    evaluateAll(context: ConstraintEvaluationContext): Promise<ConstraintEvaluationResult[]>;
    /**
     * Evaluate all constraints with tracing
     */
    evaluateAllWithTracing(context: ConstraintEvaluationContext): Promise<ConstraintEvaluationResult[]>;
    /**
     * Evaluate a single constraint
     */
    evaluate(constraint: Constraint | CompositeConstraint, context: ConstraintEvaluationContext): Promise<ConstraintEvaluationResult>;
    /**
     * Evaluate rate limit constraint
     */
    private evaluateRateLimit;
    /**
     * Evaluate time window constraint
     */
    private evaluateTimeWindow;
    /**
     * Evaluate resource cap constraint
     */
    private evaluateResourceCap;
    /**
     * Evaluate dependency constraint
     */
    private evaluateDependency;
    /**
     * Evaluate custom constraint
     */
    private evaluateCustom;
    /**
     * Evaluate composite constraint
     */
    private evaluateComposite;
    /**
     * Evaluate a simple expression
     */
    private evaluateExpression;
    /**
     * Resolve a field path from context
     */
    private resolveFieldPath;
    /**
     * Resolve a key from context
     */
    private resolveKey;
    /**
     * Parse a value string
     */
    private parseValue;
    /**
     * Parse time string to Date
     */
    private parseTime;
    /**
     * Create evaluation result
     */
    private createResult;
    /**
     * Add a constraint
     */
    addConstraint(constraint: Constraint | CompositeConstraint): void;
    /**
     * Remove a constraint
     */
    removeConstraint(constraintId: ID): boolean;
    /**
     * Get a constraint by ID
     */
    getConstraint(constraintId: ID): Constraint | CompositeConstraint | undefined;
    /**
     * Get all constraints
     */
    getAllConstraints(): Array<Constraint | CompositeConstraint>;
    /**
     * Get enabled constraints
     */
    getEnabledConstraints(): Array<Constraint | CompositeConstraint>;
    /**
     * Register custom evaluator
     */
    registerCustomEvaluator(name: string, evaluator: (constraint: CustomConstraint, context: ConstraintEvaluationContext) => Promise<boolean>): void;
    /**
     * Set state provider
     */
    setStateProvider(provider: StateProvider): void;
    /**
     * Clear all constraints
     */
    clear(): void;
    /**
     * Get evaluator statistics
     */
    getStats(): {
        totalConstraints: number;
        enabledConstraints: number;
        byType: Record<string, number>;
    };
}
/**
 * Create a new constraint evaluator instance
 */
export declare function createConstraintEvaluator(stateProvider?: StateProvider, options?: ConstraintEvaluatorOptions): ConstraintEvaluator;
/**
 * Create a rate limit constraint
 */
export declare function createRateLimitConstraint(id: ID, name: string, limit: number, windowMs: number, keyBy: string | string[], options?: Partial<RateLimitConstraint>): RateLimitConstraint;
/**
 * Create a time window constraint
 */
export declare function createTimeWindowConstraint(id: ID, name: string, options?: Partial<TimeWindowConstraint>): TimeWindowConstraint;
/**
 * Create a resource cap constraint
 */
export declare function createResourceCapConstraint(id: ID, name: string, resourceId: string, options?: Partial<ResourceCapConstraint>): ResourceCapConstraint;
/**
 * Create a dependency constraint
 */
export declare function createDependencyConstraint(id: ID, name: string, options?: Partial<DependencyConstraint>): DependencyConstraint;
/**
 * Create a composite constraint
 */
export declare function createCompositeConstraint(id: ID, name: string, logic: CompositeLogic, constraints: (Constraint | CompositeConstraint)[], options?: Partial<CompositeConstraint>): CompositeConstraint;
//# sourceMappingURL=constraint-evaluator.d.ts.map