import t from"@babel/runtime/helpers/defineProperty";import e from"@babel/runtime/helpers/asyncToGenerator";import r from"@babel/runtime/helpers/toConsumableArray";import s from"@babel/runtime/helpers/slicedToArray";import n from"@babel/runtime/regenerator";import{useState as o,useRef as i,useEffect as a}from"react";import{map as p,uniqBy as c,isEmpty as l}from"lodash";import{formatAsset as u,sortAsset as m,getRequestOptions as f}from"../utils/index.js";import"uuid";import"lodash/set";import"lodash/get";import"lodash/unset";import"moment";import{mapAssetGetRequest as d,getAssets as j}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 b}from"../../../utils/jsonaDataFormatter.js";import{PER_PAGE as y}from"../../AssetEditor/constants.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 g}from"../../../constants/assetTypes.js";import"../../../utils/env.js";import"../../GradientManager/configuration/constants.js";function h(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 v(e){for(var r=1;r<arguments.length;r++){var s=null!=arguments[r]?arguments[r]:{};r%2?h(Object(s),!0).forEach((function(r){t(e,r,s[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(s)):h(Object(s)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(s,t))}))}return e}function O(t){var h,O,A=t.appKey,P=t.assetType,w=t.company,x=t.groups,D=t.isRequiredAssetId,k=t.parentAssetId,I=t.parentAssetType,T=t.searchValue,_=t.assetFields,C=t.currentValue,E=t.setCurrentOption,q=t.scrollPage,R=t.onActiveAssetIdChange,z=t.isDrillingPlatformApp,F=o([]),G=s(F,2),S=G[0],V=G[1],K=o(!1),M=s(K,2),N=M[0],W=M[1],B=i(null),H=P===g.pad.type?[].concat(r(_),["pad.active","pad.parent_frac_fleet_id"]):_,J=(h=e(n.mark((function t(e){var s,o,i,a,l,j;return n.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,s=(null==e?void 0:e.type)||P,W(!0),o={per_page:y,page:q},I&&k&&(o[I]=k),w&&"all"!==w&&(o.company=w),T&&(o.search=T),A&&(o.app_key=A),B.current=w,i=d(s)(v(v({},f(s,H)),o)),t.next=12,i;case 12:a=t.sent,l=b.deserialize(a),j=p(l,(function(t){return u({asset:t,group:null==e?void 0:e.name,isRequiredAssetId:D,isPrimaryAsset:!0,isDrillingPlatformApp:z})})).sort(m),B.current===w&&V((function(t){return c([].concat(r(t),r(j)),"id")})),t.next=21;break;case 18:t.prev=18,t.t0=t.catch(0),console.error(t.t0);case 21:return t.prev=21,W(!1),t.finish(21);case 24:case"end":return t.stop()}}),t,null,[[0,18,21,24]])}))),function(t){return h.apply(this,arguments)}),L=(O=e(n.mark((function t(){var e,r,s,o,i,a,p;return n.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,d(P)(v(v({},f(P,H)),{},{ids:C}));case 2:if(e=t.sent,!l(e.data)){t.next=7;break}return t.next=6,j(v(v({},f(P,H)),{},{ids:C}));case 6:e=t.sent;case 7:r=b.deserialize(e),s=u({asset:r[0],isPrimaryAsset:!0,isRequiredAssetId:D,isDrillingPlatformApp:z}),E(s),P===g.rig.type&&(p=z?Number(null===(o=e)||void 0===o||null===(i=o.included)||void 0===i||null===(a=i[0])||void 0===a?void 0:a.id):(null==s?void 0:s.activeWellId)||null,R(p));case 11:case"end":return t.stop()}}),t)}))),function(){return O.apply(this,arguments)});return a((function(){w&&V([])}),[w]),a((function(){x?x.forEach((function(t){J(t)})):J()}),[P,I,k,w,A,null==x?void 0:x.length,T,q]),a((function(){C&&L()}),[]),{assets:S,loading:N}}export{O as usePrimaryAssetSelectData};
