// (C) 2007-2022 GoodData Corporation

const { log } = require('./logger');

function partial(fn, ...others) {
    return (...args) => {
        return fn.apply(null, [...others, ...args]);
    };
}

function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function retryOperation(operation, maxRetries, delayMs) {
    for (let i = 0; i < maxRetries; i += 1) {
        try {
            /* eslint-disable no-await-in-loop */
            return await operation();
        } catch (err) {
            log(`Attempt ${i} failed, retrying after ${delayMs}ms. \n`);
            /* eslint-disable no-await-in-loop */
            await delay(delayMs);
        }
    }
    throw new Error(`Operation failed after ${maxRetries} attempts.`);
}

module.exports = {
    partial,
    retryOperation
};
