"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/asyncToGenerator"),r=require("@babel/runtime/helpers/defineProperty"),t=require("@babel/runtime/helpers/slicedToArray"),n=require("@babel/runtime/regenerator"),u=require("react"),i=require("lodash");require("@babel/runtime/helpers/toConsumableArray"),require("uuid"),require("lodash/set"),require("lodash/get"),require("lodash/unset"),require("moment"),require("../../../../clients/jsonApi/index.js"),require("../../../../clients/subscriptions/constants.js"),require("../../../../clients/subscriptions.v1.js"),require("../../../../clients/subscriptions/subscriptions-client.factory.js"),require("../../../../clients/api/apiCore.js"),require("../../../../clients/constants.js"),require("../../../../clients/sso/auth0.js"),require("../../../../clients/sso/oauth2.js"),require("../../../../clients/api/rateLimiter.js");var s=require("../../constants.js");require("../../api/shared/dataFormatter.js");var a=require("../../api/metrics/fetchMetricsData.js"),c=require("../../stores/picker-props-store/usePickerPropsStore.js"),o=require("../../segment-config/index.js"),l=require("./wellTransformations.js");function f(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var p=f(e),d=f(r),b=f(t),m=f(n);function v(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function j(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?v(Object(t),!0).forEach((function(r){d.default(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):v(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var q=function(e){return Number.isFinite(e)?parseFloat(e.toFixed(2)):null};exports.useAdvancedWells=function(e,r,t,n,f,v){var h=c.usePickerPropsStore((function(e){return e.segment})),g=o.getSegmentConfig(h).metricsCollection,y=u.useState(null),O=b.default(y,2),w=O[0],P=O[1],S=u.useState(!1),x=b.default(S,2),k=x[0],E=x[1],A=u.useState(new Set),C=b.default(A,2),D=C[0],M=C[1],F=u.useRef({}),R=u.useRef(f),N=u.useRef(g),T=u.useRef(null),_=u.useMemo((function(){return v&&e?l.transformWells(e,n,r,t):null}),[e,n,r,t,v]);return u.useEffect((function(){if(!_||!v)return E(!1),void M(new Set);var e=_.map((function(e){return j(j({},e),F.current[e.id]||{})}));P(e),E(!0),T.current&&T.current.abort();var r=new AbortController;T.current=r;var t=function(){var e=p.default(m.default.mark((function e(){var t,n,u,c,o,l,v,h,y,O,w;return m.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(e.prev=0,t=[],n=!i.isEqual(R.current,f),u=N.current!==g,n||u?(t=_.map((function(e){return e.id})),F.current={},R.current=f,N.current=g):t=_.filter((function(e){return!F.current[e.id]})).map((function(e){return e.id})),t.length>0&&M(new Set(t)),!(t.length>0&&f.length>0)){e.next=20;break}return c=new Map(_.filter((function(e){return t.includes(e.id)})).map((function(e){return[e.id,e]}))),o=i.groupBy(Array.from(c.values()).filter((function(e){return e.companyId})),"companyId"),l=Object.entries(o).map(function(){var e=p.default(m.default.mark((function e(t){var n,u,i,s;return m.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=b.default(t,2),u=n[0],i=n[1],s=i.map((function(e){return e.id})),e.abrupt("return",a.fetchMetricsData(Number(u),s,f,r.signal,g));case 3:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}()),e.next=12,Promise.all(l);case 12:if(v=e.sent,!r.signal.aborted){e.next=15;break}return e.abrupt("return");case 15:h=v.flat(),y={},O=i.groupBy(h,"asset_id"),t.forEach((function(e){var r,t=null===(r=O[e])||void 0===r?void 0:r.reduce((function(e,r){var t=i.get(r,"data.value"),n=i.get(r,"data.key");return j(j({},e),{},d.default({},n,s.NonConvertableMetrics.includes(n)?t:q(t)))}),{});y[e]=t||{}})),F.current=j(j({},F.current),y);case 20:w=_.map((function(e){return j(j({},e),F.current[e.id]||{})})),P((function(e){return i.isEqual(w,e)?e:w})),E(!1),M(new Set),e.next=29;break;case 26:e.prev=26,e.t0=e.catch(0),r.signal.aborted||(console.error("Error fetching metrics data:",e.t0),P(_),E(!1),M(new Set));case 29:case"end":return e.stop()}}),e,null,[[0,26]])})));return function(){return e.apply(this,arguments)}}();return t(),function(){T.current&&(T.current.abort(),T.current=null)}}),[_,f,v,g]),[w,k,_,D]};
//# sourceMappingURL=useAdvancedWells.js.map
