import r from"@babel/runtime/helpers/defineProperty";import t from"@babel/runtime/helpers/slicedToArray";import{parse as n}from"querystring";import{useState as e,useMemo as i,useRef as o,useEffect as c}from"react";import{castArray as u}from"lodash";import{getChannelName as a}from"../../clients/subscriptions/get-channel-name.js";import{getAppKeyAndId as f}from"../../clients/utils.js";import{SubscriptionsClientFactory as s}from"../../clients/subscriptions/subscriptions-client.factory.js";import{fetchInitialSubscriptionsDataDeprecated as p,fetchInitialSubscriptionsData as m}from"./useSubscriptions.v1.js";function l(r,t){var n=Object.keys(r);if(Object.getOwnPropertySymbols){var e=Object.getOwnPropertySymbols(r);t&&(e=e.filter((function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.push.apply(n,e)}return n}function b(t){for(var n=1;n<arguments.length;n++){var e=null!=arguments[n]?arguments[n]:{};n%2?l(Object(e),!0).forEach((function(n){r(t,n,e[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):l(Object(e)).forEach((function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))}))}return t}var y=function(){var l=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],y=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},O=y.timestamp,d=y.isOldApiUsed,j=y.url,g=u(l),h=g.map(a).join(","),v=Array.from(new Set(g.map(a))).sort().join(","),w=e({}),P=t(w,2),A=P[0],D=P[1],S=e(!0),E=t(S,2),K=E[0],T=E[1],$=i((function(){var r;return null===(r=f())||void 0===r?void 0:r.appKey}),[]),k=i((function(){return s.init(j)}),[j]),q=i((function(){return g.map(a)}),[h]),x=i((function(){return Array.from(new Set(g.map(a)))}),[v]),Q=o(!0),U=i((function(){if(O)return{timestamp:{$lte:O}};var r=n(window.location.search).time,t=Array.isArray(r)?r[0]:r;return t?{timestamp:{$lte:new Date(t).getTime()/1e3}}:null}),[window.location.search,O]);return c((function(){return Q.current=!0,function(){Q.current=!1}}),[]),c((function(){if(k&&0!==x.length&&!K){var t=x.map((function(t){return k.subscribe(t,(function(n){Q.current&&D((function(e){return b(b({},e),{},r({},t,n))}))}))}));return function(){t.forEach((function(r){return r()}))}}}),[v,K,k]),c((function(){h&&(d?p:m)(g,{timeQuery:U,appKey:$}).then((function(r){if(Q.current){var t=r.reduce((function(r,t,n){return r[a(g[n])]=t,r}),{});D(t),T(!1)}}))}),[h,U,d,$]),i((function(){return 0===g.length?[]:q.map((function(r){var t=!(r in A);return t?{loading:t}:{loading:!1,data:A[r]}}))}),[A,h])};export{y as useSubscriptionsV2};
//# sourceMappingURL=useSubscriptions.v2.js.map
