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