var __extends=this&&this.__extends||function(){var r=function(t,e){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}}();import Event from"../events/Event.js";import Feature from"../Feature.js";import GeometryType from"../geom/GeometryType.js";import MapBrowserEventType from"../MapBrowserEventType.js";import Point from"../geom/Point.js";import PointerInteraction from"./Pointer.js";import VectorLayer from"../layer/Vector.js";import VectorSource from"../source/Vector.js";import{always}from"../events/condition.js";import{boundingExtent,getArea}from"../extent.js";import{closestOnSegment,distance as coordinateDistance,squaredDistance as squaredCoordinateDistance,squaredDistanceToSegment}from"../coordinate.js";import{createEditingStyle}from"../style/Style.js";import{fromExtent as polygonFromExtent}from"../geom/Polygon.js";import{toUserExtent}from"../proj.js";var ExtentEventType={EXTENTCHANGED:"extentchanged"},ExtentEvent=function(n){function t(t){var e=n.call(this,ExtentEventType.EXTENTCHANGED)||this;return e.extent=t,e}return __extends(t,n),t}(Event),Extent=function(r){function t(t){var e=this,n=t||{};return(e=r.call(this,n)||this).on,e.once,e.un,e.condition_=n.condition||always,e.extent_=null,e.pointerHandler_=null,e.pixelTolerance_=void 0!==n.pixelTolerance?n.pixelTolerance:10,e.snappedToVertex_=!1,e.extentFeature_=null,e.vertexFeature_=null,e.extentOverlay_=new VectorLayer({source:new VectorSource({useSpatialIndex:!1,wrapX:!!(t=t||{}).wrapX}),style:t.boxStyle||getDefaultExtentStyleFunction(),updateWhileAnimating:!0,updateWhileInteracting:!0}),e.vertexOverlay_=new VectorLayer({source:new VectorSource({useSpatialIndex:!1,wrapX:!!t.wrapX}),style:t.pointerStyle||getDefaultPointerStyleFunction(),updateWhileAnimating:!0,updateWhileInteracting:!0}),t.extent&&e.setExtent(t.extent),e}return __extends(t,r),t.prototype.snapToVertex_=function(t,e){var n=e.getCoordinateFromPixelInternal(t),r=this.getExtentInternal();if(r){var r=getSegments(r),r=(r.sort(function(t,e){return squaredDistanceToSegment(n,t)-squaredDistanceToSegment(n,e)}),r[0]),o=closestOnSegment(n,r),i=e.getPixelFromCoordinateInternal(o);if(coordinateDistance(t,i)<=this.pixelTolerance_)return t=e.getPixelFromCoordinateInternal(r[0]),e=e.getPixelFromCoordinateInternal(r[1]),t=squaredCoordinateDistance(i,t),i=squaredCoordinateDistance(i,e),e=Math.sqrt(Math.min(t,i)),this.snappedToVertex_=e<=this.pixelTolerance_,this.snappedToVertex_?i<t?r[1]:r[0]:o}return null},t.prototype.handlePointerMove_=function(t){var e=t.pixel,t=t.map,n=(n=this.snapToVertex_(e,t))||t.getCoordinateFromPixelInternal(e);this.createOrUpdatePointerFeature_(n)},t.prototype.createOrUpdateExtentFeature_=function(t){var e=this.extentFeature_;return e?t?e.setGeometry(polygonFromExtent(t)):e.setGeometry(void 0):(e=new Feature(t?polygonFromExtent(t):{}),this.extentFeature_=e,this.extentOverlay_.getSource().addFeature(e)),e},t.prototype.createOrUpdatePointerFeature_=function(t){var e=this.vertexFeature_;return e?e.getGeometry().setCoordinates(t):(e=new Feature(new Point(t)),this.vertexFeature_=e,this.vertexOverlay_.getSource().addFeature(e)),e},t.prototype.handleEvent=function(t){return!t.originalEvent||!this.condition_(t)||(t.type!=MapBrowserEventType.POINTERMOVE||this.handlingDownUpSequence||this.handlePointerMove_(t),r.prototype.handleEvent.call(this,t),!1)},t.prototype.handleDownEvent=function(t){function e(t){var e=null,n=null;return t[0]==i[0]?e=i[2]:t[0]==i[2]&&(e=i[0]),t[1]==i[1]?n=i[3]:t[1]==i[3]&&(n=i[1]),null!==e&&null!==n?[e,n]:null}var n,r,o=t.pixel,t=t.map,i=this.getExtentInternal(),a=this.snapToVertex_(o,t);return a&&i?(n=a[0]==i[0]||a[0]==i[2]?a[0]:null,r=a[1]==i[1]||a[1]==i[3]?a[1]:null,null!==n&&null!==r?this.pointerHandler_=getPointHandler(e(a)):null!==n?this.pointerHandler_=getEdgeHandler(e([n,i[1]]),e([n,i[3]])):null!==r&&(this.pointerHandler_=getEdgeHandler(e([i[0],r]),e([i[2],r])))):(a=t.getCoordinateFromPixelInternal(o),this.setExtent([a[0],a[1],a[0],a[1]]),this.pointerHandler_=getPointHandler(a)),!0},t.prototype.handleDragEvent=function(t){this.pointerHandler_&&(t=t.coordinate,this.setExtent(this.pointerHandler_(t)),this.createOrUpdatePointerFeature_(t))},t.prototype.handleUpEvent=function(t){this.pointerHandler_=null;var e=this.getExtentInternal();return e&&0!==getArea(e)||this.setExtent(null),!1},t.prototype.setMap=function(t){this.extentOverlay_.setMap(t),this.vertexOverlay_.setMap(t),r.prototype.setMap.call(this,t)},t.prototype.getExtent=function(){return toUserExtent(this.getExtentInternal(),this.getMap().getView().getProjection())},t.prototype.getExtentInternal=function(){return this.extent_},t.prototype.setExtent=function(t){this.extent_=t||null,this.createOrUpdateExtentFeature_(t),this.dispatchEvent(new ExtentEvent(this.extent_))},t}(PointerInteraction);function getDefaultExtentStyleFunction(){var n=createEditingStyle();return function(t,e){return n[GeometryType.POLYGON]}}function getDefaultPointerStyleFunction(){var n=createEditingStyle();return function(t,e){return n[GeometryType.POINT]}}function getPointHandler(e){return function(t){return boundingExtent([e,t])}}function getEdgeHandler(e,n){return e[0]==n[0]?function(t){return boundingExtent([e,[t[0],n[1]]])}:e[1]==n[1]?function(t){return boundingExtent([e,[n[0],t[1]]])}:null}function getSegments(t){return[[[t[0],t[1]],[t[0],t[3]]],[[t[0],t[3]],[t[2],t[3]]],[[t[2],t[3]],[t[2],t[1]]],[[t[2],t[1]],[t[0],t[1]]]]}export default Extent;export{ExtentEvent};