/**
 * INTENT Rate Limiting
 *
 * Per-tenant rate limiting with sliding window algorithm.
 * Uses Redis for distributed rate limiting across instances.
 *
 * Uses atomic Lua script to prevent race conditions under high contention.
 */
import type { Redis } from 'ioredis';
import { type Config } from '../common/config.js';
import type { ID } from '../common/types.js';
/**
 * Dependencies for RateLimiter
 *
 * Use these to inject dependencies for testing or custom configurations.
 * If not provided, defaults to global singletons for backward compatibility.
 */
export interface RateLimiterDependencies {
    /** Redis client instance */
    redis?: Redis;
    /** Application configuration */
    config?: Config;
}
export interface RateLimitConfig {
    /** Maximum requests per window */
    limit: number;
    /** Window size in seconds */
    windowSeconds: number;
}
export interface RateLimitResult {
    /** Whether the request is allowed */
    allowed: boolean;
    /** Current request count in window */
    current: number;
    /** Maximum requests allowed */
    limit: number;
    /** Remaining requests in window */
    remaining: number;
    /** Seconds until window resets */
    resetIn: number;
    /** Retry after seconds (if not allowed) */
    retryAfter?: number;
}
export interface RateLimitHeaders {
    'X-RateLimit-Limit': string;
    'X-RateLimit-Remaining': string;
    'X-RateLimit-Reset': string;
    'Retry-After'?: string;
}
/**
 * Rate Limiter Service
 */
export declare class RateLimiter {
    private redis;
    private config;
    private readonly keyPrefix;
    /**
     * Create a new RateLimiter instance.
     *
     * @param deps - Optional dependencies for dependency injection.
     *               If not provided, uses global singletons (backward compatible).
     *
     * @example
     * // Default usage (backward compatible)
     * const limiter = new RateLimiter();
     *
     * @example
     * // With dependency injection (for testing)
     * const limiter = new RateLimiter({
     *   redis: mockRedis,
     *   config: testConfig,
     * });
     */
    constructor(deps?: RateLimiterDependencies);
    /**
     * Check and consume rate limit for a tenant.
     *
     * Uses an atomic Lua script to prevent race conditions under high contention.
     * The script atomically:
     * 1. Removes expired entries outside the sliding window
     * 2. Counts current entries
     * 3. Only adds the new request if under the limit
     *
     * This eliminates the race condition in the previous optimistic add pattern
     * where multiple concurrent requests could briefly exceed the limit.
     */
    checkLimit(tenantId: ID, intentType?: string | null): Promise<RateLimitResult>;
    /**
     * Get current rate limit status without consuming
     */
    getStatus(tenantId: ID, intentType?: string | null): Promise<RateLimitResult>;
    /**
     * Reset rate limit for a tenant (admin operation)
     */
    reset(tenantId: ID, intentType?: string | null): Promise<void>;
    /**
     * Convert rate limit result to HTTP headers
     */
    toHeaders(result: RateLimitResult): RateLimitHeaders;
    /**
     * Get limit configuration for tenant/intent type.
     *
     * Priority order:
     * 1. Tenant-specific overrides (tenantMaxInFlight)
     * 2. Intent type-specific limits from config (rateLimits.highRisk, etc.)
     * 3. Default rate limit from config (rateLimits.default)
     */
    private getLimitConfig;
    /**
     * Build Redis key for rate limiting
     */
    private buildKey;
}
/**
 * Create rate limiter instance with dependency injection.
 *
 * This is the preferred way to create rate limiters in production code
 * as it makes dependencies explicit and testable.
 *
 * @param deps - Optional dependencies. If not provided, uses global singletons.
 * @returns Configured RateLimiter instance
 *
 * @example
 * // Default usage (backward compatible)
 * const limiter = createRateLimiter();
 *
 * @example
 * // With custom dependencies
 * const limiter = createRateLimiter({
 *   redis: customRedis,
 *   config: customConfig,
 * });
 */
export declare function createRateLimiter(deps?: RateLimiterDependencies): RateLimiter;
/**
 * Fastify rate limit hook
 */
export declare function createRateLimitHook(rateLimiter: RateLimiter): (request: {
    headers: Record<string, string | string[] | undefined>;
    body?: unknown;
}, reply: {
    header: (name: string, value: string) => void;
    status: (code: number) => {
        send: (body: unknown) => unknown;
    };
}) => Promise<unknown>;
//# sourceMappingURL=ratelimit.d.ts.map