"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RetrySolanaRpcFactory = void 0;
var tslib_1 = require("tslib");
var baseRpcFactories_1 = require("./baseRpcFactories");
var rateLimitedRpcFactory_1 = require("./rateLimitedRpcFactory");
var utils_1 = require("../../utils");
var NetworkUtils_1 = require("../../utils/NetworkUtils");
var RetrySolanaRpcFactory = (function (_super) {
    tslib_1.__extends(RetrySolanaRpcFactory, _super);
    function RetrySolanaRpcFactory(retries, retryDelaySeconds) {
        var _this = this;
        var rateLimitedConstructorParams = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            rateLimitedConstructorParams[_i - 2] = arguments[_i];
        }
        var superParams = rateLimitedConstructorParams.slice(-2);
        _this = _super.apply(this, superParams) || this;
        _this.retries = retries;
        _this.retryDelaySeconds = retryDelaySeconds;
        if (_this.retries < 0 || !Number.isInteger(_this.retries)) {
            throw new Error("retries cannot be < 0 and must be an integer. Currently set to ".concat(_this.retries));
        }
        if (_this.retryDelaySeconds < 0) {
            throw new Error("retryDelaySeconds cannot be < 0. Currently set to ".concat(_this.retryDelaySeconds));
        }
        var rateLimitedRpcFactory = new (rateLimitedRpcFactory_1.RateLimitedSolanaRpcFactory.bind.apply(rateLimitedRpcFactory_1.RateLimitedSolanaRpcFactory, tslib_1.__spreadArray([void 0], rateLimitedConstructorParams, false)))();
        _this.rateLimitedTransport = rateLimitedRpcFactory.createTransport();
        _this.logger = rateLimitedRpcFactory.logger;
        return _this;
    }
    RetrySolanaRpcFactory.prototype.createTransport = function () {
        var _this = this;
        return function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            return _this._tryCall(function () { return _this.rateLimitedTransport.apply(_this, args); }, args);
        };
    };
    RetrySolanaRpcFactory.prototype._tryCall = function (transportCall, args) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var method, retries, error_1;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        method = args[0].payload.method;
                        retries = this.retries;
                        _a.label = 1;
                    case 1:
                        if (!true) return [3, 7];
                        _a.label = 2;
                    case 2:
                        _a.trys.push([2, 4, , 6]);
                        return [4, transportCall()];
                    case 3: return [2, _a.sent()];
                    case 4:
                        error_1 = _a.sent();
                        if (retries-- <= 0 || this.shouldFailImmediate(method, error_1)) {
                            throw error_1;
                        }
                        this.logger.debug({
                            at: "RetryRpcFactory",
                            message: "Retrying Solana RPC call",
                            provider: (0, NetworkUtils_1.getOriginFromURL)(this.clusterUrl),
                            method: method,
                            retryAttempt: this.retries - retries,
                            retryDelaySeconds: this.retryDelaySeconds,
                            error: error_1 === null || error_1 === void 0 ? void 0 : error_1.toString(),
                        });
                        return [4, (0, utils_1.delay)(this.retryDelaySeconds)];
                    case 5:
                        _a.sent();
                        return [3, 6];
                    case 6: return [3, 1];
                    case 7: return [2];
                }
            });
        });
    };
    RetrySolanaRpcFactory.prototype.shouldFailImmediate = function (_method, _error) {
        return false;
    };
    return RetrySolanaRpcFactory;
}(baseRpcFactories_1.SolanaClusterRpcFactory));
exports.RetrySolanaRpcFactory = RetrySolanaRpcFactory;
//# sourceMappingURL=retryRpcFactory.js.map