"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.MessageBroker=void 0;const console_1=require("@b/utils/console");class MessageBroker{constructor(e){this.clients=e}sendToClientOnRoute(e,s,t,o=!1){const r=this.clients.get(e);if(r){const e=r.get(s);if(e){e.ws.cork(()=>{if(o){const s=Buffer.from(JSON.stringify(t));e.ws.send(s,!0)}else e.ws.send(JSON.stringify(t))});return!0}}return!1}sendToClient(e,s,t=!1){let o=!1;for(const[r,n]of this.clients.entries())if(n.has(e)){const r=n.get(e);try{r.ws.cork(()=>{if(t){const e=Buffer.from(JSON.stringify(s));r.ws.send(e,!0)}else r.ws.send(JSON.stringify(s))})}catch(s){console_1.logger.error("WS",`Failed to send message to client ${e}`,s);n.delete(e)}o=!0}o||console_1.logger.debug("WS",`Client ${e} not found in any route`)}broadcastToRoute(e,s){const t=JSON.stringify(s);if(this.clients.has(e)){this.clients.get(e).forEach(s=>{try{s.ws.cork(()=>{s.ws.send(t)})}catch(s){console_1.logger.error("WS",`Failed to broadcast to route ${e}`,s)}})}}broadcastToSubscribedClients(e,s,t){try{const o=JSON.stringify(s),r=this.clients.get(e);if(r){let e=0;for(const[s,n]of r)if(n.subscriptions.has(o))try{n.ws.send(JSON.stringify(t));e++}catch(e){console_1.logger.error("WS",`Failed to send to client ${s}`,e);r.delete(s)}}}catch(e){console_1.logger.error("WS","Error in broadcastToSubscribedClients",e)}}}exports.MessageBroker=MessageBroker;