import { serverWsPath, connectWebSocket, ISocketWithClientAuth, createISocketClient, } from "@superblocksteam/shared";
import { createRequestHandlers } from "./handlers.js";
export async function connectToISocketRPCServer({ superblocksBaseUrl, token, tracer, onClose, }) {
    const requestHandlers = createRequestHandlers();
    const authorization = `Bearer ${token}`;
    const wsUrl = new URL(`api/${serverWsPath}`, superblocksBaseUrl);
    if (wsUrl.protocol === "http:") {
        wsUrl.protocol = "ws:";
    }
    else if (wsUrl.protocol === "https:") {
        wsUrl.protocol = "wss:";
    }
    if (wsUrl.host === `localhost:${process.env.UI_CODE_MODE_PROXY_PORT || 3000}` ||
        wsUrl.host === `127.0.0.1:${process.env.UI_CODE_MODE_PROXY_PORT || 3000}`) {
        wsUrl.host = `127.0.0.1:${process.env.SUPERBLOCKS_SERVER_PORT || 8080}`;
    }
    else if (wsUrl.hostname === "localhost") {
        wsUrl.hostname = "127.0.0.1";
    }
    const ws = await connectWebSocket(wsUrl.href, undefined, 5_000);
    const isocket = new ISocketWithClientAuth(ws, authorization, requestHandlers, [], tracer, {
        onClose,
        timeouts: {
            connectionTimeoutInSeconds: 6 * 60, // 6 minutes
            noResponseTimeoutInSeconds: 5 * 60, // 5 minutes (for judge evaluation)
        },
    });
    const client = createISocketClient(isocket);
    return client;
}
//# sourceMappingURL=client.js.map