"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockSolanaRpcFactory = void 0;
var tslib_1 = require("tslib");
var solana_1 = require("../solana");
var MockSolanaRpcFactory = (function (_super) {
    tslib_1.__extends(MockSolanaRpcFactory, _super);
    function MockSolanaRpcFactory() {
        var clusterConstructorParams = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            clusterConstructorParams[_i] = arguments[_i];
        }
        var _this = _super.apply(this, clusterConstructorParams) || this;
        _this.responseTime = 10;
        _this.responses = new Map();
        return _this;
    }
    MockSolanaRpcFactory.prototype.createTransport = function () {
        var _this = this;
        return function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            return _this.createMockRpcTransport().apply(void 0, args);
        };
    };
    MockSolanaRpcFactory.prototype.setResult = function (method, params, result) {
        var requestKey = JSON.stringify({ method: method, params: params });
        this.responses.set(requestKey, { result: result });
    };
    MockSolanaRpcFactory.prototype.setError = function (method, params, error) {
        var requestKey = JSON.stringify({ method: method, params: params });
        this.responses.set(requestKey, { error: error });
    };
    MockSolanaRpcFactory.prototype.setThrow = function (method, params, throwError) {
        var requestKey = JSON.stringify({ method: method, params: params });
        this.responses.set(requestKey, { throwError: throwError });
    };
    MockSolanaRpcFactory.prototype.setResponseTime = function (responseTime) {
        this.responseTime = responseTime;
    };
    MockSolanaRpcFactory.prototype.createMockRpcTransport = function () {
        var _this = this;
        return function (_a) { return tslib_1.__awaiter(_this, [_a], void 0, function (_b) {
            var _c, method, params, requestKey, jsonRpcResponse, requestKeyWithoutParams;
            var _this = this;
            var payload = _b.payload;
            return tslib_1.__generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        _c = payload, method = _c.method, params = _c.params;
                        requestKey = JSON.stringify({ method: method, params: params });
                        jsonRpcResponse = this.responses.get(requestKey);
                        if (jsonRpcResponse === undefined) {
                            requestKeyWithoutParams = JSON.stringify({ method: method, params: [] });
                            jsonRpcResponse = this.responses.get(requestKeyWithoutParams);
                            if (jsonRpcResponse === undefined)
                                jsonRpcResponse = { result: null };
                        }
                        return [4, new Promise(function (resolve) { return setTimeout(resolve, _this.responseTime); })];
                    case 1:
                        _d.sent();
                        if ("throwError" in jsonRpcResponse)
                            throw new Error(jsonRpcResponse.throwError);
                        return [2, jsonRpcResponse];
                }
            });
        }); };
    };
    return MockSolanaRpcFactory;
}(solana_1.SolanaClusterRpcFactory));
exports.MockSolanaRpcFactory = MockSolanaRpcFactory;
//# sourceMappingURL=MockSolanaRpcFactory.js.map