import r from"@babel/runtime/helpers/slicedToArray";import t from"@babel/runtime/helpers/asyncToGenerator";import e from"@babel/runtime/helpers/defineProperty";import n from"@babel/runtime/regenerator";import{parse as u}from"querystring";import{useState as a,useRef as i,useMemo as o,useEffect as c}from"react";import s from"uuid/v1";import{castArray as p,differenceBy as f,omit as l,concat as m,merge as b,mapValues as d}from"lodash";import{getSubscriptionId as y}from"../clients/subscriptions.js";import{getAppKeyFromStackTrace as v}from"../clients/utils.js";import{getAppStorage as h,getDataAppStorage as O,getDataAppStorageAggregate as w}from"../clients/jsonApi/index.js";function g(r,t){var e=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),e.push.apply(e,n)}return e}function j(r){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?g(Object(n),!0).forEach((function(t){e(r,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(n)):g(Object(n)).forEach((function(t){Object.defineProperty(r,t,Object.getOwnPropertyDescriptor(n,t))}))}return r}function I(r,t,e){var n=y(r,t);return j(j({},r),{},{hookId:t,subId:n,appInstanceId:n,app_key:e})}function k(r,t){return x.apply(this,arguments)}function x(){return x=t(n.mark((function r(e,u){var a,i,o;return n.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return a=u.timeQuery,i=u.appKey,o=e.map(function(){var r=t(n.mark((function r(t){var e,u,o,c,s,p,f,l,m;return n.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(e=t.provider,u=t.collection,o=t.assetId,c=t.subscribeOnly,s=t.params,!c){r.next=3;break}return r.abrupt("return",null);case 3:return p=s.query,f=a?"{timestamp#lte#".concat(a.timestamp.$lte):"",l=p&&f?"AND":"",m="".concat(p).concat(l).concat(f),r.prev=7,r.next=10,h(e,u,o,j(j(j({},s),m?{query:m}:{}),{},{appKey:i}));case 10:return r.abrupt("return",r.sent);case 13:return r.prev=13,r.t0=r.catch(7),r.abrupt("return",[]);case 16:case"end":return r.stop()}}),r,null,[[7,13]])})));return function(t){return r.apply(this,arguments)}}()),r.abrupt("return",Promise.all(o));case 3:case"end":return r.stop()}}),r)}))),x.apply(this,arguments)}function P(r,t){return E.apply(this,arguments)}function E(){return E=t(n.mark((function r(e,u){var a,i,o;return n.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return a=u.timeQuery,i=u.appKey,o=e.map(function(){var r=t(n.mark((function r(t){var e,u,o,c,s,p,f,l,m,y,v,h;return n.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(e=t.provider,u=t.collection,o=t.assetId,c=t.subscribeOnly,s=t.params,p=void 0===s?{}:s,!c){r.next=3;break}return r.abrupt("return",null);case 3:if(!p.aggregate){r.next=5;break}return r.abrupt("return",w(e,u,j({},d(p.aggregate,JSON.stringify))));case 5:return f=JSON.stringify(b({asset_id:o},p.query,a)),l=p.limit,m=void 0===l?1:l,y=p.sort,v=void 0===y?'{"timestamp": 1}':y,r.prev=7,h=j(j({},p),{},{limit:m,sort:v,query:f,appKey:i}),r.next=11,O(e,u,h);case 11:return r.abrupt("return",r.sent);case 14:return r.prev=14,r.t0=r.catch(7),r.abrupt("return",[]);case 17:case"end":return r.stop()}}),r,null,[[7,14]])})));return function(t){return r.apply(this,arguments)}}()),r.abrupt("return",Promise.all(o));case 3:case"end":return r.stop()}}),r)}))),E.apply(this,arguments)}function D(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},b=n.timestamp,d=n.isOldApiUsed,y=a({}),h=r(y,2),O=h[0],w=h[1],g=i();g.current||(g.current=s());var x=window[Symbol.for("socket")],E=x.subscribe,D=x.unsubscribe,S=x.socket,q=o((function(){return v()}),[]),A=i([]),K=p(t).map((function(r){return I(r,g.current,q)})),N=K.map((function(r){return r.subId})).join(),Q=o((function(){if(b)return{timestamp:{$lte:b}};var r=u(window.location.search).time;return r?{timestamp:{$lte:new Date(r)/1e3}}:null}),[window.location.search,b]);return c((function(){var r=function(r){r.hookId===g.current&&w((function(t){var n=r.data;return r.subId in t&&!r.subscribeOnly&&(n=m(t[r.subId],r.data)),r.params&&r.params.limit&&(n=n.slice(-r.params.limit)),j(j({},t),{},e({},r.subId,n))}))};return S&&S.on("data",r),function(){return S&&S.off("data",r)}}),[]),c((function(){A.current.forEach(D),A.current=[]}),[Q]),c((function(){var r=A.current,t=f(K,r,"subId"),e=f(r,K,"subId");t.forEach((function(r){Q&&!r.alwaysSubscribe||E(r)})),e.forEach(D),A.current=K,(d?k:P)(t,{timeQuery:Q,appKey:q,isOldApiUsed:d}).then((function(r){return w((function(n){var u=l(n,e.map((function(r){return r.subId})));return t.forEach((function(t,e){null!==r[e]&&(u[t.subId]=r[e])})),u}))}))}),[N,Q]),c((function(){return function(){return A.current.forEach(D)}}),[]),o((function(){return K.map((function(r){var t=r.subId;return{loading:!(t in O),data:O[t]}}))}),[O,N])}export{D as default};
