import { __awaiter, __extends, __generator } from "tslib";
import { SolanaClusterRpcFactory } from "../solana";
// Exposes mocked RPC transport for Solana in the SolanaClusterRpcFactory class.
var MockSolanaRpcFactory = /** @class */ (function (_super) {
    __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; // in milliseconds
        _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) {
            var payload = _a.payload;
            return __awaiter(_this, void 0, void 0, function () {
                var _b, method, params, requestKey, jsonRpcResponse, requestKeyWithoutParams;
                var _this = this;
                return __generator(this, function (_c) {
                    switch (_c.label) {
                        case 0:
                            _b = payload, method = _b.method, params = _b.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 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, _this.responseTime); })];
                        case 1:
                            _c.sent();
                            if ("throwError" in jsonRpcResponse)
                                throw new Error(jsonRpcResponse.throwError);
                            return [2 /*return*/, jsonRpcResponse];
                    }
                });
            });
        };
    };
    return MockSolanaRpcFactory;
}(SolanaClusterRpcFactory));
export { MockSolanaRpcFactory };
//# sourceMappingURL=MockSolanaRpcFactory.js.map