import e from"@babel/runtime/helpers/toConsumableArray";import t from"@babel/runtime/helpers/slicedToArray";import r from"@babel/runtime/helpers/defineProperty";import{useReducer as a,useState as n,useMemo as s,useEffect as c}from"react";import{isEmpty as o,get as d}from"lodash";import i from"jsona";import{getPads as l}from"../../clients/jsonApi/index.js";function u(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,a)}return r}function p(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?u(Object(a),!0).forEach((function(t){r(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):u(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}var f=new i,S={status:"idle",padsData:[],wellsData:[],lastStage:[],error:null,companyId:0,selectedPadsData:[]};function E(e,t){switch(t.type){case"FETCH_PADS":return p(p({},e),{},{status:"pending",target:"FETCH_PADS"});case"PADS_REQUEST_SUCCESS":return p(p({},e),{},{status:"success",target:"",padsData:t.filteredPads});case"GET_WELLS":return p(p({},e),{},{status:"success",target:"GET_WELLS"});case"WELLS_REQUEST_SUCCESS":return p(p({},e),{},{status:"success",target:"",wellsData:t.wellsData});case"FAILURE":return p(p({},e),{},{status:"failure",error:t.error});default:throw new Error("Unhandled action type: ".concat(t.type))}}function m(r){var i=r.opened,u=r.companyId,m=r.currentOffsetSetting,y=r.assetIds,P=a(E,S),D=t(P,2),h=D[0],b=D[1],w=n({selectedPadIds:[],selectedWellIds:[],status:"closed"}),g=t(w,2),_=g[0],O=g[1],I=s((function(){return o(m)?new Set:new Set(m.map((function(e){return e.selectedWellId})))}),[m,y]);return c((function(){h.companyId=u,b({type:"FETCH_PADS"})}),[]),c((function(){var e;"FETCH_PADS"===h.target?function(e){var t=e.dispatch,r=e.companyId;l({fields:["pad.name","pad.wells"],sort:"name",per_page:1e3,company:r}).then((function(e){return f.deserialize(e)})).then((function(e){return e.filter((function(e){return!o(d(e,"wells"))}))})).then((function(e){return t({type:"PADS_REQUEST_SUCCESS",filteredPads:e})})).catch((function(e){return t({type:"FAILURE",error:e})}))}({dispatch:b,companyId:h.companyId}):"GET_WELLS"===h.target&&(0,(e={dispatch:b,selectedPadsData:h.selectedPadsData}).dispatch)({type:"WELLS_REQUEST_SUCCESS",wellsData:e.selectedPadsData.reduce((function(e,t){return e.concat(t.wells)}),[])})}),[h.target]),c((function(){if(!o(_.selectedPadIds)){var e=h.padsData.filter((function(e){return _.selectedPadIds.includes(e.id)}));h.selectedPadsData=e,b({type:"GET_WELLS"})}}),[_.selectedPadIds]),c((function(){if(i){var t=new Set;h.padsData.forEach((function(e){e.wells.forEach((function(r){o(I)?y.has(r.asset_id)&&t.add(e.id):I.has(r.asset_id)&&t.add(e.id)}))})),O((function(r){return p(p({},r),{},{selectedPadIds:e(t),selectedWellIds:e(I),status:"open"})}))}}),[i]),{requestState:h,dialogState:_,setDialogState:O}}export{m as useDialogState};
