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