"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const backoff_1 = (0, tslib_1.__importDefault)(require("./backoff"));
const sleep_1 = require("./sleep");
async function retry(run, { delay: backoffDelay = 1000, attempts = 15, onRetry, shouldRetry, } = {}) {
    for (let attempt = 1; attempt < attempts; attempt++) {
        try {
            return await run();
        }
        catch (error) {
            if (!shouldRetry || shouldRetry(error)) {
                const delay = (0, backoff_1.default)(attempt, backoffDelay);
                onRetry === null || onRetry === void 0 ? void 0 : onRetry({ attempt, delay, error });
                await (0, sleep_1.sleep)(delay);
            }
            else {
                throw error;
            }
        }
    }
    return run();
}
exports.default = retry;
//# sourceMappingURL=retry.js.map