import t from"@babel/runtime/helpers/slicedToArray";import e from"@babel/runtime/helpers/defineProperty";import{useState as n,useRef as r,useEffect as o,useCallback as i}from"react";import{getWellsInPolygon as u}from"../utils/selection-geometry.js";function l(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function c(t){for(var n=1;n<arguments.length;n++){var r=null!=arguments[n]?arguments[n]:{};n%2?l(Object(r),!0).forEach((function(n){e(t,n,r[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):l(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var a=function(t,e,n,r,o,l,a,s,d,f,v){var m=i((function(n,r){var o,i=function(t,e,n){var r,o=null===(r=t.current)||void 0===r?void 0:r.getMap();if(!o)return null;var i=o.unproject([Math.min(e.x,n.x),Math.max(e.y,n.y)]),u=o.unproject([Math.max(e.x,n.x),Math.min(e.y,n.y)]),l=o.unproject([Math.min(e.x,n.x),Math.min(e.y,n.y)]),c=o.unproject([Math.max(e.x,n.x),Math.max(e.y,n.y)]);return[{latitude:l.lat,longitude:l.lng},{latitude:u.lat,longitude:u.lng},{latitude:c.lat,longitude:c.lng},{latitude:i.lat,longitude:i.lng}]}(t,n,r);return i?u(e,i,null!==(o=a.current)&&void 0!==o?o:void 0).map((function(t){return t.id})):[]}),[t,e,a]);return{handleMouseDown:i((function(t){if(t.shiftKey&&n.current){t.preventDefault(),t.stopPropagation();var e=n.current.getBoundingClientRect();f({isActive:!0,startPoint:{x:t.clientX-e.left,y:t.clientY-e.top},endPoint:{x:t.clientX-e.left,y:t.clientY-e.top}})}}),[n,f]),handleMouseMove:i((function(t){f((function(e){if(!e.isActive||!e.startPoint||!n.current)return e;var r=n.current.getBoundingClientRect(),o={x:Math.max(0,Math.min(t.clientX-r.left,r.width)),y:Math.max(0,Math.min(t.clientY-r.top,r.height))},i=m(e.startPoint,o),u=new Set(l.current);return v(i.filter((function(t){return!u.has(t)}))),c(c({},e),{},{endPoint:o})}))}),[m,n,l,v,f]),handleMouseUp:i((function(){f((function(t){if(!t.isActive||!t.startPoint||!t.endPoint)return{isActive:!1,startPoint:null,endPoint:null};var e=m(t.startPoint,t.endPoint);return e.length>0&&!o.current&&(o.current=!0,r.current=!0,s.current(e.map((function(t){return{id:t}})),(function(t){var e;null===(e=d.current)||void 0===e||e.call(d,t),o.current=!1}))),v([]),{isActive:!1,startPoint:null,endPoint:null}}))}),[m,o,r,s,d,v,f])}},s=function(e){var i=e.mapRef,u=e.isEnabled,l=e.wells,c=e.offsetWellIds,s=e.subjectWellId,d=e.onBatchSelectWells,f=e.onSelectionComplete,v=n(!1),m=t(v,2),p=m[0],h=m[1],y=n({isActive:!1,startPoint:null,endPoint:null}),g=t(y,2),P=g[0],M=g[1],b=n([]),x=t(b,2),j=x[0],O=x[1],w=r(null),E=r(!1),L=r(!1),S=r(c);S.current=c;var k=r(s);k.current=s;var A=r(d);A.current=d;var D=r(f);D.current=f,function(t,e,n){o((function(){var r=function(e){"Shift"!==e.key||e.repeat||t(!0)},o=function(r){"Shift"===r.key&&(t(!1),e({isActive:!1,startPoint:null,endPoint:null}),n([]))};return document.addEventListener("keydown",r),document.addEventListener("keyup",o),function(){document.removeEventListener("keydown",r),document.removeEventListener("keyup",o)}}),[t,e,n])}(h,M,O);var C=a(i,l,w,E,L,S,k,A,D,M,O);return function(t,e,n,r,i,u){o((function(){var o,l=null===(o=t.current)||void 0===o?void 0:o.getMap();if(l&&e){var c=l.getCanvas();return u.current=c,c.addEventListener("mousedown",n),document.addEventListener("mousemove",r),document.addEventListener("mouseup",i),function(){c.removeEventListener("mousedown",n),document.removeEventListener("mousemove",r),document.removeEventListener("mouseup",i),u.current=null}}}),[t,e,n,r,i,u])}(i,u,C.handleMouseDown,C.handleMouseMove,C.handleMouseUp,w),{boxState:P,wellsInBoxCount:j.length,isShiftHeld:p,shouldSuppressClickRef:E}};export{s as useBoxSelection};
//# sourceMappingURL=useBoxSelection.js.map
