import n from"socket.io-client";import*as c from"query-string";import o from"lodash/noop";var t=new RegExp("^/reports.*$"),e=c.parse(window.location.search).subscriptions_url||process.env.REACT_APP_SUBSCRIPTIONS_URL||"https://subscriptions.qa.corva.ai";function r(n){return t.test(window.location.pathname)?o:n}function i(n,c){return"".concat(n.appInstanceId||c,":").concat(n.companyId,":").concat(n.userId,":").concat(n.collection)}function a(n,c){return"".concat(n.appInstanceId||c,":").concat(n.companyId,":").concat(n.collection)}function s(n,c){return n.event?"".concat(n.appInstanceId||c,":").concat(n.provider,":").concat(n.collection,":").concat(n.assetId,":").concat(n.event):"".concat(n.appInstanceId||c,":").concat(n.provider,":").concat(n.collection,":").concat(n.assetId)}function u(n){var c=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";return"userId"in n?i(n,c):"companyId"in n?a(n,c):s(n,c)}function d(){var c,o={},t=!1,i=r((function(n){if(!c)throw new Error("Not connected");var t=u(n);t in o&&(delete o[t],c.emit("unsubscribe",n))})),a=r((function(n){if(!c)throw new Error("Not connected");var e=u(n);o[e]=n,t&&c.emit("subscribe",n)}));return{connect:r((function(r){var s=r.onDataReceive,u=r.onResubscribe,d=!1;return(c=n(e,{transports:["websocket"]})).on("connect",(function(){c.emit("authenticate"),d&&(Object.values(o).forEach((function(n){i(n),a(n),u(n)})),d=!1)})),c.on("authenticated",(function(){t=!0,Object.values(o).forEach(a)})),c.on("data",s),c.on("disconnect",(function(n){"io server disconnect"===n&&(d=!0,c.connect())})),c.on("reconnect",(function(){d=!0})),c})),disconnect:r((function(){c&&(c.disconnect(),c=null)})),subscribe:a,unsubscribe:i}}var p=function(){var n=d(),c=n.connect,t=n.disconnect,e=n.subscribe,r=n.unsubscribe,i=c({onDataReceive:o,onResubscribe:o});window[Symbol.for("socket")]={subscribe:e,unsubscribe:r,disconnect:t,socket:i}};export{u as getSubscriptionId,d as getSubscriptionsClient,p as initializeSocketClient};
