"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockSolanaRpcFactory = void 0;
const solana_1 = require("../solana");
class MockSolanaRpcFactory extends solana_1.SolanaClusterRpcFactory {
    responseTime = 10;
    responses = new Map();
    constructor(...clusterConstructorParams) {
        super(...clusterConstructorParams);
    }
    createTransport() {
        return (...args) => {
            return this.createMockRpcTransport()(...args);
        };
    }
    setResult(method, params, result) {
        const requestKey = JSON.stringify({ method, params });
        this.responses.set(requestKey, { result });
    }
    setError(method, params, error) {
        const requestKey = JSON.stringify({ method, params });
        this.responses.set(requestKey, { error });
    }
    setThrow(method, params, throwError) {
        const requestKey = JSON.stringify({ method, params });
        this.responses.set(requestKey, { throwError });
    }
    setResponseTime(responseTime) {
        this.responseTime = responseTime;
    }
    createMockRpcTransport() {
        return async ({ payload }) => {
            const { method, params } = payload;
            const requestKey = JSON.stringify({ method, params });
            let jsonRpcResponse = this.responses.get(requestKey);
            if (jsonRpcResponse === undefined) {
                const requestKeyWithoutParams = JSON.stringify({ method, params: [] });
                jsonRpcResponse = this.responses.get(requestKeyWithoutParams);
                if (jsonRpcResponse === undefined)
                    jsonRpcResponse = { result: null };
            }
            await new Promise((resolve) => setTimeout(resolve, this.responseTime));
            if ("throwError" in jsonRpcResponse)
                throw new Error(jsonRpcResponse.throwError);
            return jsonRpcResponse;
        };
    }
}
exports.MockSolanaRpcFactory = MockSolanaRpcFactory;
//# sourceMappingURL=MockSolanaRpcFactory.js.map