import { NETWORK_PROFILES } from "../core/networkProfiles"

export function createHttpLatency(profileName?: string) {
    if (!profileName) return (_req: any, _res: any, next: any) => next()

    const profile = NETWORK_PROFILES[profileName]
    if (!profile) throw new Error("Unknown network profile")

    return (_req: any, _res: any, next: any) => {
        const delay =
            Math.floor(Math.random() * (profile.maxDelay - profile.minDelay)) +
            profile.minDelay

        setTimeout(next, delay)
    }
}