import e from"@babel/runtime/helpers/slicedToArray";import t from"@babel/runtime/helpers/toConsumableArray";import r from"@babel/runtime/helpers/asyncToGenerator";import n from"@babel/runtime/regenerator";import{useState as a,useEffect as s}from"react";import{getResolvedAssets as i,getWells as u}from"../../clients/jsonApi/index.js";var c=["well.name","well.asset_id","well.status","well.pad"],l=function(e){var t=e.appPad,r=e.assets;return t?[{id:t.id.toString(),name:t.name,wells:r.map((function(e){return{assetId:e.asset_id,name:e.name,status:e.status,id:e.id}}))}]:[]},o=function(){var e=r(n.mark((function e(r){var a,s,u,c,l,o,d,p,f,m,b,v;return n.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s=r.appPad,u=r.assets,c=r.currentOffsetSetting,l=s&&u[0]?[]:[null===(a=u[0])||void 0===a?void 0:a.id],o=new Set(u.map((function(e){return e.asset_id}))),(d=c.filter((function(e){return!o.has(e.selectedWellId)}))).length){e.next=6;break}return e.abrupt("return",l);case 6:if(p=d.reduce((function(e,t){var r=e.wellIds,n=e.assetIdsToResolve,a=t.selectedWellId,s=t.selectedWellIdNew;return{assetIdsToResolve:s?n:n.concat(a),wellIds:s?r.concat(s):r}}),{assetIdsToResolve:[],wellIds:[]}),f=p.assetIdsToResolve,m=p.wellIds,!f.length){e.next=13;break}return e.next=10,i({assets:f});case 10:e.t0=e.sent,e.next=14;break;case 13:e.t0={wells:[]};case 14:return b=e.t0,v=b.wells,e.abrupt("return",l.concat.apply(l,t(m).concat(t(v.map((function(e){return e.data.id}))))));case 17:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),d=function(){var e=r(n.mark((function e(t){var r,a,s,i,l,o,d,p;return n.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t.length){e.next=2;break}return e.abrupt("return",[]);case 2:return r=function(e){var t=e.id,r=e.attributes;return{id:t,assetId:r.asset_id,name:r.name,status:r.status}},e.next=5,u({ids:t,fields:c});case 5:if(a=e.sent,s=a.data,i=a.included,l=new Map(s.map((function(e){var t=e.id,n=e.attributes;return[t,r({id:t,attributes:n})]}))),o=i.filter((function(e){return"pad"===e.type})),!(d=o.reduce((function(e,t){var r=t.relationships.wells.data.map((function(e){return e.id})).filter((function(e){return!l.has(e)}));return e.concat(r)}),[])).length){e.next=17;break}return e.next=14,u({ids:d,fields:c});case 14:e.t0=e.sent,e.next=18;break;case 17:e.t0={data:[]};case 18:return p=e.t0,p.data.forEach((function(e){var t=e.id,n=e.attributes;return l.set(t,r({id:t,attributes:n}))})),e.abrupt("return",o.map((function(e){return{id:e.id,name:e.attributes.name,wells:e.relationships.wells.data.map((function(e){var t=e.id;return l.get(t)})).filter(Boolean)}})));case 22:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),p=function(t){var i=t.opened,u=t.currentOffsetSetting,c=t.appPad,p=t.assets,f=a([]),m=e(f,2),b=m[0],v=m[1],w=a([]),h=e(w,2),x=h[0],I=h[1];return s((function(){var e=function(){var e=r(n.mark((function e(){var t,r,a,s;return n.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=u.map((function(e){return e.selectedWellId})),r=l({appPad:c,assets:p}),e.next=4,o({appPad:c,assets:p,currentOffsetSetting:u});case 4:return a=e.sent,e.next=7,d(a);case 7:s=e.sent,v(r.concat(s)),I(t);case 10:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}();i&&e()}),[i,p,u]),{selectedPads:b,selectedWellAssetIds:x,setSelectedPads:v,setSelectedWellAssetIds:I}};export{p as useOffsetPickerActions};
//# sourceMappingURL=useOffsetPickerActions.js.map
