import t from"@babel/runtime/helpers/toConsumableArray";import e from"@babel/runtime/helpers/defineProperty";import r from"@babel/runtime/helpers/asyncToGenerator";import s from"@babel/runtime/helpers/slicedToArray";import n from"@babel/runtime/regenerator";import{useState as o,useEffect as i,useRef as a}from"react";import{isEmpty as p}from"lodash";import{formatAsset as c,sortAsset as u,getRequestOptions as l}from"../utils/index.js";import"uuid";import"lodash/set";import"lodash/get";import"lodash/unset";import"moment";import{mapAssetGetRequest as m}from"../../../clients/jsonApi/index.js";import"../../../clients/subscriptions.js";import"../../../clients/api/apiCore.js";import"../../../clients/constants.js";import"../../../clients/sso/auth0.js";import"../../../clients/sso/oauth2.js";import{jsonaDataFormatter as f}from"../../../utils/jsonaDataFormatter.js";import"../../../constants/goals.js";import"../../../constants/segment.js";import"../../../constants/alerts.js";import"../../../constants/apps.js";import"../../../constants/drillstring.js";import"../../../constants/completion.js";import"../../../constants/appPackages.js";import"../../../constants/notifications.js";import"../../../constants/feed.js";import"../../../constants/bha.js";import{ASSET_TYPES as d}from"../../../constants/assetTypes.js";import"../../../utils/env.js";import"../../GradientManager/configuration/constants.js";function y(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(t);e&&(s=s.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,s)}return r}function j(t){for(var r=1;r<arguments.length;r++){var s=null!=arguments[r]?arguments[r]:{};r%2?y(Object(s),!0).forEach((function(r){e(t,r,s[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(s)):y(Object(s)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(s,e))}))}return t}function b(t){var a=t.appKey,p=t.assetType,d=t.assetId,y=t.company,b=t.parentAssetType,h=t.parentAssetId,g=t.queryParams,v=t.assetFields,O=t.activeWellId,A=t.isRequiredAssetId,I=o([]),P=s(I,2),w=P[0],x=P[1],T=o(!0),k=s(T,2),_=k[0],q=k[1];return i((function(){function t(){return(t=r(n.mark((function t(){var r,s;return n.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,m(p)(j(j(j({},l(p,v)),{},e({per_page:1e4,company:"all"!==y?y:null},b,h),a&&{app_key:a}),g));case 2:r=t.sent,s=f.deserialize(r),x(s.map((function(t){return c({asset:t,activeWellId:O,isRequiredAssetId:A})})).sort(u)),q(!1);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}q(!0),function(){t.apply(this,arguments)}()}),[p,d,b,h,a,O,y]),{assets:w,loading:_}}function h(e){var y,b=e.appKey,h=e.assetType,g=e.company,v=e.groups,O=e.isRequiredAssetId,A=e.parentAssetId,I=e.parentAssetType,P=e.searchText,w=e.assetFields,x=e.currentValue,T=e.setCurrentOption,k=e.onActiveAssetIdChange,_=o([]),q=s(_,2),D=q[0],C=q[1],R=o(!1),E=s(R,2),F=E[0],W=E[1],z=a(null),G=(y=r(n.mark((function e(r){var s,o,i,a,y,v,_,q;return n.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,s=(null==r?void 0:r.type)||h,W(!0),o={per_page:1e4},I&&A&&(o[I]=A),g&&"all"!==g&&(o.company=g),P&&(o.search=P),b&&(o.app_key=b),z.current=g,i=s===d.pad.type?[].concat(t(w),["pad.active","pad.parent_frac_fleet_id","pad.current"]):w,a=m(s)(j(j({},l(s,i)),o)),e.next=13,a;case 13:y=e.sent,v=f.deserialize(y),_=v.map((function(t){return c({asset:t,group:null==r?void 0:r.name,isRequiredAssetId:O,isPrimaryAsset:!0})})).filter(Boolean).sort(u),z.current===g&&C((function(e){return[].concat(t(e),t(_))})),x&&s===h&&(q=_.find((function(t){return x===t.id||t.assetId&&x===t.assetId})),p(q)||T(q),s===d.rig.type&&k(null==q?void 0:q.activeWellId)),e.next=23;break;case 20:e.prev=20,e.t0=e.catch(0),console.error(e.t0);case 23:return e.prev=23,W(!1),e.finish(23);case 26:case"end":return e.stop()}}),e,null,[[0,20,23,26]])}))),function(t){return y.apply(this,arguments)});return i((function(){g&&C([])}),[g]),i((function(){v?v.forEach((function(t){G(t)})):G()}),[h,I,A,g,b,null==v?void 0:v.length]),{assets:D,loading:F}}export{h as usePrimaryAssetSelectData,b as useSecondaryAssetSelectData};
