/**
 * Rate Limiting Middleware
 *
 * Provides configurable per-tenant and per-endpoint rate limiting with
 * graceful degradation, rate limit headers, and OpenTelemetry tracing.
 *
 * @packageDocumentation
 */
import type { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
/** Type for preHandler hook function */
type PreHandlerFn = (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
/**
 * Rate limit configuration for an endpoint
 */
export interface RateLimitConfig {
    /** Maximum requests allowed in the window */
    limit: number;
    /** Time window in seconds */
    windowSeconds: number;
    /** Optional burst limit (max requests per second) */
    burstLimit?: number;
    /** Skip rate limiting for certain conditions */
    skip?: (request: FastifyRequest) => boolean;
    /** Custom key generator for rate limiting (default: tenantId) */
    keyGenerator?: (request: FastifyRequest) => string | null;
    /** Custom error message */
    errorMessage?: string;
    /** Enable graceful degradation (allow requests when rate limiter fails) */
    gracefulDegradation?: boolean;
}
/**
 * Per-tenant rate limit configuration
 */
export interface TenantRateLimitConfig extends RateLimitConfig {
    /** Per-tenant limit overrides */
    tenantOverrides?: Record<string, Partial<RateLimitConfig>>;
}
/**
 * Rate limit result
 */
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 */
    resetInSeconds: number;
    /** Unix timestamp when window resets */
    resetAt: number;
    /** Retry-After value in seconds (only if not allowed) */
    retryAfter?: number;
}
/**
 * In-memory rate limit store
 *
 * For production use with multiple instances, use Redis-backed store.
 */
declare class InMemoryRateLimitStore {
    private windows;
    private cleanupInterval;
    constructor();
    /**
     * Check and increment rate limit counter
     */
    check(key: string, limit: number, windowSeconds: number): RateLimitResult;
    /**
     * Get current state without incrementing
     */
    peek(key: string, limit: number): RateLimitResult | null;
    /**
     * Reset rate limit for a key
     */
    reset(key: string): void;
    /**
     * Reset all rate limits for a tenant (matches keys starting with tenantId)
     */
    resetTenant(tenantId: string): void;
    /**
     * Cleanup expired entries
     */
    private cleanup;
    /**
     * Get store statistics
     */
    getStats(): {
        activeKeys: number;
        totalRequests: number;
    };
    /**
     * Stop the cleanup timer
     */
    stop(): void;
}
/**
 * Get or create the global rate limit store
 */
export declare function getRateLimitStore(): InMemoryRateLimitStore;
/**
 * Reset the global rate limit store (for testing)
 */
export declare function resetRateLimitStore(): void;
/**
 * Create rate limiting middleware for an endpoint
 *
 * @param config - Rate limit configuration
 * @returns Fastify preHandler hook
 *
 * @example
 * ```typescript
 * // Basic rate limiting
 * server.post('/api/data', {
 *   preHandler: rateLimit({ limit: 100, windowSeconds: 60 }),
 * }, handler);
 *
 * // With per-tenant limits
 * server.post('/api/data', {
 *   preHandler: rateLimitPerTenant({
 *     limit: 100,
 *     windowSeconds: 60,
 *     tenantOverrides: {
 *       'enterprise-tenant': { limit: 1000 },
 *     },
 *   }),
 * }, handler);
 * ```
 */
export declare function rateLimit(config: RateLimitConfig): PreHandlerFn;
/**
 * Create per-tenant rate limiting middleware
 *
 * @param config - Per-tenant rate limit configuration
 * @returns Fastify preHandler hook
 */
export declare function rateLimitPerTenant(config: TenantRateLimitConfig): PreHandlerFn;
/**
 * Create endpoint-specific rate limiter with different limits per method
 *
 * @param configs - Configuration per HTTP method
 * @returns Fastify preHandler hook
 */
export declare function rateLimitByMethod(configs: Partial<Record<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', RateLimitConfig>>): PreHandlerFn;
/**
 * Register rate limiting plugin for Fastify
 *
 * @param server - Fastify instance
 * @param defaultConfig - Default rate limit configuration
 */
export declare function registerRateLimitPlugin(server: FastifyInstance, defaultConfig?: RateLimitConfig): Promise<void>;
/**
 * Get rate limit statistics
 */
export declare function getRateLimitStats(): {
    activeKeys: number;
    totalRequests: number;
};
/**
 * Reset rate limit for a specific tenant
 */
export declare function resetTenantRateLimit(tenantId: string): void;
export {};
//# sourceMappingURL=rateLimit.d.ts.map