"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useJSONRPCServer = void 0;
const react_1 = require("react");
const json_rpc_2_0_1 = require("json-rpc-2.0");
const useJSONRPCServer = (handlers, send) => {
    const serverRef = (0, react_1.useRef)(null);
    (0, react_1.useEffect)(() => {
        const server = new json_rpc_2_0_1.JSONRPCServerAndClient(new json_rpc_2_0_1.JSONRPCServer(), new json_rpc_2_0_1.JSONRPCClient(send));
        const methodIds = Object.keys(handlers);
        methodIds.forEach((methodId) => {
            server.addMethod(methodId, handlers[methodId]);
        });
        serverRef.current = server;
    }, [send, handlers]);
    const receive = (0, react_1.useCallback)(async (request) => {
        if (serverRef.current) {
            await serverRef.current.receiveAndSend(request);
        }
    }, []);
    return [receive];
};
exports.useJSONRPCServer = useJSONRPCServer;
//# sourceMappingURL=JSONRPCServer.js.map