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