import{__awaiter as e,__generator as t,__spreadArray as s}from'./../../ext-esm/tslib/tslib.es6.js';import{useState as n,useEffect as a}from"react";import{getResolvedAssets as r,getWells as i}from"../../clients/jsonApi/index.js";var l=["well.name","well.asset_id","well.status","well.pad"],d=function(n){return e(void 0,[n],void 0,(function(e){var n,a,i,l,d,u,c,o,f,p=e.appPad,v=e.assets,m=e.currentOffsetSetting;return t(this,(function(e){switch(e.label){case 0:return n=p&&v[0]?[]:[null===(f=v[0])||void 0===f?void 0:f.id],a=new Set(v.map((function(e){return e.asset_id}))),(i=m.filter((function(e){return!a.has(e.selectedWellId)}))).length?(l=i.reduce((function(e,t){var s=e.wellIds,n=e.assetIdsToResolve,a=t.selectedWellId,r=t.selectedWellIdNew;return{assetIdsToResolve:r?n:n.concat(a),wellIds:r?s.concat(r):s}}),{assetIdsToResolve:[],wellIds:[]}),d=l.assetIdsToResolve,u=l.wellIds,d.length?[4,r({assets:d})]:[3,2]):[2,n];case 1:return o=e.sent(),[3,3];case 2:o={wells:[]},e.label=3;case 3:return c=o.wells,[2,n.concat.apply(n,s(s([],u,!1),c.map((function(e){return e.data.id})),!1))]}}))}))},u=function(s){var r=s.opened,u=s.currentOffsetSetting,c=s.appPad,o=s.assets,f=n([]),p=f[0],v=f[1],m=n([]),w=m[0],I=m[1];return a((function(){r&&e(void 0,void 0,void 0,(function(){var s,n,a,r;return t(this,(function(f){switch(f.label){case 0:return s=u.map((function(e){return e.selectedWellId})),n=function(e){var t=e.appPad,s=e.assets;return t?[{id:t.id.toString(),name:t.name,wells:s.map((function(e){return{assetId:e.asset_id,name:e.name,status:e.status,id:e.id}}))}]:[]}({appPad:c,assets:o}),[4,d({appPad:c,assets:o,currentOffsetSetting:u})];case 1:return a=f.sent(),[4,(p=a,e(void 0,void 0,void 0,(function(){var e,s,n,a,r,d,u,c;return t(this,(function(t){switch(t.label){case 0:return p.length?(e=function(e){var t=e.id,s=e.attributes;return{id:t,assetId:s.asset_id,name:s.name,status:s.status}},[4,i({ids:p,fields:l})]):[2,[]];case 1:return s=t.sent(),n=s.data,a=s.included,r=new Map(n.map((function(t){var s=t.id,n=t.attributes;return[s,e({id:s,attributes:n})]}))),d=a.filter((function(e){return"pad"===e.type})),u=d.reduce((function(e,t){var s=t.relationships.wells.data.map((function(e){return e.id})).filter((function(e){return!r.has(e)}));return e.concat(s)}),[]),u.length?[4,i({ids:u,fields:l})]:[3,3];case 2:return c=t.sent(),[3,4];case 3:c={data:[]},t.label=4;case 4:return c.data.forEach((function(t){var s=t.id,n=t.attributes;return r.set(s,e({id:s,attributes:n}))})),[2,d.map((function(e){return{id:e.id,name:e.attributes.name,wells:e.relationships.wells.data.map((function(e){var t=e.id;return r.get(t)})).filter(Boolean)}}))]}}))})))];case 2:return r=f.sent(),v(n.concat(r)),I(s),[2]}var p}))}))}),[r,o,u]),{selectedPads:p,selectedWellAssetIds:w,setSelectedPads:v,setSelectedWellAssetIds:I}};export{u as useOffsetPickerActions};
