export function defaultWebSocketFactory(url, _options) {
    const globalWs = globalThis.WebSocket;
    if (!globalWs) {
        throw new Error("WebSocket is not available in this runtime");
    }
    return new globalWs(url);
}
export function createWebSocketTransportFactory(factory) {
    return ({ url, headers }) => {
        const ws = factory(url, { headers });
        if ("binaryType" in ws) {
            try {
                ws.binaryType = "arraybuffer";
            }
            catch {
                // no-op
            }
        }
        return {
            send: (data) => {
                if (typeof ws.readyState === "number" && ws.readyState !== 1) {
                    throw new Error(`WebSocket not open (readyState=${ws.readyState})`);
                }
                ws.send(data);
            },
            close: (code, reason) => ws.close(code, reason),
            onOpen: (handler) => bindWsHandler(ws, "open", handler),
            onClose: (handler) => bindWsHandler(ws, "close", handler),
            onError: (handler) => bindWsHandler(ws, "error", handler),
            onMessage: (handler) => bindWsHandler(ws, "message", handler),
        };
    };
}
export function bindWsHandler(ws, event, handler) {
    if (typeof ws.addEventListener === "function") {
        ws.addEventListener(event, handler);
        return () => {
            if (typeof ws.removeEventListener === "function") {
                ws.removeEventListener(event, handler);
            }
        };
    }
    if (typeof ws.on === "function") {
        ws.on(event, handler);
        return () => {
            if (typeof ws.off === "function") {
                ws.off(event, handler);
                return;
            }
            if (typeof ws.removeListener === "function") {
                ws.removeListener(event, handler);
            }
        };
    }
    const prop = `on${event}`;
    const previous = ws[prop];
    ws[prop] = handler;
    return () => {
        if (ws[prop] === handler) {
            ws[prop] = previous ?? null;
        }
    };
}
//# sourceMappingURL=daemon-client-websocket-transport.js.map