"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/asyncToGenerator"),n=require("@babel/runtime/regenerator"),t=require("react"),r=require("lodash");function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var l=u(e),i=u(n);exports.useSelectionHandlers=function(e){var n=e.subjectWell,u=e.wells,a=e.filteredWells,c=e.externalOffsets,o=e.rawWellsWithSubject,f=e.maxOffsetWellNumber,d=e.maxOffsetSize,s=e.fetchSearchedWells,h=e.setSelectedWells,v=e.setCurrentSelectionZone,p=e.setSelectedWellSection,m=e.hasUserSelectionEditsRef,b=t.useCallback((function(e,n){m.current=!0,v(null),h(n?function(n){if(n.length>=f)return n;var t=(null==u?void 0:u.find((function(n){return n.id===e.id})))||e;return n.concat(t)}:function(n){return n.filter((function(n){return n.id!==e.id}))})}),[u,f,m,h,v]),g=t.useCallback((function(e){if(m.current=!0,v(null),e&&a){var t=n?[n]:[],u=null==n?void 0:n.id,l=r.sortBy(r.uniqBy(a.filter((function(e){return e.id!==u})),"id"),"distance"),i=Math.max(0,f-t.length);h(t.concat(l.slice(0,i)))}else h(n?[n]:[])}),[a,n,f,m,h,v]),S=t.useCallback((function(e,t,l){m.current=!0,h((function(i){if(null!=l&&l.override){var a=n?[n]:[],c=null==n?void 0:n.id,o=e.filter((function(e){return e.id!==c})),d=f-a.length,s=o.slice(0,d),h=s.map((function(e){return(null==u?void 0:u.find((function(n){return n.id===e.id})))||e}));return setTimeout((function(){return null==t?void 0:t({attempted:o.length,added:s.length})}),0),r.uniqBy(a.concat(h),"id")}var v=new Set(i.map((function(e){return e.id}))),p=e.filter((function(e){return!v.has(e.id)})),m=p.length;if(0===m)return setTimeout((function(){return null==t?void 0:t({attempted:0,added:0})}),0),i;var b=f-i.length,g=p.slice(0,b),S=g.length,x=g.map((function(e){return(null==u?void 0:u.find((function(n){return n.id===e.id})))||e}));return setTimeout((function(){return null==t?void 0:t({attempted:m,added:S})}),0),r.uniqBy(i.concat(x),"id")}))}),[n,u,f,m,h]),x=t.useCallback((function(e){v(e)}),[v]),W=t.useCallback(function(){var e=l.default(i.default.mark((function e(t){var u,l,a,f,v,p,m,b,g;return i.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(c){e.next=3;break}return h(n?[n]:[]),e.abrupt("return");case 3:if(l=null==n?void 0:n.id,a=(null!==(u=c[t])&&void 0!==u?u:[]).filter((function(e){return null!=e&&e!==l})),f=new Map((null==o?void 0:o.map((function(e){return[e.id,e]})))||[]),!((v=a.filter((function(e){return!f.has(e)}))).length>0)){e.next=12;break}return e.next=10,s(v);case 10:e.sent.forEach((function(e){return f.set(e.id,e)}));case 12:p=a.map((function(e){return f.get(e)})).filter((function(e){return void 0!==e})),m=r.sortBy(p,"distance"),b=n?[n]:[],g=Math.max(0,d-b.length),h(b.concat(m.slice(0,g)));case 17:case"end":return e.stop()}}),e)})));return function(n){return e.apply(this,arguments)}}(),[c,o,d,n,s,h]),y=t.useCallback(function(){var e=l.default(i.default.mark((function e(n){return i.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return m.current=!0,p(n),e.next=4,W(n);case 4:case"end":return e.stop()}}),e)})));return function(n){return e.apply(this,arguments)}}(),[W,m,p]);return{handleChangeOffsetWell:b,handleChangeAllOffsetWells:g,handleBatchSelectWells:S,handleSelectionZoneConfirm:x,handleChangeWellSection:y,updateSelectedWellsBySection:W}};
//# sourceMappingURL=useSelectionHandlers.js.map
