// apps/api/src/utils/delay.js

class Delayer {
    static async sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }
    
    static async randomDelay(minMs = 1000, maxMs = 3000) {
        const delay = Math.floor(Math.random() * (maxMs - minMs + 1) + minMs);
        await this.sleep(delay);
        return delay;
    }
    
    static async exponentialBackoff(attempt, baseDelay = 1000) {
        const delay = baseDelay * Math.pow(2, attempt);
        await this.sleep(delay);
        return delay;
    }
    
    static async withDelay(fn, minDelay = 1000, maxDelay = 3000) {
        await this.randomDelay(minDelay, maxDelay);
        return await fn();
    }
}

module.exports = Delayer;