"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/defineProperty"),t=require("@babel/runtime/helpers/asyncToGenerator"),r=require("@babel/runtime/helpers/toConsumableArray"),s=require("@babel/runtime/helpers/slicedToArray"),n=require("@babel/runtime/regenerator"),a=require("react"),i=require("lodash"),u=require("../utils/index.js");require("uuid"),require("lodash/set"),require("lodash/get"),require("lodash/unset"),require("moment");var o=require("../../../clients/jsonApi/index.js");require("../../../clients/subscriptions.js"),require("../../../clients/api/apiCore.js"),require("../../../clients/constants.js"),require("../../../clients/sso/auth0.js"),require("../../../clients/sso/oauth2.js");var c=require("../../../utils/jsonaDataFormatter.js"),l=require("../../AssetEditor/constants.js");require("../../../constants/goals.js"),require("../../../constants/segment.js"),require("../../../constants/alerts.js"),require("../../../constants/apps.js"),require("../../../constants/drillstring.js"),require("../../../constants/completion.js"),require("../../../constants/appPackages.js"),require("../../../constants/notifications.js"),require("../../../constants/feed.js"),require("../../../constants/bha.js");var p=require("../../../constants/assetTypes.js");function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("../../../utils/env.js"),require("../../GradientManager/configuration/constants.js");var f=d(e),q=d(t),j=d(r),m=d(s),y=d(n);function b(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);t&&(s=s.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,s)}return r}function g(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?b(Object(r),!0).forEach((function(t){f.default(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):b(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}exports.usePrimaryAssetSelectData=function(e){var t,r,s=e.appKey,n=e.assetType,d=e.company,f=e.groups,b=e.isRequiredAssetId,v=e.parentAssetId,h=e.parentAssetType,A=e.searchValue,O=e.assetFields,P=e.currentValue,E=e.setCurrentOption,x=e.scrollPage,D=e.onActiveAssetIdChange,S=e.isDrillingPlatformApp,w=a.useState([]),R=m.default(w,2),_=R[0],T=R[1],k=a.useState(!1),I=m.default(k,2),G=I[0],C=I[1],F=a.useRef(null),z=n===p.ASSET_TYPES.pad.type?[].concat(j.default(O),["pad.active","pad.parent_frac_fleet_id"]):O,M=(t=q.default(y.default.mark((function e(t){var r,a,p,f,q,m;return y.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,r=(null==t?void 0:t.type)||n,C(!0),a={per_page:l.PER_PAGE,page:x},h&&v&&(a[h]=v),d&&"all"!==d&&(a.company=d),A&&(a.search=A),s&&(a.app_key=s),F.current=d,p=o.mapAssetGetRequest(r)(g(g({},u.getRequestOptions(r,z)),a)),e.next=12,p;case 12:f=e.sent,q=c.jsonaDataFormatter.deserialize(f),m=i.map(q,(function(e){return u.formatAsset({asset:e,group:null==t?void 0:t.name,isRequiredAssetId:b,isPrimaryAsset:!0,isDrillingPlatformApp:S})})).sort(u.sortAsset),F.current===d&&T((function(e){return i.uniqBy([].concat(j.default(e),j.default(m)),"id")})),e.next=21;break;case 18:e.prev=18,e.t0=e.catch(0),console.error(e.t0);case 21:return e.prev=21,C(!1),e.finish(21);case 24:case"end":return e.stop()}}),e,null,[[0,18,21,24]])}))),function(e){return t.apply(this,arguments)}),V=(r=q.default(y.default.mark((function e(){var t,r,s,a,l,d,f;return y.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,o.mapAssetGetRequest(n)(g(g({},u.getRequestOptions(n,z)),{},{ids:P}));case 2:if(t=e.sent,!i.isEmpty(t.data)){e.next=7;break}return e.next=6,o.getAssets(g(g({},u.getRequestOptions(n,z)),{},{ids:P}));case 6:t=e.sent;case 7:r=c.jsonaDataFormatter.deserialize(t),s=u.formatAsset({asset:r[0],isPrimaryAsset:!0,isRequiredAssetId:b,isDrillingPlatformApp:S}),E(s),n===p.ASSET_TYPES.rig.type&&(f=S?Number(null===(a=t)||void 0===a||null===(l=a.included)||void 0===l||null===(d=l[0])||void 0===d?void 0:d.id):(null==s?void 0:s.activeWellId)||null,D(f));case 11:case"end":return e.stop()}}),e)}))),function(){return r.apply(this,arguments)});return a.useEffect((function(){d&&T([])}),[d]),a.useEffect((function(){f?f.forEach((function(e){M(e)})):M()}),[n,h,v,d,s,null==f?void 0:f.length,A,x]),a.useEffect((function(){P&&V()}),[]),{assets:_,loading:G}};
