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