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