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