import n from"@babel/runtime/helpers/defineProperty";import t from"socket.io-client";import*as e from"query-string";import o from"lodash/noop";import{getHrefDebugField as c}from"./subscriptions/constants.js";function r(n,t){var e=Object.keys(n);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(n);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),e.push.apply(e,o)}return e}function i(t){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{};e%2?r(Object(o),!0).forEach((function(e){n(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):r(Object(o)).forEach((function(n){Object.defineProperty(t,n,Object.getOwnPropertyDescriptor(o,n))}))}return t}var s=new RegExp("^/reports.*$"),a=e.parse(window.location.search).subscriptions_url||process.env.REACT_APP_SUBSCRIPTIONS_URL||"https://subscriptions.qa.corva.ai";function u(n){return s.test(window.location.pathname)?o:n}function p(n,t){return"".concat(n.appInstanceId||t,":").concat(n.companyId,":").concat(n.userId,":").concat(n.collection)}function b(n,t){return"".concat(n.appInstanceId||t,":").concat(n.companyId,":").concat(n.collection)}function d(n,t){return n.event?"".concat(n.appInstanceId||t,":").concat(n.provider,":").concat(n.collection,":").concat(n.assetId,":").concat(n.event):"".concat(n.appInstanceId||t,":").concat(n.provider,":").concat(n.collection,":").concat(n.assetId)}function f(n){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";return"userId"in n?p(n,t):"companyId"in n?b(n,t):d(n,t)}function l(){var n,e={},o=!1,r=u((function(t){if(!n)throw new Error("Not connected");var o=f(t);o in e&&(delete e[o],n.emit("unsubscribe",i(i({},t),c())))})),s=u((function(t){if(!n)throw new Error("Not connected");var r=f(t);e[r]=t,o&&n.emit("subscribe",i(i({},t),c()))})),p=u((function(){var c=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=c.onDataReceive,i=void 0===r?void 0:r,u=c.onResubscribe,p=void 0===u?void 0:u,b=!1;return(n=t(a,{transports:["websocket"]})).on("authenticated",(function(){o=!0,Object.values(e).forEach((function(n){s(n),b&&p&&p(n)})),b=!1})),i&&n.on("data",i),n.on("reconnect",(function(){b=!0})),n}));return{connect:p,disconnect:u((function(){n&&(n.disconnect(),n=null)})),subscribe:s,unsubscribe:r}}var v=function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=n.onDataReceive,e=n.onResubscribe,o=window[Symbol.for("socket")];null!=o&&o.socket&&(console.warn("Existing socket, disconnecting."),o.disconnect());var c=l(),r=c.connect,i=c.disconnect,s=c.subscribe,a=c.unsubscribe,u=r({onDataReceive:t,onResubscribe:e});window[Symbol.for("socket")]={subscribe:s,unsubscribe:a,disconnect:i,socket:u}};export{f as getSubscriptionId,l as getSubscriptionsClient,v as initializeSocketClient};
//# sourceMappingURL=subscriptions.v1.js.map
