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