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 l}from"../../clients/jsonApi/index.js";var i=function(n){return e(void 0,[n],void 0,(function(e){var n,a,l,i,d,u,o,c,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}))),(l=m.filter((function(e){return!a.has(e.selectedWellId)}))).length?(i=l.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=i.assetIdsToResolve,u=i.wellIds,d.length?[4,r({assets:d})]:[3,2]):[2,n];case 1:return c=e.sent(),[3,3];case 2:c={wells:[]},e.label=3;case 3:return o=c.wells,[2,n.concat.apply(n,s(s([],u,!1),o.map((function(e){return e.data.id})),!1))]}}))}))},d=function(s){var r=s.opened,d=s.currentOffsetSetting,u=s.appPad,o=s.assets,c=n([]),f=c[0],p=c[1],v=n([]),m=v[0],w=v[1];return a((function(){r&&e(void 0,void 0,void 0,(function(){var s,n,a,r;return t(this,(function(c){switch(c.label){case 0:return s=d.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:u,assets:o}),[4,i({appPad:u,assets:o,currentOffsetSetting:d})];case 1:return a=c.sent(),[4,(f=a,e(void 0,void 0,void 0,(function(){var e,s;return t(this,(function(t){switch(t.label){case 0:return f.length?[4,l({ids:f,fields:["well.name","well.asset_id","well.status","well.pad"]})]:[2,[]];case 1:return e=t.sent(),s=new Map(e.data.map((function(e){var t=e.id,s=e.attributes;return[t,{id:t,assetId:s.asset_id,name:s.name,status:s.status}]}))),[2,e.included.filter((function(e){return"pad"===e.type})).map((function(e){return{id:e.id,name:e.attributes.name,wells:e.relationships.wells.data.map((function(e){var t=e.id;return s.get(t)})).filter(Boolean)}}))]}}))})))];case 2:return r=c.sent(),p(n.concat(r)),w(s),[2]}var f}))}))}),[r,o,d]),{selectedPads:f,selectedWellAssetIds:m,setSelectedPads:p,setSelectedWellAssetIds:w}};export{d as useOffsetPickerActions};
