import r from"@babel/runtime/helpers/asyncToGenerator";import e from"@babel/runtime/helpers/defineProperty";import t from"@babel/runtime/helpers/slicedToArray";import n from"@babel/runtime/regenerator";import{useState as i,useRef as o,useEffect as u}from"react";import{get as a,keyBy as c,omit as s,mapValues as f,merge as p}from"lodash";import{getAppHash as l,getInitialAppsDataState as d,isAppNeedingInterventionUnitWellRefresh as v}from"./utils.js";import{resolveAppsData as m}from"./dataResolvers.js";import{isDevCenterApp as h}from"../../../../utils/devcenter.js";import{APPS_DATA_REQUESTS as b}from"./constants.js";function O(r,e){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),t.push.apply(t,n)}return t}function g(r){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?O(Object(n),!0).forEach((function(t){e(r,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(n)):O(Object(n)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(n,e))}))}return r}function y(e){var O=e.apps,y=e.dashboardAssetId,j=i({}),w=t(j,2),I=w[0],P=w[1],E=o(),L=o(),A=o({}),S=o({wellsById:{},rigsById:{},fracFleetsById:{},interventionUnitsById:{},deprecatedAssetData:{}}),_=O.reduce((function(r,e){var t=a(e,["app","ui_settings","multi_rig"]);return h(e)&&!t&&r.push(g(g({},e),{},{appHash:l(e)})),r}),[]),D=function(r){return P((function(e){return f(e,(function(e){var t=r[e.id]||{};return g(g(g({},e),t),{},{isLoading:!1})}))}))},T=function(r){S.current=p(S.current,r)};u((function(){var r=Object.values(I);if(_.length||r.length)if(_.length<r.length){var e=c(_,"id"),t=r.find((function(r){return!e[r.id]})).id;P((function(r){return s(r,t)}))}else{var n=_.filter((function(r){return!I[r.id]||I[r.id].appHash!==r.appHash}));P((function(r){return g(g({},r),d(n))})),m({apps:_,dashboardAssetId:y,cache:S,updateCache:T}).then(D)}}),[String(_.map((function(r){return r.appHash}))),y]),A.current={apps:_,dashboardAssetId:y,cache:S,updateCache:T},u((function(){return E.current=setInterval((function(){m(g(g({},A.current),{},{requests:[b.FRAC_FLEET_WELLS,b.PAD_WELLS]})).then(D)}),3e5),function(){return clearInterval(E.current)}}),[]);var B=_.some(v);return u((function(){if(B)return L.current=setInterval(r(n.mark((function r(){var e,i,o;return n.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return e=A.current.apps,i=new Set(e.filter(v).map((function(r){return r.id}))),r.next=4,m(g(g({},A.current),{},{requests:[b.INTERVENTION_UNIT_WELLS]}));case 4:o=r.sent,P((function(r){var e=Object.entries(r);return e.some((function(r){var e,n,u=t(r,2),a=u[0],c=u[1];if(!i.has(a))return!1;var s=o[a];return!!s&&(null===(e=s.well)||void 0===e?void 0:e.id)!==(null===(n=c.well)||void 0===n?void 0:n.id)}))?e.reduce((function(r,e){var n,u,a=t(e,2),c=a[0],s=a[1];if(!i.has(c))return r[c]=s,r;var f=o[c];return f&&(null===(n=f.well)||void 0===n?void 0:n.id)!==(null===(u=s.well)||void 0===u?void 0:u.id)?(r[c]=g(g(g({},s),f),{},{isLoading:!1}),r):(r[c]=s,r)}),{}):r}));case 6:case"end":return r.stop()}}),r)}))),3e5),function(){return clearInterval(L.current)}}),[B]),I}export{y as default};
//# sourceMappingURL=useAppsData.js.map
