terra-draw 1.0.0-beta.6 → 1.0.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var o in i)Object.prototype.hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t},t.apply(this,arguments)}function e(t,e=9){const i=Math.pow(10,e);return Math.round(t*i)/i}const i=(t,e)=>{const{x:i,y:o}=t,{x:r,y:s}=e,n=r-i,a=s-o;return Math.sqrt(a*a+n*n)};class o{constructor({name:t,callback:e,unregister:i,register:o}){this.name=void 0,this.callback=void 0,this.registered=!1,this.register=void 0,this.unregister=void 0,this.name=t,this.register=()=>{this.registered||(this.registered=!0,o(e))},this.unregister=()=>{this.register&&(this.registered=!1,i(e))},this.callback=e}}class r{constructor(t){this._minPixelDragDistance=void 0,this._minPixelDragDistanceDrawing=void 0,this._minPixelDragDistanceSelecting=void 0,this._lastDrawEvent=void 0,this._coordinatePrecision=void 0,this._heldKeys=new Set,this._listeners=[],this._dragState="not-dragging",this._currentModeCallbacks=void 0,this._minPixelDragDistance="number"==typeof t.minPixelDragDistance?t.minPixelDragDistance:1,this._minPixelDragDistanceSelecting="number"==typeof t.minPixelDragDistanceSelecting?t.minPixelDragDistanceSelecting:1,this._minPixelDragDistanceDrawing="number"==typeof t.minPixelDragDistanceDrawing?t.minPixelDragDistanceDrawing:8,this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9}getButton(t){return-1===t.button?"neither":0===t.button?"left":1===t.button?"middle":2===t.button?"right":"neither"}getMapElementXYPosition(t){const e=this.getMapEventElement(),{left:i,top:o}=e.getBoundingClientRect();return{containerX:t.clientX-i,containerY:t.clientY-o}}getDrawEventFromEvent(t){const i=this.getLngLatFromEvent(t);if(!i)return null;const{lng:o,lat:r}=i,{containerX:s,containerY:n}=this.getMapElementXYPosition(t),a=this.getButton(t),l=Array.from(this._heldKeys);return{lng:e(o,this._coordinatePrecision),lat:e(r,this._coordinatePrecision),containerX:s,containerY:n,button:a,heldKeys:l}}register(t){this._currentModeCallbacks=t,this._listeners=this.getAdapterListeners(),this._listeners.forEach(t=>{t.register()})}getCoordinatePrecision(){return this._coordinatePrecision}getAdapterListeners(){return[new o({name:"pointerdown",callback:t=>{if(!this._currentModeCallbacks)return;if(!t.isPrimary)return;const e=this.getDrawEventFromEvent(t);e&&(this._dragState="pre-dragging",this._lastDrawEvent=e)},register:t=>{this.getMapEventElement().addEventListener("pointerdown",t)},unregister:t=>{this.getMapEventElement().removeEventListener("pointerdown",t)}}),new o({name:"pointermove",callback:t=>{if(!this._currentModeCallbacks)return;if(!t.isPrimary)return;t.preventDefault();const e=this.getDrawEventFromEvent(t);if(e)if("not-dragging"===this._dragState)this._currentModeCallbacks.onMouseMove(e),this._lastDrawEvent=e;else if("pre-dragging"===this._dragState){if(!this._lastDrawEvent)return;const t={x:this._lastDrawEvent.containerX,y:this._lastDrawEvent.containerY},o={x:e.containerX,y:e.containerY},r=this._currentModeCallbacks.getState(),s=i(t,o);let n=!1;if(n="drawing"===r?s<this._minPixelDragDistanceDrawing:"selecting"===r?s<this._minPixelDragDistanceSelecting:s<this._minPixelDragDistance,n)return;this._dragState="dragging",this._currentModeCallbacks.onDragStart(e,t=>{this.setDraggability.bind(this)(t)})}else"dragging"===this._dragState&&this._currentModeCallbacks.onDrag(e,t=>{this.setDraggability.bind(this)(t)})},register:t=>{this.getMapEventElement().addEventListener("pointermove",t)},unregister:t=>{this.getMapEventElement().removeEventListener("pointermove",t)}}),new o({name:"contextmenu",callback:t=>{this._currentModeCallbacks&&t.preventDefault()},register:t=>{this.getMapEventElement().addEventListener("contextmenu",t)},unregister:t=>{this.getMapEventElement().removeEventListener("contextmenu",t)}}),new o({name:"pointerup",callback:t=>{if(!this._currentModeCallbacks)return;if(t.target!==this.getMapEventElement())return;if(!t.isPrimary)return;const e=this.getDrawEventFromEvent(t);e&&("dragging"===this._dragState?this._currentModeCallbacks.onDragEnd(e,t=>{this.setDraggability.bind(this)(t)}):"not-dragging"!==this._dragState&&"pre-dragging"!==this._dragState||this._currentModeCallbacks.onClick(e),this._dragState="not-dragging",this.setDraggability(!0))},register:t=>{this.getMapEventElement().addEventListener("pointerup",t)},unregister:t=>{this.getMapEventElement().removeEventListener("pointerup",t)}}),new o({name:"keyup",callback:t=>{this._currentModeCallbacks&&(this._heldKeys.delete(t.key),this._currentModeCallbacks.onKeyUp({key:t.key,heldKeys:Array.from(this._heldKeys),preventDefault:()=>t.preventDefault()}))},register:t=>{this.getMapEventElement().addEventListener("keyup",t)},unregister:t=>{this.getMapEventElement().removeEventListener("keyup",t)}}),new o({name:"keydown",callback:t=>{this._currentModeCallbacks&&(this._heldKeys.add(t.key),this._currentModeCallbacks.onKeyDown({key:t.key,heldKeys:Array.from(this._heldKeys),preventDefault:()=>t.preventDefault()}))},register:t=>{this.getMapEventElement().addEventListener("keydown",t)},unregister:t=>{this.getMapEventElement().removeEventListener("keydown",t)}})]}unregister(){this._listeners.forEach(t=>{t.unregister()}),this.clear()}}class s extends r{constructor(t){if(super(t),this._cursor=void 0,this._cursorStyleSheet=void 0,this._lib=void 0,this._map=void 0,this._overlay=void 0,this._clickEventListener=void 0,this._mouseMoveEventListener=void 0,this.renderedFeatureIds=new Set,this._lib=t.lib,this._map=t.map,!this._map.getDiv().id)throw new Error("Google Map container div requires and id to be set");this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9}get _layers(){var t;return Boolean((null==(t=this.renderedFeatureIds)?void 0:t.size)>0)}circlePath(t,e,i){const o=2*i;return`M ${t} ${e} m -${i}, 0 a ${i},${i} 0 1,0 ${o},0 a ${i},${i} 0 1,0 -${o},0`}register(t){super.register(t),this._overlay=new this._lib.OverlayView,this._overlay.draw=function(){},this._overlay.onAdd=()=>{this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()},this._overlay.setMap(this._map),this._clickEventListener=this._map.data.addListener("click",t=>{const e=this._listeners.find(({name:t})=>"click"===t);e&&e.callback(t)}),this._mouseMoveEventListener=this._map.data.addListener("mousemove",t=>{const e=this._listeners.find(({name:t})=>"mousemove"===t);e&&e.callback(t)})}unregister(){var t,e,i;super.unregister(),null==(t=this._clickEventListener)||t.remove(),null==(e=this._mouseMoveEventListener)||e.remove(),null==(i=this._overlay)||i.setMap(null),this._overlay=void 0}getLngLatFromEvent(t){if(!this._overlay)throw new Error("cannot get overlay");const e=this._map.getBounds();if(!e)return null;const i=e.getNorthEast(),o=e.getSouthWest(),r=new this._lib.LatLngBounds(o,i),s=this._map.getDiv(),n=t.clientX-s.getBoundingClientRect().left,a=t.clientY-s.getBoundingClientRect().top,l=new this._lib.Point(n,a),h=this._overlay.getProjection();if(!h)return null;const c=h.fromContainerPixelToLatLng(l);return c&&r.contains(c)?{lng:c.lng(),lat:c.lat()}:null}getMapEventElement(){return this._map.getDiv().querySelector('div[style*="z-index: 3;"]')}project(t,e){if(!this._overlay)throw new Error("cannot get overlay");if(void 0===this._map.getBounds())throw new Error("cannot get bounds");const i=this._overlay.getProjection();if(void 0===i)throw new Error("cannot get projection");const o=i.fromLatLngToContainerPixel(new this._lib.LatLng(e,t));if(null===o)throw new Error("cannot project coordinates");return{x:o.x,y:o.y}}unproject(t,e){if(!this._overlay)throw new Error("cannot get overlay");const i=this._overlay.getProjection();if(void 0===i)throw new Error("cannot get projection");const o=i.fromContainerPixelToLatLng(new this._lib.Point(t,e));if(null===o)throw new Error("cannot unproject coordinates");return{lng:o.lng(),lat:o.lat()}}setCursor(t){if(t!==this._cursor){if(this._cursorStyleSheet&&(this._cursorStyleSheet.remove(),this._cursorStyleSheet=void 0),"unset"!==t){const e=this._map.getDiv(),i=document.querySelector(`#${e.id} .gm-style > div`);if(i){i.classList.add("terra-draw-google-maps");const e=document.createElement("style");e.innerHTML=`.terra-draw-google-maps { cursor: ${t} !important; }`,document.getElementsByTagName("head")[0].appendChild(e),this._cursorStyleSheet=e}}this._cursor=t}}setDoubleClickToZoom(t){this._map.setOptions(t?{disableDoubleClickZoom:!1}:{disableDoubleClickZoom:!0})}setDraggability(t){this._map.setOptions({draggable:t})}render(t,e){this._layers&&(t.deletedIds.forEach(t=>{const e=this._map.data.getFeatureById(t);e&&(this._map.data.remove(e),this.renderedFeatureIds.delete(t))}),t.updated.forEach(t=>{if(!t||!t.id)throw new Error("Feature is not valid");const e=this._map.data.getFeatureById(t.id);if(!e)throw new Error("Feature could not be found by Google Maps API");switch(e.forEachProperty((t,i)=>{e.setProperty(i,void 0)}),Object.keys(t.properties).forEach(i=>{e.setProperty(i,t.properties[i])}),t.geometry.type){case"Point":{const i=t.geometry.coordinates;e.setGeometry(new this._lib.Data.Point(new this._lib.LatLng(i[1],i[0])))}break;case"LineString":{const i=t.geometry.coordinates,o=[];for(let t=0;t<i.length;t++){const e=i[t],r=new this._lib.LatLng(e[1],e[0]);o.push(r)}e.setGeometry(new this._lib.Data.LineString(o))}break;case"Polygon":{const i=t.geometry.coordinates,o=[];for(let t=0;t<i.length;t++){const e=[];for(let o=0;o<i[t].length;o++){const r=new this._lib.LatLng(i[t][o][1],i[t][o][0]);e.push(r)}o.push(e)}e.setGeometry(new this._lib.Data.Polygon(o))}}}),t.created.forEach(t=>{this.renderedFeatureIds.add(t.id),this._map.data.addGeoJson(t)})),t.created.forEach(t=>{this.renderedFeatureIds.add(t.id)});const i={type:"FeatureCollection",features:[...t.created]};this._map.data.addGeoJson(i),this._map.data.setStyle(t=>{const i=t.getProperty("mode"),o=t.getGeometry();if(!o)throw new Error("Google Maps geometry not found");const r=o.getType(),s={};t.forEachProperty((t,e)=>{s[e]=t});const n=e[i]({type:"Feature",geometry:{type:r,coordinates:[]},properties:s});switch(r){case"Point":return{clickable:!1,icon:{path:this.circlePath(0,0,n.pointWidth),fillColor:n.pointColor,fillOpacity:1,strokeColor:n.pointOutlineColor,strokeWeight:n.pointOutlineWidth,rotation:0,scale:1}};case"LineString":return{strokeColor:n.lineStringColor,strokeWeight:n.lineStringWidth};case"Polygon":return{strokeColor:n.polygonOutlineColor,strokeWeight:n.polygonOutlineWidth,fillOpacity:n.polygonFillOpacity,fillColor:n.polygonFillColor}}throw Error("Unknown feature type")})}clearLayers(){this._layers&&(this._map.data.forEach(t=>{const e=t.getId();this.renderedFeatureIds.has(e)&&this._map.data.remove(t)}),this.renderedFeatureIds=new Set)}clear(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this.clearLayers())}getCoordinatePrecision(){return super.getCoordinatePrecision()}}class n extends r{constructor(t){super(t),this._lib=void 0,this._map=void 0,this._panes={},this._container=void 0,this._layers={},this._lib=t.lib,this._map=t.map,this._container=this._map.getContainer()}createPaneStyleSheet(t,e){const i=document.createElement("style");return i.innerHTML=`.leaflet-${t}-pane {z-index: ${e+600}`,document.getElementsByTagName("head")[0].appendChild(i),this._map.createPane(t),i}clearPanes(){Object.values(this._panes).forEach(t=>{t&&t.remove()}),this._panes={}}clearLayers(){Object.values(this._layers).forEach(t=>{this._map.removeLayer(t)}),this._layers={}}styleGeoJSONLayer(t){return{pointToLayer:(e,i)=>{if(!e.properties)throw new Error("Feature has no properties");if("string"!=typeof e.properties.mode)throw new Error("Feature mode is not a string");const o=(0,t[e.properties.mode])(e),r=String(o.zIndex);return this._panes[r]||(this._panes[r]=this.createPaneStyleSheet(r,o.zIndex)),this._lib.circleMarker(i,{radius:o.pointWidth,stroke:o.pointOutlineWidth||!1,color:o.pointOutlineColor,weight:o.pointOutlineWidth,fillOpacity:.8,fillColor:o.pointColor,pane:r,interactive:!1})},style:e=>{if(!e||!e.properties)return{};const i=e,o=(0,t[i.properties.mode])(i),r=String(o.zIndex);return this._panes[r]||(this._panes[r]=this.createPaneStyleSheet(r,o.zIndex)),"LineString"===i.geometry.type?{interactive:!1,color:o.lineStringColor,weight:o.lineStringWidth,pane:r}:"Polygon"===i.geometry.type?{interactive:!1,fillOpacity:o.polygonFillOpacity,fillColor:o.polygonFillColor,weight:o.polygonOutlineWidth,stroke:!0,color:o.polygonFillColor,pane:r}:{}}}}getLngLatFromEvent(t){const{containerX:e,containerY:i}=this.getMapElementXYPosition(t),o=this._map.containerPointToLatLng({x:e,y:i});return null===o.lng||isNaN(o.lng)||null===o.lat||isNaN(o.lat)?null:{lng:o.lng,lat:o.lat}}getMapEventElement(){return this._container}setDraggability(t){t?this._map.dragging.enable():this._map.dragging.disable()}project(t,e){const{x:i,y:o}=this._map.latLngToContainerPoint({lng:t,lat:e});return{x:i,y:o}}unproject(t,e){const{lng:i,lat:o}=this._map.containerPointToLatLng({x:t,y:e});return{lng:i,lat:o}}setCursor(t){"unset"===t?this.getMapEventElement().style.removeProperty("cursor"):this.getMapEventElement().style.cursor=t}setDoubleClickToZoom(t){t?this._map.doubleClickZoom.enable():this._map.doubleClickZoom.disable()}render(t,e){t.created.forEach(t=>{this._layers[t.id]=this._lib.geoJSON(t,this.styleGeoJSONLayer(e)),this._map.addLayer(this._layers[t.id])}),t.deletedIds.forEach(t=>{this._map.removeLayer(this._layers[t])}),t.updated.forEach(t=>{this._map.removeLayer(this._layers[t.id]),this._layers[t.id]=this._lib.geoJSON(t,this.styleGeoJSONLayer(e)),this._map.addLayer(this._layers[t.id])})}clear(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this.clearLayers(),this.clearPanes())}register(t){super.register(t),this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()}getCoordinatePrecision(){return super.getCoordinatePrecision()}unregister(){return super.unregister()}}class a extends r{constructor(t){super(t),this._nextRender=void 0,this._map=void 0,this._container=void 0,this._rendered=!1,this.changedIds={deletion:!1,points:!1,linestrings:!1,polygons:!1,styling:!1},this._map=t.map,this._container=this._map.getContainer()}clearLayers(){this._rendered&&(["point","linestring","polygon"].forEach(t=>{const e=`td-${t.toLowerCase()}`;this._map.removeLayer(e),"polygon"===t&&this._map.removeLayer(e+"-outline"),this._map.removeSource(e)}),this._rendered=!1,this._nextRender&&(cancelAnimationFrame(this._nextRender),this._nextRender=void 0))}_addGeoJSONSource(t,e){this._map.addSource(t,{type:"geojson",data:{type:"FeatureCollection",features:e},tolerance:0})}_addFillLayer(t){return this._map.addLayer({id:t,source:t,type:"fill",paint:{"fill-color":["get","polygonFillColor"],"fill-opacity":["get","polygonFillOpacity"]}})}_addFillOutlineLayer(t){return this._map.addLayer({id:t+"-outline",source:t,type:"line",paint:{"line-width":["get","polygonOutlineWidth"],"line-color":["get","polygonOutlineColor"]}})}_addLineLayer(t){return this._map.addLayer({id:t,source:t,type:"line",paint:{"line-width":["get","lineStringWidth"],"line-color":["get","lineStringColor"]}})}_addPointLayer(t){return this._map.addLayer({id:t,source:t,type:"circle",paint:{"circle-stroke-color":["get","pointOutlineColor"],"circle-stroke-width":["get","pointOutlineWidth"],"circle-radius":["get","pointWidth"],"circle-color":["get","pointColor"]}})}_addLayer(t,e){"Point"===e&&this._addPointLayer(t),"LineString"===e&&this._addLineLayer(t),"Polygon"===e&&(this._addFillLayer(t),this._addFillOutlineLayer(t))}_addGeoJSONLayer(t,e){const i=`td-${t.toLowerCase()}`;return this._addGeoJSONSource(i,e),this._addLayer(i,t),i}_setGeoJSONLayerData(t,e){const i=`td-${t.toLowerCase()}`;return this._map.getSource(i).setData({type:"FeatureCollection",features:e}),i}updateChangedIds(t){[...t.updated,...t.created].forEach(t=>{"Point"===t.geometry.type?this.changedIds.points=!0:"LineString"===t.geometry.type?this.changedIds.linestrings=!0:"Polygon"===t.geometry.type&&(this.changedIds.polygons=!0)}),t.deletedIds.length>0&&(this.changedIds.deletion=!0),0===t.created.length&&0===t.updated.length&&0===t.deletedIds.length&&(this.changedIds.styling=!0)}getLngLatFromEvent(t){const{left:e,top:i}=this._container.getBoundingClientRect();return this.unproject(t.clientX-e,t.clientY-i)}getMapEventElement(){return this._map.getCanvas()}setDraggability(t){t?(this._map.dragRotate.enable(),this._map.dragPan.enable()):(this._map.dragRotate.disable(),this._map.dragPan.disable())}project(t,e){const{x:i,y:o}=this._map.project({lng:t,lat:e});return{x:i,y:o}}unproject(t,e){const{lng:i,lat:o}=this._map.unproject({x:t,y:e});return{lng:i,lat:o}}setCursor(t){const e=this._map.getCanvas();"unset"===t?e.style.removeProperty("cursor"):e.style.cursor=t}setDoubleClickToZoom(t){t?this._map.doubleClickZoom.enable():this._map.doubleClickZoom.disable()}render(t,e){this.updateChangedIds(t),this._nextRender&&cancelAnimationFrame(this._nextRender),this._nextRender=requestAnimationFrame(()=>{const i=[...t.created,...t.updated,...t.unchanged],o=[],r=[],s=[];for(let t=0;t<i.length;t++){const n=i[t],{properties:a}=n,l=e[a.mode](n);"Point"===n.geometry.type?(a.pointColor=l.pointColor,a.pointOutlineColor=l.pointOutlineColor,a.pointOutlineWidth=l.pointOutlineWidth,a.pointWidth=l.pointWidth,o.push(n)):"LineString"===n.geometry.type?(a.lineStringColor=l.lineStringColor,a.lineStringWidth=l.lineStringWidth,r.push(n)):"Polygon"===n.geometry.type&&(a.polygonFillColor=l.polygonFillColor,a.polygonFillOpacity=l.polygonFillOpacity,a.polygonOutlineColor=l.polygonOutlineColor,a.polygonOutlineWidth=l.polygonOutlineWidth,s.push(n))}if(this._rendered){const t=this.changedIds.deletion||this.changedIds.styling,e=t||this.changedIds.linestrings,i=t||this.changedIds.polygons;let n;(t||this.changedIds.points)&&(n=this._setGeoJSONLayerData("Point",o)),e&&this._setGeoJSONLayerData("LineString",r),i&&this._setGeoJSONLayerData("Polygon",s),n&&this._map.moveLayer(n)}else{const t=this._addGeoJSONLayer("Point",o);this._addGeoJSONLayer("LineString",r),this._addGeoJSONLayer("Polygon",s),this._rendered=!0,t&&this._map.moveLayer(t)}this.changedIds={points:!1,linestrings:!1,polygons:!1,deletion:!1,styling:!1}})}clear(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this.clearLayers())}getCoordinatePrecision(){return super.getCoordinatePrecision()}unregister(){return super.unregister()}register(t){super.register(t),this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()}}class l extends r{constructor(t){super(t),this.mapboxglAdapter=void 0,this.mapboxglAdapter=new a(t)}register(t){this.mapboxglAdapter.register(t)}unregister(){this.mapboxglAdapter.unregister()}getCoordinatePrecision(){return this.mapboxglAdapter.getCoordinatePrecision()}getLngLatFromEvent(t){return this.mapboxglAdapter.getLngLatFromEvent(t)}getMapEventElement(){return this.mapboxglAdapter.getMapEventElement()}setDraggability(t){this.mapboxglAdapter.setDraggability(t)}project(t,e){return this.mapboxglAdapter.project(t,e)}unproject(t,e){return this.mapboxglAdapter.unproject(t,e)}setCursor(t){this.mapboxglAdapter.setCursor(t)}setDoubleClickToZoom(t){this.mapboxglAdapter.setDoubleClickToZoom(t)}render(t,e){this.mapboxglAdapter.render(t,e)}clear(){this.mapboxglAdapter.clear()}}const h={radians:6370997/(2*Math.PI),degrees:2*Math.PI*6370997/360,ft:.3048,m:1,"us-ft":1200/3937};var c=class{constructor(t){this.code_=t.code,this.units_=t.units,this.extent_=void 0!==t.extent?t.extent:null,this.worldExtent_=void 0!==t.worldExtent?t.worldExtent:null,this.axisOrientation_=void 0!==t.axisOrientation?t.axisOrientation:"enu",this.global_=void 0!==t.global&&t.global,this.canWrapX_=!(!this.global_||!this.extent_),this.getPointResolutionFunc_=t.getPointResolution,this.defaultTileGrid_=null,this.metersPerUnit_=t.metersPerUnit}canWrapX(){return this.canWrapX_}getCode(){return this.code_}getExtent(){return this.extent_}getUnits(){return this.units_}getMetersPerUnit(){return this.metersPerUnit_||h[this.units_]}getWorldExtent(){return this.worldExtent_}getAxisOrientation(){return this.axisOrientation_}isGlobal(){return this.global_}setGlobal(t){this.global_=t,this.canWrapX_=!(!t||!this.extent_)}getDefaultTileGrid(){return this.defaultTileGrid_}setDefaultTileGrid(t){this.defaultTileGrid_=t}setExtent(t){this.extent_=t,this.canWrapX_=!(!this.global_||!t)}setWorldExtent(t){this.worldExtent_=t}setGetPointResolution(t){this.getPointResolutionFunc_=t}getPointResolutionFunc(){return this.getPointResolutionFunc_}};const d=6378137,u=Math.PI*d,g=[-u,-u,u,u],p=[-180,-85,180,85],y=d*Math.log(Math.tan(Math.PI/2));class m extends c{constructor(t){super({code:t,units:"m",extent:g,global:!0,worldExtent:p,getPointResolution:function(t,e){return t/Math.cosh(e[1]/d)}})}}const f=[new m("EPSG:3857"),new m("EPSG:102100"),new m("EPSG:102113"),new m("EPSG:900913"),new m("http://www.opengis.net/def/crs/EPSG/0/3857"),new m("http://www.opengis.net/gml/srs/epsg.xml#3857")],C=[-180,-90,180,90],v=6378137*Math.PI/180;class P extends c{constructor(t,e){super({code:t,units:"degrees",extent:C,axisOrientation:e,global:!0,metersPerUnit:v,worldExtent:C})}}const _=[new P("CRS:84"),new P("EPSG:4326","neu"),new P("urn:ogc:def:crs:OGC:1.3:CRS84"),new P("urn:ogc:def:crs:OGC:2:84"),new P("http://www.opengis.net/def/crs/OGC/1.3/CRS84"),new P("http://www.opengis.net/gml/srs/epsg.xml#4326","neu"),new P("http://www.opengis.net/def/crs/EPSG/0/4326","neu")];let x={},S={};function M(t,e,i){const o=t.getCode(),r=e.getCode();o in S||(S[o]={}),S[o][r]=i}function E(t,e){if(void 0!==e)for(let i=0,o=t.length;i<o;++i)e[i]=t[i];else e=t.slice();return e}function b(t,e){if(void 0!==e&&t!==e){for(let i=0,o=t.length;i<o;++i)e[i]=t[i];t=e}return t}function w(t){!function(t,e){x[t]=e}(t.getCode(),t),M(t,t,E)}function I(t){return"string"==typeof t?x[e=t]||x[e.replace(/urn:(x-)?ogc:def:crs:EPSG:(.*:)?(\w+)$/,"EPSG:$3")]||null:t||null;var e}function F(t){!function(t){t.forEach(w)}(t),t.forEach(function(e){t.forEach(function(t){e!==t&&M(e,t,E)})})}function D(t,e,i){const o=function(t,e){return function(t,e){let i=function(t,e){let i;return t in S&&e in S[t]&&(i=S[t][e]),i}(t.getCode(),e.getCode());return i||(i=b),i}(I(t),I(e))}(e,i);return o(t,void 0,t.length)}var k,O,j,W;F(f),F(_),k=f,O=function(t,e,i){const o=t.length;i=i>1?i:2,void 0===e&&(e=i>2?t.slice():new Array(o));for(let r=0;r<o;r+=i){e[r]=u*t[r]/180;let i=d*Math.log(Math.tan(Math.PI*(+t[r+1]+90)/360));i>y?i=y:i<-y&&(i=-y),e[r+1]=i}return e},j=function(t,e,i){const o=t.length;i=i>1?i:2,void 0===e&&(e=i>2?t.slice():new Array(o));for(let r=0;r<o;r+=i)e[r]=180*t[r]/u,e[r+1]=360*Math.atan(Math.exp(t[r+1]/d))/Math.PI-90;return e},_.forEach(function(t){k.forEach(function(e){M(t,e,O),M(e,t,j)})});class L extends r{constructor(t){super(t),this.stylingFunction=()=>({}),this._lib=void 0,this._map=void 0,this._container=void 0,this._projection=void 0,this._vectorSource=void 0,this._geoJSONReader=void 0,this._map=t.map,this._lib=t.lib,this._geoJSONReader=new this._lib.GeoJSON,this._projection=()=>{var t;return null!=(t=this._lib.getUserProjection())?t:new c({code:"EPSG:3857"})},this._container=this._map.getViewport(),this._container.setAttribute("tabindex","0");const e=new this._lib.VectorSource({features:[]});this._vectorSource=e;const i=new this._lib.VectorLayer({source:e,style:t=>this.getStyles(t,this.stylingFunction())});this._map.addLayer(i)}hexToRGB(t){return{r:parseInt(t.slice(1,3),16),g:parseInt(t.slice(3,5),16),b:parseInt(t.slice(5,7),16)}}getStyles(t,e){const i=t.getGeometry();if(i)return{Point:t=>{const i=t.getProperties(),o=e[i.mode]({type:"Feature",geometry:{type:"Point",coordinates:[]},properties:i});return new this._lib.Style({image:new this._lib.Circle({radius:o.pointWidth,fill:new this._lib.Fill({color:o.pointColor}),stroke:new this._lib.Stroke({color:o.pointOutlineColor,width:o.pointOutlineWidth})})})},LineString:t=>{const i=t.getProperties(),o=e[i.mode]({type:"Feature",geometry:{type:"LineString",coordinates:[]},properties:i});return new this._lib.Style({stroke:new this._lib.Stroke({color:o.lineStringColor,width:o.lineStringWidth})})},Polygon:t=>{const i=t.getProperties(),o=e[i.mode]({type:"Feature",geometry:{type:"Polygon",coordinates:[]},properties:i}),{r,g:s,b:n}=this.hexToRGB(o.polygonFillColor);return new this._lib.Style({stroke:new this._lib.Stroke({color:o.polygonOutlineColor,width:o.polygonOutlineWidth}),fill:new this._lib.Fill({color:`rgba(${r},${s},${n},${o.polygonFillOpacity})`})})}}[i.getType()](t)}clearLayers(){this._vectorSource&&this._vectorSource.clear()}addFeature(t){const e=this._geoJSONReader.readFeature(t,{dataProjection:"EPSG:4326",featureProjection:this._projection()});this._vectorSource.addFeature(e)}removeFeature(t){const e=this._vectorSource.getFeatureById(t);e&&this._vectorSource.removeFeature(e)}getLngLatFromEvent(t){const{containerX:e,containerY:i}=this.getMapElementXYPosition(t);try{return this.unproject(e,i)}catch(t){return null}}getMapEventElement(){const t=this._container.querySelectorAll("canvas");if(t.length>1)throw Error("Terra Draw currently only supports 1 canvas with OpenLayers");return t[0]}setDraggability(t){this._map.getInteractions().forEach(e=>{"DragPan"===e.constructor.name&&e.setActive(t)})}project(t,e){const[i,o]=this._map.getPixelFromCoordinate(D([t,e],"EPSG:4326",void 0!==(r=this._projection())?r:"EPSG:3857"));var r;return{x:i,y:o}}unproject(t,e){const[i,o]=function(t,e){const i=D(t,void 0!==e?e:"EPSG:3857","EPSG:4326"),o=i[0];return(o<-180||o>180)&&(i[0]=function(t,e){const i=t%360;return 360*i<0?i+360:i}(o+180)-180),i}(this._map.getCoordinateFromPixel([t,e]),this._projection());return{lng:i,lat:o}}setCursor(t){"unset"===t?this.getMapEventElement().style.removeProperty("cursor"):this.getMapEventElement().style.cursor=t}setDoubleClickToZoom(t){this._map.getInteractions().forEach(function(e){"DoubleClickZoom"===e.constructor.name&&e.setActive(t)})}render(t,e){this.stylingFunction=()=>e,t.deletedIds.forEach(t=>{this.removeFeature(t)}),t.updated.forEach(t=>{this.removeFeature(t.id),this.addFeature(t)}),t.created.forEach(t=>{this.addFeature(t)})}clear(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this.clearLayers())}register(t){super.register(t),this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()}getCoordinatePrecision(){return super.getCoordinatePrecision()}unregister(){return super.unregister()}}class B extends r{constructor(t){super(t),this._lib=void 0,this._mapView=void 0,this._container=void 0,this._featureIdAttributeName="__tdId",this._featureLayerName="__terraDrawFeatures",this._featureLayer=void 0,this._dragEnabled=!0,this._zoomEnabled=!0,this._dragHandler=void 0,this._doubleClickHandler=void 0,this._mapView=t.map,this._lib=t.lib,this._container=this._mapView.container,this._featureLayer=new this._lib.GraphicsLayer({id:this._featureLayerName}),this._mapView.map.add(this._featureLayer)}register(t){super.register(t),this._dragHandler=this._mapView.on("drag",t=>{this._dragEnabled||t.stopPropagation()}),this._doubleClickHandler=this._mapView.on("double-click",t=>{this._zoomEnabled||t.stopPropagation()}),this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()}unregister(){super.unregister(),this._dragHandler&&this._dragHandler.remove(),this._doubleClickHandler&&this._doubleClickHandler.remove()}getCoordinatePrecision(){return super.getCoordinatePrecision()}getLngLatFromEvent(t){const{containerX:e,containerY:i}=this.getMapElementXYPosition(t);return this.unproject(e,i)}getMapEventElement(){return this._container.querySelector(".esri-view-surface")}setDraggability(t){this._dragEnabled=t}project(t,e){const i=new this._lib.Point({longitude:t,latitude:e}),{x:o,y:r}=this._mapView.toScreen(i);return{x:o,y:r}}unproject(t,e){const{latitude:i,longitude:o}=this._mapView.toMap({x:t,y:e});return{lng:o,lat:i}}setCursor(t){"unset"===t?this.getMapEventElement().style.removeProperty("cursor"):this.getMapEventElement().style.cursor=t}setDoubleClickToZoom(t){this._zoomEnabled=t}render(t,e){t.created.forEach(t=>{this.addFeature(t,e)}),t.updated.forEach(t=>{this.removeFeatureById(t.id),this.addFeature(t,e)}),t.deletedIds.forEach(t=>{this.removeFeatureById(t)})}clear(){this._featureLayer.graphics.removeAll()}removeFeatureById(t){const e=this._featureLayer.graphics.find(e=>e.attributes[this._featureIdAttributeName]===t);this._featureLayer.remove(e)}addFeature(t,e){const{coordinates:i,type:o}=t.geometry,r=e[t.properties.mode](t);let s,n;switch(o){case"Point":n=new this._lib.Point({latitude:i[1],longitude:i[0]}),s=new this._lib.SimpleMarkerSymbol({color:this.getColorFromHex(r.pointColor),size:2*r.pointWidth+"px",outline:{color:this.getColorFromHex(r.pointOutlineColor),width:r.pointOutlineWidth+"px"}});break;case"LineString":n=new this._lib.Polyline({paths:[i]}),s=new this._lib.SimpleLineSymbol({color:this.getColorFromHex(r.lineStringColor),width:r.lineStringWidth+"px"});break;case"Polygon":n=new this._lib.Polygon({rings:i}),s=new this._lib.SimpleFillSymbol({color:this.getColorFromHex(r.polygonFillColor,r.polygonFillOpacity),outline:{color:this.getColorFromHex(r.polygonOutlineColor),width:r.polygonOutlineWidth+"px"}})}const a=new this._lib.Graphic({geometry:n,symbol:s,attributes:{[this._featureIdAttributeName]:t.id}});"Point"===o?this._featureLayer.graphics.add(a):this._featureLayer.graphics.add(a,0)}getColorFromHex(t,e){const i=this._lib.Color.fromHex(t);return e&&(i.a=e),i}}!function(t){t.Commit="commit",t.Provisional="provisional",t.Finish="finish"}(W||(W={}));const G="selected",N="midPoint",V="closingPoint";function A(t){return Boolean(t&&"object"==typeof t&&null!==t&&!Array.isArray(t))}function T(t){if(!function(t){return"number"==typeof t&&!isNaN(new Date(t).valueOf())}(t))throw new Error("updatedAt and createdAt are not valid timestamps");return!0}var R;!function(t){t.Drawing="drawing",t.Select="select",t.Static="static",t.Render="render"}(R||(R={}));class U{get state(){return this._state}set state(t){throw new Error("Please use the modes lifecycle methods")}get styles(){return this._styles}set styles(t){if("object"!=typeof t)throw new Error("Styling must be an object");this.onStyleChange([],"styling"),this._styles=t}registerBehaviors(t){}constructor(e){this._state=void 0,this._styles=void 0,this.behaviors=[],this.validate=void 0,this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.onStyleChange=void 0,this.store=void 0,this.setDoubleClickToZoom=void 0,this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this.projection=void 0,this.type=R.Drawing,this.mode="base",this._state="unregistered",this._styles=e&&e.styles?t({},e.styles):{},this.pointerDistance=e&&e.pointerDistance||40,this.validate=e&&e.validation,this.projection=e&&e.projection||"web-mercator"}setDrawing(){if("started"!==this._state)throw new Error("Mode must be unregistered or stopped to start");this._state="drawing"}setStarted(){if("stopped"!==this._state&&"registered"!==this._state&&"drawing"!==this._state&&"selecting"!==this._state)throw new Error("Mode must be unregistered or stopped to start");this._state="started",this.setDoubleClickToZoom(!1)}setStopped(){if("started"!==this._state)throw new Error("Mode must be started to be stopped");this._state="stopped",this.setDoubleClickToZoom(!0)}register(t){if("unregistered"!==this._state)throw new Error("Can not register unless mode is unregistered");this._state="registered",this.store=t.store,this.store.registerOnChange(t.onChange),this.setDoubleClickToZoom=t.setDoubleClickToZoom,this.project=t.project,this.unproject=t.unproject,this.onSelect=t.onSelect,this.onDeselect=t.onDeselect,this.setCursor=t.setCursor,this.onStyleChange=t.onChange,this.onFinish=t.onFinish,this.coordinatePrecision=t.coordinatePrecision,this.registerBehaviors({mode:t.mode,store:this.store,project:this.project,unproject:this.unproject,pointerDistance:this.pointerDistance,coordinatePrecision:t.coordinatePrecision,projection:this.projection})}validateFeature(t){if("unregistered"===this._state)throw new Error("Mode must be registered");const e=function(t,e){let i;if(A(t))if(null==t.id)i="Feature has no id";else if("string"!=typeof t.id&&"number"!=typeof t.id)i="Feature must be string or number as per GeoJSON spec";else if(e(t.id))if(A(t.geometry))if(A(t.properties))if("string"==typeof t.geometry.type&&["Polygon","LineString","Point"].includes(t.geometry.type))if(Array.isArray(t.geometry.coordinates)){if(!t.properties.mode||"string"!=typeof t.properties.mode)throw new Error("Feature does not have a valid mode property")}else i="Feature coordinates is not an array";else i="Feature is not Point, LineString or Polygon";else i="Feature has no properties";else i="Feature has no geometry";else i="Feature must match the id strategy (default is UUID4)";else i="Feature is not object";if(i)throw new Error(i);return!0}(t,this.store.idStrategy.isValidId);return this.validate?this.validate(t,{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:W.Provisional}):e}onFinish(t,e){}onDeselect(t){}onSelect(t){}onKeyDown(t){}onKeyUp(t){}onMouseMove(t){}onClick(t){}onDragStart(t,e){}onDrag(t,e){}onDragEnd(t,e){}getHexColorStylingValue(t,e,i){return this.getStylingValue(t,e,i)}getNumericStylingValue(t,e,i){return this.getStylingValue(t,e,i)}getStylingValue(t,e,i){return void 0===t?e:"function"==typeof t?t(i):t}}class X extends U{constructor(...t){super(...t),this.type=R.Select}}function z(t,e){const i=t=>t*Math.PI/180,o=i(t[1]),r=i(t[0]),s=i(e[1]),n=s-o,a=i(e[0])-r,l=Math.sin(n/2)*Math.sin(n/2)+Math.cos(o)*Math.cos(s)*Math.sin(a/2)*Math.sin(a/2);return 2*Math.atan2(Math.sqrt(l),Math.sqrt(1-l))*6371e3/1e3}const Y=6371008.8;function K(t){return t%360*Math.PI/180}function H(t){return t/(Y/1e3)}function $(t){return t%(2*Math.PI)*180/Math.PI}const J=57.29577951308232,Z=.017453292519943295,q=6378137,Q=(t,e)=>({x:0===t?0:t*Z*q,y:0===e?0:Math.log(Math.tan(Math.PI/4+e*Z/2))*q}),tt=(t,e)=>({lng:0===t?0:J*(t/q),lat:0===e?0:(2*Math.atan(Math.exp(e/q))-Math.PI/2)*J});function et(t,e,i){const o=K(t[0]),r=K(t[1]),s=K(i),n=H(e),a=Math.asin(Math.sin(r)*Math.cos(n)+Math.cos(r)*Math.sin(n)*Math.cos(s));return[$(o+Math.atan2(Math.sin(s)*Math.sin(n)*Math.cos(r),Math.cos(n)-Math.sin(r)*Math.sin(a))),$(a)]}function it(t){const{center:i,radiusKilometers:o,coordinatePrecision:r}=t,s=t.steps?t.steps:64,n=[];for(let t=0;t<s;t++){const a=et(i,o,-360*t/s);n.push([e(a[0],r),e(a[1],r)])}return n.push(n[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[n]},properties:{}}}function ot(t){const e={epsilon:0};let i;if("Polygon"===t.geometry.type)i=t.geometry.coordinates;else{if("LineString"!==t.geometry.type)throw new Error("Self intersects only accepts Polygons and LineStrings");i=[t.geometry.coordinates]}const o=[];for(let t=0;t<i.length;t++)for(let e=0;e<i[t].length-1;e++)for(let o=0;o<i.length;o++)for(let r=0;r<i[o].length-1;r++)s(t,e,o,r);return o.length>0;function r(t){return t<0-e.epsilon||t>1+e.epsilon}function s(t,e,s,n){const a=i[t][e],l=i[t][e+1],h=i[s][n],c=i[s][n+1],d=function(t,e,i,o){if(rt(t,i)||rt(t,o)||rt(e,i)||rt(o,i))return null;const r=t[0],s=t[1],n=e[0],a=e[1],l=i[0],h=i[1],c=o[0],d=o[1],u=(r-n)*(h-d)-(s-a)*(l-c);return 0===u?null:[((r*a-s*n)*(l-c)-(r-n)*(l*d-h*c))/u,((r*a-s*n)*(h-d)-(s-a)*(l*d-h*c))/u]}(a,l,h,c);if(null===d)return;let u,g;u=l[0]!==a[0]?(d[0]-a[0])/(l[0]-a[0]):(d[1]-a[1])/(l[1]-a[1]),g=c[0]!==h[0]?(d[0]-h[0])/(c[0]-h[0]):(d[1]-h[1])/(c[1]-h[1]),r(u)||r(g)||(d.toString(),o.push(d))}}function rt(t,e){return t[0]===e[0]&&t[1]===e[1]}function st(t,e){return 2===t.length&&"number"==typeof t[0]&&"number"==typeof t[1]&&Infinity!==t[0]&&Infinity!==t[1]&&(o=t[0])>=-180&&o<=180&&(i=t[1])>=-90&&i<=90&&nt(t[0])<=e&&nt(t[1])<=e;var i,o}function nt(t){let e=1,i=0;for(;Math.round(t*e)/e!==t;)e*=10,i++;return i}function at(t,e){return"Polygon"===t.geometry.type&&1===t.geometry.coordinates.length&&t.geometry.coordinates[0].length>=4&&t.geometry.coordinates[0].every(t=>st(t,e))&&(i=t.geometry.coordinates[0][0])[0]===(o=t.geometry.coordinates[0][t.geometry.coordinates[0].length-1])[0]&&i[1]===o[1];var i,o}function lt(t,e){return at(t,e)&&!ot(t)}class ht extends U{constructor(e){var i;super(e),this.mode="circle",this.center=void 0,this.clickCount=0,this.currentCircleId=void 0,this.keyEvents=void 0,this.cursors=void 0,this.startingRadiusKilometers=1e-5;const o={start:"crosshair"};if(this.cursors=e&&e.cursors?t({},o,e.cursors):o,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.startingRadiusKilometers=null!=(i=null==e?void 0:e.startingRadiusKilometers)?i:1e-5,this.validate=null==e?void 0:e.validation}close(){if(void 0===this.currentCircleId)return;const t=this.currentCircleId;if(this.validate&&t){const e=this.store.getGeometryCopy(t);if(!this.validate({type:"Feature",id:t,geometry:e,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:W.Finish}))return}this.center=void 0,this.currentCircleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){if(0===this.clickCount){this.center=[t.lng,t.lat];const e=it({center:this.center,radiusKilometers:this.startingRadiusKilometers,coordinatePrecision:this.coordinatePrecision}),[i]=this.store.create([{geometry:e.geometry,properties:{mode:this.mode,radiusKilometers:this.startingRadiusKilometers}}]);this.currentCircleId=i,this.clickCount++,this.setDrawing()}else 1===this.clickCount&&this.center&&void 0!==this.currentCircleId&&this.updateCircle(t),this.close()}onMouseMove(t){this.updateCircle(t)}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentCircleId;this.center=void 0,this.currentCircleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t])}catch(t){}}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Polygon"===e.geometry.type&&e.properties.mode===this.mode?(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i):i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&lt(t,this.coordinatePrecision)}updateCircle(t){if(1===this.clickCount&&this.center&&this.currentCircleId){const i=z(this.center,[t.lng,t.lat]);let o;if("web-mercator"===this.projection){const r=function(t,e){const i=1e3*z(t,e);if(0===i)return 1;const{x:o,y:r}=Q(t[0],t[1]),{x:s,y:n}=Q(e[0],e[1]);return Math.sqrt(Math.pow(s-o,2)+Math.pow(n-r,2))/i}(this.center,[t.lng,t.lat]);o=function(t){const{center:i,radiusKilometers:o,coordinatePrecision:r}=t,s=t.steps?t.steps:64,n=1e3*o,[a,l]=i,{x:h,y:c}=Q(a,l),d=[];for(let t=0;t<s;t++){const i=360*t/s*Math.PI/180,o=n*Math.cos(i),a=n*Math.sin(i),[l,u]=[h+o,c+a],{lng:g,lat:p}=tt(l,u);d.push([e(g,r),e(p,r)])}return d.push(d[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[d]},properties:{}}}({center:this.center,radiusKilometers:i*r,coordinatePrecision:this.coordinatePrecision})}else{if("globe"!==this.projection)throw new Error("Invalid projection");o=it({center:this.center,radiusKilometers:i,coordinatePrecision:this.coordinatePrecision})}if(this.validate&&!this.validate({type:"Feature",id:this.currentCircleId,geometry:o.geometry,properties:{radiusKilometers:i}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:W.Provisional}))return;this.store.updateGeometry([{id:this.currentCircleId,geometry:o.geometry}]),this.store.updateProperty([{id:this.currentCircleId,property:"radiusKilometers",value:i}])}}}class ct extends U{constructor(e){super(e),this.mode="freehand",this.startingClick=!1,this.currentId=void 0,this.closingPointId=void 0,this.minDistance=void 0,this.keyEvents=void 0,this.cursors=void 0,this.preventPointsNearClose=void 0;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,this.preventPointsNearClose=e&&e.preventPointsNearClose||!0,this.minDistance=e&&e.minDistance||20,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.validate=null==e?void 0:e.validation}close(){if(void 0===this.currentId)return;const t=this.currentId;if(this.validate&&t){const e=this.store.getGeometryCopy(t);if(!this.validate({type:"Feature",id:t,geometry:e,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:W.Finish}))return}this.closingPointId&&this.store.delete([this.closingPointId]),this.startingClick=!1,this.currentId=void 0,this.closingPointId=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(void 0===this.currentId||!1===this.startingClick)return;const e=this.store.getGeometryCopy(this.currentId),o=e.coordinates[0].length-2,[r,s]=e.coordinates[0][o],{x:n,y:a}=this.project(r,s),l=i({x:n,y:a},{x:t.containerX,y:t.containerY}),[h,c]=e.coordinates[0][0],{x:d,y:u}=this.project(h,c);if(i({x:d,y:u},{x:t.containerX,y:t.containerY})<this.pointerDistance){if(this.setCursor(this.cursors.close),this.preventPointsNearClose)return}else this.setCursor(this.cursors.start);if(l<this.minDistance)return;e.coordinates[0].pop();const g={type:"Polygon",coordinates:[[...e.coordinates[0],[t.lng,t.lat],e.coordinates[0][0]]]};this.validate&&!this.validate({type:"Feature",id:this.currentId,geometry:g,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:W.Provisional})||this.store.updateGeometry([{id:this.currentId,geometry:g}])}onClick(t){if(!1===this.startingClick){const[e,i]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}},{geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:{mode:this.mode}}]);return this.currentId=e,this.closingPointId=i,this.startingClick=!0,void this.setDrawing()}this.close()}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentId,e=this.closingPointId;this.closingPointId=void 0,this.currentId=void 0,this.startingClick=!1,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t]),void 0!==e&&this.store.delete([e])}catch(t){}}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Polygon"===e.geometry.type&&e.properties.mode===this.mode?(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i):"Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode?(i.pointWidth=this.getNumericStylingValue(this.styles.closingPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.closingPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.closingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.closingPointOutlineWidth,2,e),i.zIndex=40,i):i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}}class dt{constructor({store:t,mode:e,project:i,unproject:o,pointerDistance:r,coordinatePrecision:s,projection:n}){this.store=void 0,this.mode=void 0,this.project=void 0,this.unproject=void 0,this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.projection=void 0,this.store=t,this.mode=e,this.project=i,this.unproject=o,this.pointerDistance=r,this.coordinatePrecision=s,this.projection=n}}function ut({unproject:t,point:e,pointerDistance:i}){const o=i/2,{x:r,y:s}=e;return{type:"Feature",properties:{},geometry:{type:"Polygon",coordinates:[[t(r-o,s-o),t(r+o,s-o),t(r+o,s+o),t(r-o,s+o),t(r-o,s-o)].map(t=>[t.lng,t.lat])]}}}class gt extends dt{constructor(t){super(t)}create(t){const{containerX:e,containerY:i}=t;return ut({unproject:this.unproject,point:{x:e,y:i},pointerDistance:this.pointerDistance})}}class pt extends dt{constructor(t){super(t)}measure(t,e){const{x:o,y:r}=this.project(e[0],e[1]);return i({x:o,y:r},{x:t.containerX,y:t.containerY})}}class yt extends dt{constructor(t,e,i){super(t),this.config=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.getSnappableCoordinateFirstClick=t=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode)),this.getSnappableCoordinate=(t,e)=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e)),this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),r={coord:void 0,minDist:Infinity};return o.forEach(e=>{let i;if("Polygon"===e.geometry.type)i=e.geometry.coordinates[0];else{if("LineString"!==e.geometry.type)return;i=e.geometry.coordinates}i.forEach(e=>{const i=this.pixelDistance.measure(t,e);i<r.minDist&&i<this.pointerDistance&&(r.coord=e,r.minDist=i)})}),r.coord}}function mt(t,e,i){const o=K(t[0]),r=K(t[1]),s=K(i),n=H(e),a=Math.asin(Math.sin(r)*Math.cos(n)+Math.cos(r)*Math.sin(n)*Math.cos(s));return[$(o+Math.atan2(Math.sin(s)*Math.sin(n)*Math.cos(r),Math.cos(n)-Math.sin(r)*Math.sin(a))),$(a)]}function ft({x:t,y:e},i,o){const r=K(o);return{x:t+i*Math.cos(r),y:e+i*Math.sin(r)}}function Ct(t,e){const i=K(t[0]),o=K(e[0]),r=K(t[1]),s=K(e[1]),n=Math.sin(o-i)*Math.cos(s),a=Math.cos(r)*Math.sin(s)-Math.sin(r)*Math.cos(s)*Math.cos(o-i);return $(Math.atan2(n,a))}function vt({x:t,y:e},{x:i,y:o}){let r=Math.atan2(o-e,i-t);return r*=180/Math.PI,r>180?r-=360:r<-180&&(r+=360),r}function Pt(t){return(t+360)%360}function _t(t,e,i){const o=[],r=t.length;let s,n,a,l=0;for(let r=0;r<t.length&&!(e>=l&&r===t.length-1);r++){if(l>e&&0===o.length){if(s=e-l,!s)return o.push(t[r]),o;n=Ct(t[r],t[r-1])-180,a=mt(t[r],s,n),o.push(a)}if(l>=i)return s=i-l,s?(n=Ct(t[r],t[r-1])-180,a=mt(t[r],s,n),o.push(a),o):(o.push(t[r]),o);if(l>=e&&o.push(t[r]),r===t.length-1)return o;l+=z(t[r],t[r+1])}if(l<e&&t.length===r)throw new Error("Start position is beyond line");const h=t[t.length-1];return[h,h]}function xt(t){return t*(Math.PI/180)}function St(t){return t*(180/Math.PI)}class Mt extends dt{constructor(t){super(t),this.config=void 0,this.config=t}generateInsertionCoordinates(t,e,i){const o=[t,e];let r=0;for(let t=0;t<o.length-1;t++)r+=z(o[0],o[1]);if(r<=i)return o;let s=r/i-1;Number.isInteger(s)||(s=Math.floor(s)+1);const n=[];for(let t=0;t<s;t++){const e=_t(o,i*t,i*(t+1));n.push(e)}const a=[];for(let t=0;t<n.length;t++)a.push(n[t][1]);return this.limitCoordinates(a)}generateInsertionGeodesicCoordinates(t,e,i){const o=z(t,e),r=function(t,e,i){const o=[],r=xt(t[1]),s=xt(t[0]),n=xt(e[1]),a=xt(e[0]);i+=1;const l=2*Math.asin(Math.sqrt(Math.sin((n-r)/2)**2+Math.cos(r)*Math.cos(n)*Math.sin((a-s)/2)**2));if(0===l||isNaN(l))return o;for(let t=0;t<=i;t++){const e=t/i,h=Math.sin((1-e)*l)/Math.sin(l),c=Math.sin(e*l)/Math.sin(l),d=h*Math.cos(r)*Math.cos(s)+c*Math.cos(n)*Math.cos(a),u=h*Math.cos(r)*Math.sin(s)+c*Math.cos(n)*Math.sin(a),g=h*Math.sin(r)+c*Math.sin(n);if(isNaN(d)||isNaN(u)||isNaN(g))continue;const p=Math.atan2(g,Math.sqrt(d**2+u**2)),y=Math.atan2(u,d);isNaN(p)||isNaN(y)||o.push([St(y),St(p)])}return o.slice(1,-1)}(t,e,Math.floor(o/i));return this.limitCoordinates(r)}limitCoordinates(t){return t.map(t=>[e(t[0],this.config.coordinatePrecision),e(t[1],this.config.coordinatePrecision)])}}function Et(t,e){return t[0]===e[0]&&t[1]===e[1]}class bt extends U{constructor(e){super(e),this.mode="linestring",this.currentCoordinate=0,this.currentId=void 0,this.closingPointId=void 0,this.keyEvents=void 0,this.snappingEnabled=void 0,this.cursors=void 0,this.mouseMove=!1,this.insertCoordinates=void 0,this.lastCommitedCoordinates=void 0,this.snapping=void 0,this.insertPoint=void 0;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,this.snappingEnabled=!(!e||void 0===e.snapping)&&e.snapping,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.validate=null==e?void 0:e.validation,this.insertCoordinates=null==e?void 0:e.insertCoordinates}close(){if(void 0===this.currentId)return;const t=this.store.getGeometryCopy(this.currentId);t.coordinates.pop(),this.updateGeometries([...t.coordinates],void 0,W.Commit);const e=this.currentId;this.closingPointId&&this.store.delete([this.closingPointId]),this.currentCoordinate=0,this.currentId=void 0,this.closingPointId=void 0,this.lastCommitedCoordinates=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(e,{mode:this.mode,action:"draw"})}updateGeometries(t,e,i){if(!this.currentId)return;const o={type:"LineString",coordinates:t};if(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i}))return;const r=[{id:this.currentId,geometry:o}];this.closingPointId&&e&&r.push({id:this.closingPointId,geometry:{type:"Point",coordinates:e}}),"commit"===i&&(this.lastCommitedCoordinates=o.coordinates),this.store.updateGeometry(r)}generateInsertCoordinates(t,e){if(!this.insertCoordinates||!this.lastCommitedCoordinates)throw new Error("Not able to insert coordinates");if("amount"!==this.insertCoordinates.strategy)throw new Error("Strategy does not exist");const i=z(t,e)/(this.insertCoordinates.value+1);let o=[];return"globe"===this.projection?o=this.insertPoint.generateInsertionGeodesicCoordinates(t,e,i):"web-mercator"===this.projection&&(o=this.insertPoint.generateInsertionCoordinates(t,e,i)),o}createLine(t){const[e]=this.store.create([{geometry:{type:"LineString",coordinates:[t,t]},properties:{mode:this.mode}}]);this.lastCommitedCoordinates=[t,t],this.currentId=e,this.currentCoordinate++,this.setDrawing()}firstUpdateToLine(t){if(!this.currentId)return;const e=this.store.getGeometryCopy(this.currentId).coordinates,[i]=this.store.create([{geometry:{type:"Point",coordinates:[...t]},properties:{mode:this.mode}}]);this.closingPointId=i,this.setCursor(this.cursors.close);const o=[...e,t];this.updateGeometries(o,void 0,W.Commit),this.currentCoordinate++}updateToLine(t,e){if(!this.currentId)return;const o=this.store.getGeometryCopy(this.currentId).coordinates,[r,s]=this.lastCommitedCoordinates?this.lastCommitedCoordinates[this.lastCommitedCoordinates.length-1]:o[o.length-2],{x:n,y:a}=this.project(r,s);if(i({x:n,y:a},{x:e.x,y:e.y})<this.pointerDistance)return void this.close();this.setCursor(this.cursors.close);const l=[...o,t];this.updateGeometries(l,o[o.length-1],W.Commit),this.currentCoordinate++}registerBehaviors(t){this.snapping=new yt(t,new pt(t),new gt(t)),this.insertPoint=new Mt(t)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;const e=this.store.getGeometryCopy(this.currentId).coordinates;e.pop();const o=this.snappingEnabled&&this.snapping.getSnappableCoordinate(t,this.currentId)||[t.lng,t.lat];if(this.closingPointId){const[o,r]=e[e.length-1],{x:s,y:n}=this.project(o,r);i({x:s,y:n},{x:t.containerX,y:t.containerY})<this.pointerDistance&&this.setCursor(this.cursors.close)}let r=[...e,o];if(this.insertCoordinates&&this.currentId&&this.lastCommitedCoordinates){const t=this.lastCommitedCoordinates[this.lastCommitedCoordinates.length-1],e=o;if(!Et(t,e)){const i=this.generateInsertCoordinates(t,e);r=[...this.lastCommitedCoordinates.slice(0,-1),...i,o]}}this.updateGeometries(r,void 0,W.Provisional)}onClick(t){this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1;const e=this.currentId&&this.snappingEnabled&&this.snapping.getSnappableCoordinate(t,this.currentId)||[t.lng,t.lat];0===this.currentCoordinate?this.createLine(e):1===this.currentCoordinate&&this.currentId?this.firstUpdateToLine(e):this.currentId&&this.updateToLine(e,{x:t.containerX,y:t.containerY})}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp(),t.key===this.keyEvents.finish&&this.close()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentId,e=this.closingPointId;this.closingPointId=void 0,this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t]),void 0!==e&&this.store.delete([e])}catch(t){}}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"LineString"===e.geometry.type&&e.properties.mode===this.mode?(i.lineStringColor=this.getHexColorStylingValue(this.styles.lineStringColor,i.lineStringColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.lineStringWidth,i.lineStringWidth,e),i.zIndex=10,i):"Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode?(i.pointColor=this.getHexColorStylingValue(this.styles.closingPointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.closingPointWidth,i.pointWidth,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.closingPointOutlineColor,"#ffffff",e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.closingPointOutlineWidth,2,e),i.zIndex=40,i):i}validateFeature(t){return!!super.validateFeature(t)&&"LineString"===t.geometry.type&&t.properties.mode===this.mode&&t.geometry.coordinates.length>=2}}function wt(t,e){return"Point"===t.geometry.type&&st(t.geometry.coordinates,e)}class It extends U{constructor(e){super(e),this.mode="point",this.cursors=void 0;const i={create:"crosshair"};this.cursors=e&&e.cursors?t({},i,e.cursors):i}start(){this.setStarted(),this.setCursor(this.cursors.create)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){if(!this.store)throw new Error("Mode must be registered first");const e={type:"Point",coordinates:[t.lng,t.lat]},i={mode:this.mode};if(this.validate&&!this.validate({type:"Feature",geometry:e,properties:i},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:W.Finish}))return;const[o]=this.store.create([{geometry:e,properties:i}]);this.onFinish(o,{mode:this.mode,action:"draw"})}onMouseMove(){}onKeyDown(){}onKeyUp(){}cleanUp(){}onDragStart(){}onDrag(){}onDragEnd(){}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode&&(i.pointWidth=this.getNumericStylingValue(this.styles.pointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.pointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.pointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.pointOutlineWidth,2,e),i.zIndex=30),i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&wt(t,this.coordinatePrecision)}}class Ft extends dt{constructor(t,e){super(t),this.config=void 0,this.pixelDistance=void 0,this._startEndPoints=[],this.config=t,this.pixelDistance=e}get ids(){return this._startEndPoints.concat()}set ids(t){}create(t,e){if(this.ids.length)throw new Error("Opening and closing points already created");if(t.length<=3)throw new Error("Requires at least 4 coordinates");this._startEndPoints=this.store.create([{geometry:{type:"Point",coordinates:t[0]},properties:{mode:e,[V]:!0}},{geometry:{type:"Point",coordinates:t[t.length-2]},properties:{mode:e,[V]:!0}}])}delete(){this.ids.length&&(this.store.delete(this.ids),this._startEndPoints=[])}update(t){if(2!==this.ids.length)throw new Error("No closing points to update");this.store.updateGeometry([{id:this.ids[0],geometry:{type:"Point",coordinates:t[0]}},{id:this.ids[1],geometry:{type:"Point",coordinates:t[t.length-3]}}])}isClosingPoint(t){const e=this.store.getGeometryCopy(this.ids[0]),i=this.store.getGeometryCopy(this.ids[1]),o=this.pixelDistance.measure(t,e.coordinates),r=this.pixelDistance.measure(t,i.coordinates);return{isClosing:o<this.pointerDistance,isPreviousClosing:r<this.pointerDistance}}}class Dt extends U{constructor(e){super(e),this.mode="polygon",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=void 0,this.snappingEnabled=void 0,this.snapping=void 0,this.pixelDistance=void 0,this.closingPoints=void 0,this.cursors=void 0,this.mouseMove=!1;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,this.snappingEnabled=!(!e||void 0===e.snapping)&&e.snapping,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}}close(){if(void 0===this.currentId)return;const t=this.store.getGeometryCopy(this.currentId).coordinates[0];if(t.length<5)return;if(!this.updatePolygonGeometry([...t.slice(0,-2),t[0]],W.Finish))return;const e=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.closingPoints.delete(),"drawing"===this.state&&this.setStarted(),this.onFinish(e,{mode:this.mode,action:"draw"})}registerBehaviors(t){this.pixelDistance=new pt(t),this.snapping=new yt(t,this.pixelDistance,new gt(t)),this.closingPoints=new Ft(t,this.pixelDistance)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0,i=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(e&&(t.lng=e[0],t.lat=e[1]),1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),r=Math.max(1e-6,e);o=[i[0],[t.lng,t.lat],[t.lng,t.lat-r],i[0]]}else if(2===this.currentCoordinate)o=[i[0],i[1],[t.lng,t.lat],i[0]];else{const{isClosing:e,isPreviousClosing:r}=this.closingPoints.isClosingPoint(t);r||e?(this.setCursor(this.cursors.close),o=[...i.slice(0,-2),i[0],i[0]]):o=[...i.slice(0,-2),[t.lng,t.lat],i[0]]}this.updatePolygonGeometry(o,W.Provisional)}updatePolygonGeometry(t,e){if(!this.currentId)return!1;const i={type:"Polygon",coordinates:[t]};return!(this.validate&&!this.validate({type:"Feature",geometry:i},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e})||(this.store.updateGeometry([{id:this.currentId,geometry:i}]),0))}onClick(t){if(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,0===this.currentCoordinate){const e=this.snappingEnabled?this.snapping.getSnappableCoordinateFirstClick(t):void 0;e&&(t.lng=e[0],t.lat=e[1]);const[i]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentId=i,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0;e&&(t.lng=e[0],t.lat=e[1]);const i=this.store.getGeometryCopy(this.currentId);if(Et([t.lng,t.lat],i.coordinates[0][0]))return;if(!this.updatePolygonGeometry([i.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]],W.Commit))return;this.currentCoordinate++}else if(2===this.currentCoordinate&&this.currentId){const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0;e&&(t.lng=e[0],t.lat=e[1]);const i=this.store.getGeometryCopy(this.currentId).coordinates[0];if(Et([t.lng,t.lat],i[1]))return;if(!this.updatePolygonGeometry([i[0],i[1],[t.lng,t.lat],[t.lng,t.lat],i[0]],W.Commit))return;2===this.currentCoordinate&&this.closingPoints.create(i,"polygon"),this.currentCoordinate++}else if(this.currentId){const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0,i=this.store.getGeometryCopy(this.currentId).coordinates[0],{isClosing:o,isPreviousClosing:r}=this.closingPoints.isClosingPoint(t);if(r||o)this.close();else{if(e&&(t.lng=e[0],t.lat=e[1]),Et([t.lng,t.lat],i[this.currentCoordinate-1]))return;const o=function(t=[[[0,0],[0,1],[1,1],[1,0],[0,0]]]){return{type:"Feature",geometry:{type:"Polygon",coordinates:t},properties:{}}}([[...i.slice(0,-1),[t.lng,t.lat],i[0]]]);if(!this.updatePolygonGeometry(o.geometry.coordinates[0],W.Commit))return;this.currentCoordinate++,this.closingPoints.ids.length&&this.closingPoints.update(o.geometry.coordinates[0])}}}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(){this.setCursor("unset")}onDrag(){}onDragEnd(){this.setCursor(this.cursors.start)}cleanUp(){const t=this.currentId;this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t]),this.closingPoints.ids.length&&this.closingPoints.delete()}catch(t){}}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});if(e.properties.mode===this.mode){if("Polygon"===e.geometry.type)return i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i;if("Point"===e.geometry.type)return i.pointWidth=this.getNumericStylingValue(this.styles.closingPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.closingPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.closingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.closingPointOutlineWidth,2,e),i.zIndex=30,i}return i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}}class kt extends U{constructor(e){super(e),this.mode="rectangle",this.center=void 0,this.clickCount=0,this.currentRectangleId=void 0,this.keyEvents=void 0,this.cursors=void 0;const i={start:"crosshair"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}}updateRectangle(t,e){if(1===this.clickCount&&this.center&&this.currentRectangleId){const i=this.store.getGeometryCopy(this.currentRectangleId).coordinates[0][0],o={type:"Polygon",coordinates:[[i,[t.lng,i[1]],[t.lng,t.lat],[i[0],t.lat],i]]};if(this.validate&&!this.validate({id:this.currentRectangleId,geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e}))return;this.store.updateGeometry([{id:this.currentRectangleId,geometry:o}])}}close(){const t=this.currentRectangleId;this.center=void 0,this.currentRectangleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted(),t&&this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){if(0===this.clickCount){this.center=[t.lng,t.lat];const[e]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentRectangleId=e,this.clickCount++,this.setDrawing()}else this.updateRectangle(t,W.Finish),this.close()}onMouseMove(t){this.updateRectangle(t,W.Provisional)}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentRectangleId;this.center=void 0,this.currentRectangleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted(),void 0!==t&&this.store.delete([t])}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Polygon"===e.geometry.type&&e.properties.mode===this.mode?(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i):i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&lt(t,this.coordinatePrecision)}}class Ot extends U{constructor(t){super({styles:t.styles}),this.type=R.Render,this.mode="render",this.mode=t.modeName}registerBehaviors(t){this.mode=t.mode}start(){this.setStarted()}stop(){this.setStopped()}onKeyUp(){}onKeyDown(){}onClick(){}onDragStart(){}onDrag(){}onDragEnd(){}onMouseMove(){}cleanUp(){}styleFeature(t){return{pointColor:this.getHexColorStylingValue(this.styles.pointColor,"#3f97e0",t),pointWidth:this.getNumericStylingValue(this.styles.pointWidth,6,t),pointOutlineColor:this.getHexColorStylingValue(this.styles.pointOutlineColor,"#ffffff",t),pointOutlineWidth:this.getNumericStylingValue(this.styles.pointOutlineWidth,0,t),polygonFillColor:this.getHexColorStylingValue(this.styles.polygonFillColor,"#3f97e0",t),polygonFillOpacity:this.getNumericStylingValue(this.styles.polygonFillOpacity,.3,t),polygonOutlineColor:this.getHexColorStylingValue(this.styles.polygonOutlineColor,"#3f97e0",t),polygonOutlineWidth:this.getNumericStylingValue(this.styles.polygonOutlineWidth,4,t),lineStringWidth:this.getNumericStylingValue(this.styles.lineStringWidth,4,t),lineStringColor:this.getHexColorStylingValue(this.styles.lineStringColor,"#3f97e0",t),zIndex:this.getNumericStylingValue(this.styles.zIndex,0,t)}}validateFeature(t){return super.validateFeature(t)&&(wt(t,this.coordinatePrecision)||at(t,this.coordinatePrecision)||function(t,e){return"LineString"===t.geometry.type&&t.geometry.coordinates.length>=2&&t.geometry.coordinates.every(t=>st(t,e))}(t,this.coordinatePrecision))}}function jt(t,e){const i=t,o=e,r=K(i[1]),s=K(o[1]);let n=K(o[0]-i[0]);n>Math.PI&&(n-=2*Math.PI),n<-Math.PI&&(n+=2*Math.PI);const a=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(r/2+Math.PI/4)),l=($(Math.atan2(n,a))+360)%360;return l>180?-(360-l):l}function Wt(t,e,i){let o=e;e<0&&(o=-Math.abs(o));const r=o/Y,s=t[0]*Math.PI/180,n=K(t[1]),a=K(i),l=r*Math.cos(a);let h=n+l;Math.abs(h)>Math.PI/2&&(h=h>0?Math.PI-h:-Math.PI-h);const c=Math.log(Math.tan(h/2+Math.PI/4)/Math.tan(n/2+Math.PI/4)),d=Math.abs(c)>1e-11?l/c:Math.cos(n),u=[(180*(s+r*Math.sin(a)/d)/Math.PI+540)%360-180,180*h/Math.PI];return u[0]+=u[0]-t[0]>180?-360:t[0]-u[0]>180?360:0,u}function Lt(t,i,o,r,s){const n=r(t[0],t[1]),a=r(i[0],i[1]),{lng:l,lat:h}=s((n.x+a.x)/2,(n.y+a.y)/2);return[e(l,o),e(h,o)]}function Bt(t,i,o){const r=Wt(t,1e3*z(t,i)/2,jt(t,i));return[e(r[0],o),e(r[1],o)]}function Gt({featureCoords:t,precision:e,unproject:i,project:o,projection:r}){const s=[];for(let n=0;n<t.length-1;n++){let a;if("web-mercator"===r)a=Lt(t[n],t[n+1],e,o,i);else{if("globe"!==r)throw new Error("Invalid projection");a=Bt(t[n],t[n+1],e)}s.push(a)}return s}class Nt extends dt{constructor(t,e){super(t),this.config=void 0,this.selectionPointBehavior=void 0,this._midPoints=[],this.config=t,this.selectionPointBehavior=e}get ids(){return this._midPoints.concat()}set ids(t){}insert(t,e){const i=this.store.getGeometryCopy(t),{midPointFeatureId:o,midPointSegment:r}=this.store.getPropertiesCopy(t),s=this.store.getGeometryCopy(o),n="Polygon"===s.type?s.coordinates[0]:s.coordinates;n.splice(r+1,0,i.coordinates),s.coordinates="Polygon"===s.type?[n]:n,this.store.updateGeometry([{id:o,geometry:s}]),this.store.delete([...this._midPoints,...this.selectionPointBehavior.ids]),this.create(n,o,e),this.selectionPointBehavior.create(n,s.type,o)}create(t,e,i){if(!this.store.has(e))throw new Error("Store does not have feature with this id");this._midPoints=this.store.create(function(t,e,i,o,r,s){return Gt({featureCoords:t,precision:i,project:o,unproject:r,projection:s}).map((t,i)=>({geometry:{type:"Point",coordinates:t},properties:e(i)}))}(t,t=>({mode:this.mode,[N]:!0,midPointSegment:t,midPointFeatureId:e}),i,this.config.project,this.config.unproject,this.projection))}delete(){this._midPoints.length&&(this.store.delete(this._midPoints),this._midPoints=[])}getUpdated(t){if(0!==this._midPoints.length)return Gt({featureCoords:t,precision:this.coordinatePrecision,project:this.config.project,unproject:this.config.unproject,projection:this.config.projection}).map((t,e)=>({id:this._midPoints[e],geometry:{type:"Point",coordinates:t}}))}}class Vt extends dt{constructor(t){super(t),this._selectionPoints=[]}get ids(){return this._selectionPoints.concat()}set ids(t){}create(t,e,i){this._selectionPoints=this.store.create(function(t,e,i){const o=[],r="Polygon"===e?t.length-1:t.length;for(let e=0;e<r;e++)o.push({geometry:{type:"Point",coordinates:t[e]},properties:i(e)});return o}(t,e,t=>({mode:this.mode,selectionPoint:!0,selectionPointFeatureId:i,index:t})))}delete(){this.ids.length&&(this.store.delete(this.ids),this._selectionPoints=[])}getUpdated(t){if(0!==this._selectionPoints.length)return this._selectionPoints.map((e,i)=>({id:e,geometry:{type:"Point",coordinates:t[i]}}))}getOneUpdated(t,e){if(void 0!==this._selectionPoints[t])return{id:this._selectionPoints[t],geometry:{type:"Point",coordinates:e}}}}function At(t,e){let i=!1;for(let n=0,a=e.length;n<a;n++){const a=e[n];for(let e=0,n=a.length,l=n-1;e<n;l=e++)(r=a[e])[1]>(o=t)[1]!=(s=a[l])[1]>o[1]&&o[0]<(s[0]-r[0])*(o[1]-r[1])/(s[1]-r[1])+r[0]&&(i=!i)}var o,r,s;return i}const Tt=(t,e,i)=>{const o=t=>t*t,r=(t,e)=>o(t.x-e.x)+o(t.y-e.y);return Math.sqrt(((t,e,i)=>{const o=r(e,i);if(0===o)return r(t,e);let s=((t.x-e.x)*(i.x-e.x)+(t.y-e.y)*(i.y-e.y))/o;return s=Math.max(0,Math.min(1,s)),r(t,{x:e.x+s*(i.x-e.x),y:e.y+s*(i.y-e.y)})})(t,e,i))};class Rt extends dt{constructor(t,e,i){super(t),this.config=void 0,this.createClickBoundingBox=void 0,this.pixelDistance=void 0,this.config=t,this.createClickBoundingBox=e,this.pixelDistance=i}find(t,e){let i,o,r,s,n=Infinity,a=Infinity,l=Infinity;const h=this.createClickBoundingBox.create(t),c=this.store.search(h);for(let h=0;h<c.length;h++){const d=c[h],u=d.geometry;if("Point"===u.type){if(d.properties.selectionPoint||!e&&d.properties[N])continue;const o=this.pixelDistance.measure(t,u.coordinates);d.properties[N]&&o<this.pointerDistance&&o<l?(l=o,r=d):!d.properties[N]&&o<this.pointerDistance&&o<n&&(n=o,i=d)}else if("LineString"===u.type){if(i)continue;for(let e=0;e<u.coordinates.length-1;e++){const i=u.coordinates[e],r=u.coordinates[e+1],s=Tt({x:t.containerX,y:t.containerY},this.project(i[0],i[1]),this.project(r[0],r[1]));s<this.pointerDistance&&s<a&&(a=s,o=d)}}else if("Polygon"===u.type){if(i||o)continue;At([t.lng,t.lat],u.coordinates)&&(s=d)}}return{clickedFeature:i||o||s,clickedMidPoint:r}}}class Ut extends dt{constructor(t,e,i,o){super(t),this.config=void 0,this.featuresAtMouseEvent=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.draggedFeatureId=null,this.dragPosition=void 0,this.config=t,this.featuresAtMouseEvent=e,this.selectionPoints=i,this.midPoints=o}startDragging(t,e){this.draggedFeatureId=e,this.dragPosition=[t.lng,t.lat]}stopDragging(){this.draggedFeatureId=null,this.dragPosition=void 0}isDragging(){return null!==this.draggedFeatureId}canDrag(t,e){const{clickedFeature:i}=this.featuresAtMouseEvent.find(t,!0);return!(!i||i.id!==e)}drag(t,i){if(!this.draggedFeatureId)return;const o=this.store.getGeometryCopy(this.draggedFeatureId),r=[t.lng,t.lat];if("Polygon"===o.type||"LineString"===o.type){let s,n;if("Polygon"===o.type?(s=o.coordinates[0],n=s.length-1):(s=o.coordinates,n=s.length),!this.dragPosition)return!1;for(let t=0;t<n;t++){const i=s[t],o=[this.dragPosition[0]-r[0],this.dragPosition[1]-r[1]],n=e(i[0]-o[0],this.config.coordinatePrecision),a=e(i[1]-o[1],this.config.coordinatePrecision);if(n>180||n<-180||a>90||a<-90)return!1;s[t]=[n,a]}"Polygon"===o.type&&(s[s.length-1]=[s[0][0],s[0][1]]);const a=this.selectionPoints.getUpdated(s)||[],l=this.midPoints.getUpdated(s)||[];if(i&&!i({type:"Feature",id:this.draggedFeatureId,geometry:o,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:W.Provisional}))return!1;this.store.updateGeometry([{id:this.draggedFeatureId,geometry:o},...a,...l]),this.dragPosition=[t.lng,t.lat]}else"Point"===o.type&&(this.store.updateGeometry([{id:this.draggedFeatureId,geometry:{type:"Point",coordinates:r}}]),this.dragPosition=[t.lng,t.lat])}}class Xt extends dt{constructor(t,e,i,o){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.draggedCoordinate={id:null,index:-1},this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o}getClosestCoordinate(t,e){const i={dist:Infinity,index:-1,isFirstOrLastPolygonCoord:!1};let o;if("LineString"===e.type)o=e.coordinates;else{if("Polygon"!==e.type)return i;o=e.coordinates[0]}for(let r=0;r<o.length;r++){const s=this.pixelDistance.measure(t,o[r]);if(s<this.pointerDistance&&s<i.dist){const t="Polygon"===e.type&&(r===o.length-1||0===r);i.dist=s,i.index=t?0:r,i.isFirstOrLastPolygonCoord=t}}return i}getDraggableIndex(t,e){const i=this.store.getGeometryCopy(e),o=this.getClosestCoordinate(t,i);return-1===o.index?-1:o.index}drag(t,e,i){if(!this.draggedCoordinate.id)return!1;const o=this.draggedCoordinate.index,r=this.store.getGeometryCopy(this.draggedCoordinate.id),s="LineString"===r.type?r.coordinates:r.coordinates[0],n=[t.lng,t.lat];if(t.lng>180||t.lng<-180||t.lat>90||t.lat<-90)return!1;if("Polygon"!==r.type||o!==s.length-1&&0!==o)s[o]=n;else{const t=s.length-1;s[0]=n,s[t]=n}const a=this.selectionPoints.getOneUpdated(o,n),l=a?[a]:[],h=this.midPoints.getUpdated(s)||[];return!("Point"!==r.type&&!e&&ot({type:"Feature",geometry:r,properties:{}})||i&&!i({type:"Feature",id:this.draggedCoordinate.id,geometry:r,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:W.Provisional})||(this.store.updateGeometry([{id:this.draggedCoordinate.id,geometry:r},...l,...h]),0))}isDragging(){return null!==this.draggedCoordinate.id}startDragging(t,e){this.draggedCoordinate={id:t,index:e}}stopDragging(){this.draggedCoordinate={id:null,index:-1}}}function zt(t){let e=0,i=0,o=0;return("Polygon"===t.geometry.type?t.geometry.coordinates[0].slice(0,-1):t.geometry.coordinates).forEach(t=>{e+=t[0],i+=t[1],o++},!0),[e/o,i/o]}function Yt(t,e){t[0]+=t[0]-e[0]>180?-360:e[0]-t[0]>180?360:0;const i=Y,o=e[1]*Math.PI/180,r=t[1]*Math.PI/180,s=r-o;let n=Math.abs(t[0]-e[0])*Math.PI/180;n>Math.PI&&(n-=2*Math.PI);const a=Math.log(Math.tan(r/2+Math.PI/4)/Math.tan(o/2+Math.PI/4)),l=Math.abs(a)>1e-11?s/a:Math.cos(o);return Math.sqrt(s*s+l*l*n*n)*i}function Kt(t){const e=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(t=>{const{x:e,y:i}=Q(t[0],t[1]);return[e,i]});return"Polygon"===t.geometry.type?function(t){let e=0,i=0,o=0;const r=t.length;for(let s=0;s<r-1;s++){const[r,n]=t[s],[a,l]=t[s+1],h=r*l-a*n;e+=h,i+=(r+a)*h,o+=(n+l)*h}return e/=2,i/=6*e,o/=6*e,{x:i,y:o}}(e):function(t){const e=t.length;let i=0,o=0;for(let r=0;r<e;r++){const[e,s]=t[r];i+=e,o+=s}return{x:i/e,y:o/e}}(e)}class Ht extends dt{constructor(t,e,i){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.lastBearing=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i}reset(){this.lastBearing=void 0}rotate(t,i,o){const r=this.store.getGeometryCopy(i);if("Polygon"!==r.type&&"LineString"!==r.type)return;const s=[t.lng,t.lat];let n;const a={type:"Feature",geometry:r,properties:{}};if("web-mercator"===this.config.projection){if(n=vt(Kt(a),Q(t.lng,t.lat)),!this.lastBearing)return void(this.lastBearing=n);((t,e)=>{if(0===e||360===e||-360===e)return t;const i=.017453292519943295*e,o=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(([t,e])=>Q(t,e)),r=o.reduce((t,e)=>({x:t.x+e.x,y:t.y+e.y}),{x:0,y:0});r.x/=o.length,r.y/=o.length;const s=o.map(t=>({x:r.x+(t.x-r.x)*Math.cos(i)-(t.y-r.y)*Math.sin(i),y:r.y+(t.x-r.x)*Math.sin(i)+(t.y-r.y)*Math.cos(i)})).map(({x:t,y:e})=>[tt(t,e).lng,tt(t,e).lat]);"Polygon"===t.geometry.type?t.geometry.coordinates[0]=s:t.geometry.coordinates=s})(a,-(this.lastBearing-n))}else{if("globe"!==this.config.projection)throw new Error("Unsupported projection");if(n=jt(zt({type:"Feature",geometry:r,properties:{}}),s),!this.lastBearing)return void(this.lastBearing=n+180);!function(t,e){if(0===e||360===e||-360===e)return t;const i=zt(t);("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{const o=jt(i,t)+e,r=Yt(i,t),s=Wt(i,r,o);t[0]=s[0],t[1]=s[1]})}(a,-(this.lastBearing-(n+180)))}const l="Polygon"===r.type?r.coordinates[0]:r.coordinates;l.forEach(t=>{t[0]=e(t[0],this.coordinatePrecision),t[1]=e(t[1],this.coordinatePrecision)});const h=this.midPoints.getUpdated(l)||[],c=this.selectionPoints.getUpdated(l)||[];if(o&&!o({id:i,type:"Feature",geometry:r,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:W.Provisional}))return!1;this.store.updateGeometry([{id:i,geometry:r},...c,...h]),"web-mercator"===this.projection?this.lastBearing=n:"globe"===this.projection&&(this.lastBearing=n+180)}}class $t extends dt{constructor(t,e,i){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.lastDistance=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i}reset(){this.lastDistance=void 0}scale(t,o,r){const s=this.store.getGeometryCopy(o);if("Polygon"!==s.type&&"LineString"!==s.type)return;const n=[t.lng,t.lat],a={type:"Feature",geometry:s,properties:{}};let l;const h=Kt(a);if("web-mercator"===this.config.projection){const e=Q(t.lng,t.lat);l=i(h,e)}else{if("globe"!==this.config.projection)throw new Error("Invalid projection");l=z(zt({type:"Feature",geometry:s,properties:{}}),n)}if(!this.lastDistance)return void(this.lastDistance=l);const c=1-(this.lastDistance-l)/l;if("web-mercator"===this.config.projection){const{lng:t,lat:e}=tt(h.x,h.y);!function(t,e,i){if(1===e)return t;const o=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(([t,e])=>Q(t,e)),r=Q(i[0],i[1]),s=o.map(t=>({x:r.x+(t.x-r.x)*e,y:r.y+(t.y-r.y)*e})).map(({x:t,y:e})=>[tt(t,e).lng,tt(t,e).lat]);"Polygon"===t.geometry.type?t.geometry.coordinates[0]=s:t.geometry.coordinates=s}(a,c,[t,e])}else"globe"===this.config.projection&&function(t,e,i,o="xy"){1===e||("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{const r=Yt(i,t),s=jt(i,t),n=Wt(i,r*e,s);"x"!==o&&"xy"!==o||(t[0]=n[0]),"y"!==o&&"xy"!==o||(t[1]=n[1])})}(a,c,zt(a));const d="Polygon"===s.type?s.coordinates[0]:s.coordinates;d.forEach(t=>{t[0]=e(t[0],this.coordinatePrecision),t[1]=e(t[1],this.coordinatePrecision)});const u=this.midPoints.getUpdated(d)||[],g=this.selectionPoints.getUpdated(d)||[];if(r&&!r({id:o,type:"Feature",geometry:s,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:W.Provisional}))return!1;this.store.updateGeometry([{id:o,geometry:s},...g,...u]),this.lastDistance=l}}class Jt extends dt{constructor(t,e,i,o){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.minimumScale=1e-4,this.draggedCoordinate={id:null,index:-1},this.boundingBoxMaps={opposite:{0:4,1:5,2:6,3:7,4:0,5:1,6:2,7:3}},this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o}getClosestCoordinate(t,e){const i={dist:Infinity,index:-1,isFirstOrLastPolygonCoord:!1};let o;if("LineString"===e.type)o=e.coordinates;else{if("Polygon"!==e.type)return i;o=e.coordinates[0]}for(let r=0;r<o.length;r++){const s=this.pixelDistance.measure(t,o[r]);if(s<this.pointerDistance&&s<i.dist){const t="Polygon"===e.type&&(r===o.length-1||0===r);i.dist=s,i.index=t?0:r,i.isFirstOrLastPolygonCoord=t}}return i}isValidDragWebMercator(t,e,i){switch(t){case 0:if(e<=0||i>=0)return!1;break;case 1:if(i>=0)return!1;break;case 2:if(e>=0||i>=0)return!1;break;case 3:if(e>=0)return!1;break;case 4:if(e>=0||i<=0)return!1;break;case 5:if(i<=0)return!1;break;case 6:if(e<=0||i<=0)return!1;break;case 7:if(e<=0)return!1}return!0}getSelectedFeatureDataWebMercator(){if(!this.draggedCoordinate.id||-1===this.draggedCoordinate.index)return null;const t=this.getFeature(this.draggedCoordinate.id);if(!t)return null;const e=this.getNormalisedCoordinates(t.geometry);return{boundingBox:this.getBBoxWebMercator(e),feature:t,updatedCoords:e,selectedCoordinate:e[this.draggedCoordinate.index]}}centerWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{feature:i,boundingBox:o,updatedCoords:r,selectedCoordinate:s}=e,n=Kt(i);if(!n)return null;const a=Q(s[0],s[1]),{closestBBoxIndex:l}=this.getIndexesWebMercator(o,a),h=Q(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:l,updatedCoords:r,webMercatorCursor:h,webMercatorSelected:a,webMercatorOrigin:n}),r}centerFixedWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{feature:i,boundingBox:o,updatedCoords:r,selectedCoordinate:s}=e,n=Kt(i);if(!n)return null;const a=Q(s[0],s[1]),{closestBBoxIndex:l}=this.getIndexesWebMercator(o,a),h=Q(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:l,updatedCoords:r,webMercatorCursor:h,webMercatorSelected:a,webMercatorOrigin:n}),r}scaleFixedWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:o,webMercatorCursor:r,updatedCoords:s}){if(!this.isValidDragWebMercator(t,e.x-r.x,e.y-r.y))return null;let n=i(e,r)/i(e,o);return n<0&&(n=this.minimumScale),this.performWebMercatorScale(s,e.x,e.y,n,n),s}oppositeFixedWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{boundingBox:i,updatedCoords:o,selectedCoordinate:r}=e,s=Q(r[0],r[1]),{oppositeBboxIndex:n,closestBBoxIndex:a}=this.getIndexesWebMercator(i,s),l={x:i[n][0],y:i[n][1]},h=Q(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:a,updatedCoords:o,webMercatorCursor:h,webMercatorSelected:s,webMercatorOrigin:l}),o}oppositeWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{boundingBox:i,updatedCoords:o,selectedCoordinate:r}=e,s=Q(r[0],r[1]),{oppositeBboxIndex:n,closestBBoxIndex:a}=this.getIndexesWebMercator(i,s),l={x:i[n][0],y:i[n][1]},h=Q(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:a,updatedCoords:o,webMercatorCursor:h,webMercatorSelected:s,webMercatorOrigin:l}),o}scaleWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:i,webMercatorCursor:o,updatedCoords:r}){const s=e.x-o.x,n=e.y-o.y;if(!this.isValidDragWebMercator(t,s,n))return null;let a=1;0!==s&&1!==t&&5!==t&&(a=1-(e.x-i.x-s)/s);let l=1;return 0!==n&&3!==t&&7!==t&&(l=1-(e.y-i.y-n)/n),this.validateScale(a,l)?(a<0&&(a=this.minimumScale),l<0&&(l=this.minimumScale),this.performWebMercatorScale(r,e.x,e.y,a,l),r):null}getFeature(t){if(null===this.draggedCoordinate.id)return null;const e=this.store.getGeometryCopy(t);return"Polygon"!==e.type&&"LineString"!==e.type?null:{type:"Feature",geometry:e,properties:{}}}getNormalisedCoordinates(t){return"Polygon"===t.type?t.coordinates[0]:t.coordinates}validateScale(t,e){const i=!isNaN(t)&&e<Number.MAX_SAFE_INTEGER,o=!isNaN(e)&&e<Number.MAX_SAFE_INTEGER;return i&&o}performWebMercatorScale(t,e,i,o,r){t.forEach(t=>{const{x:s,y:n}=Q(t[0],t[1]),a=e+(s-e)*o,l=i+(n-i)*r,{lng:h,lat:c}=tt(a,l);t[0]=h,t[1]=c})}getBBoxWebMercator(t){const e=[Infinity,Infinity,-Infinity,-Infinity];(t=t.map(t=>{const{x:e,y:i}=Q(t[0],t[1]);return[e,i]})).forEach(([t,i])=>{t<e[0]&&(e[0]=t),i<e[1]&&(e[1]=i),t>e[2]&&(e[2]=t),i>e[3]&&(e[3]=i)});const[i,o,r,s]=e;return[[i,s],[(i+r)/2,s],[r,s],[r,s+(o-s)/2],[r,o],[(i+r)/2,o],[i,o],[i,s+(o-s)/2]]}getIndexesWebMercator(t,e){let o,r=Infinity;for(let s=0;s<t.length;s++){const n=i({x:e.x,y:e.y},{x:t[s][0],y:t[s][1]});n<r&&(o=s,r=n)}if(void 0===o)throw new Error("No closest coordinate found");return{oppositeBboxIndex:this.boundingBoxMaps.opposite[o],closestBBoxIndex:o}}isDragging(){return null!==this.draggedCoordinate.id}startDragging(t,e){this.draggedCoordinate={id:t,index:e}}stopDragging(){this.draggedCoordinate={id:null,index:-1}}getDraggableIndex(t,e){const i=this.store.getGeometryCopy(e),o=this.getClosestCoordinate(t,i);return-1===o.index?-1:o.index}drag(t,i,o){if(!this.draggedCoordinate.id)return!1;const r=this.getFeature(this.draggedCoordinate.id);if(!r)return!1;let s=null;if("center"===i?s=this.centerWebMercatorDrag(t):"opposite"===i?s=this.oppositeWebMercatorDrag(t):"center-fixed"===i?s=this.centerFixedWebMercatorDrag(t):"opposite-fixed"===i&&(s=this.oppositeFixedWebMercatorDrag(t)),!s)return!1;for(let t=0;t<s.length;t++){const i=s[t];if(i[0]=e(i[0],this.coordinatePrecision),i[1]=e(i[1],this.coordinatePrecision),!st(i,this.coordinatePrecision))return!1}const n=this.midPoints.getUpdated(s)||[],a=this.selectionPoints.getUpdated(s)||[],l={type:r.geometry.type,coordinates:"Polygon"===r.geometry.type?[s]:s};return!(o&&!o({id:this.draggedCoordinate.id,type:"Feature",geometry:l,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:W.Provisional})||(this.store.updateGeometry([{id:this.draggedCoordinate.id,geometry:l},...a,...n]),0))}}class Zt extends X{constructor(e){var i;super(e),this.mode="select",this.allowManualDeselection=!0,this.dragEventThrottle=5,this.dragEventCount=0,this.selected=[],this.flags=void 0,this.keyEvents=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.featuresAtMouseEvent=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.dragFeature=void 0,this.dragCoordinate=void 0,this.rotateFeature=void 0,this.scaleFeature=void 0,this.dragCoordinateResizeFeature=void 0,this.cursors=void 0,this.validations={},this.flags=e&&e.flags?e.flags:{};const o={pointerOver:"move",dragStart:"move",dragEnd:"move",insertMidpoint:"crosshair"};if(this.cursors=e&&e.cursors?t({},o,e.cursors):o,null===(null==e?void 0:e.keyEvents))this.keyEvents={deselect:null,delete:null,rotate:null,scale:null};else{const i={deselect:"Escape",delete:"Delete",rotate:["Control","r"],scale:["Control","s"]};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}if(this.dragEventThrottle=e&&void 0!==e.dragEventThrottle&&e.dragEventThrottle||5,this.allowManualDeselection=null==(i=null==e?void 0:e.allowManualDeselection)||i,e&&e.flags&&e.flags)for(const t in e.flags){const i=e.flags[t].feature;i&&i.validation&&(this.validations[t]=i.validation)}}selectFeature(t){this.select(t,!1)}setSelecting(){if("started"!==this._state)throw new Error("Mode must be started to move to selecting state");this._state="selecting"}registerBehaviors(t){this.pixelDistance=new pt(t),this.clickBoundingBox=new gt(t),this.featuresAtMouseEvent=new Rt(t,this.clickBoundingBox,this.pixelDistance),this.selectionPoints=new Vt(t),this.midPoints=new Nt(t,this.selectionPoints),this.rotateFeature=new Ht(t,this.selectionPoints,this.midPoints),this.scaleFeature=new $t(t,this.selectionPoints,this.midPoints),this.dragFeature=new Ut(t,this.featuresAtMouseEvent,this.selectionPoints,this.midPoints),this.dragCoordinate=new Xt(t,this.pixelDistance,this.selectionPoints,this.midPoints),this.dragCoordinateResizeFeature=new Jt(t,this.pixelDistance,this.selectionPoints,this.midPoints)}deselectFeature(){this.deselect()}deselect(){const t=this.selected.filter(t=>this.store.has(t)).map(t=>({id:t,property:G,value:!1}));this.store.updateProperty(t),this.onDeselect(this.selected[0]),this.selected=[],this.selectionPoints.delete(),this.midPoints.delete()}deleteSelected(){this.store.delete(this.selected),this.selected=[]}onRightClick(t){if(!this.selectionPoints.ids.length)return;let e,i=Infinity;if(this.selectionPoints.ids.forEach(o=>{const r=this.store.getGeometryCopy(o),s=this.pixelDistance.measure(t,r.coordinates);s<this.pointerDistance&&s<i&&(i=s,e=this.store.getPropertiesCopy(o))}),!e)return;const o=e.selectionPointFeatureId,r=e.index,s=this.store.getPropertiesCopy(o),n=this.flags[s.mode],a=this.validations[s.mode];if(!(n&&n.feature&&n.feature.coordinates&&n.feature.coordinates.deletable))return;const l=this.store.getGeometryCopy(o);let h;if("Polygon"===l.type){if(h=l.coordinates[0],h.length<=4)return}else if("LineString"===l.type&&(h=l.coordinates,h.length<=3))return;if(h){if("Polygon"===l.type&&0===r||r===h.length-1?(h.shift(),h.pop(),h.push([h[0][0],h[0][1]])):h.splice(r,1),a&&!a({id:o,type:"Feature",geometry:l,properties:s},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:W.Commit}))return;this.store.delete([...this.midPoints.ids,...this.selectionPoints.ids]),this.store.updateGeometry([{id:o,geometry:l}]),this.selectionPoints.create(h,l.type,o),n&&n.feature&&n.feature.coordinates&&n.feature.coordinates.midpoints&&this.midPoints.create(h,o,this.coordinatePrecision)}}select(t,e=!0){if(this.selected[0]===t)return;const{mode:i}=this.store.getPropertiesCopy(t),o=this.flags[i];if(!o||!o.feature)return;const r=this.selected[0];if(r){if(r===t)return;this.deselect()}e&&this.setCursor(this.cursors.pointerOver),this.selected=[t],this.store.updateProperty([{id:t,property:"selected",value:!0}]),this.onSelect(t);const{type:s,coordinates:n}=this.store.getGeometryCopy(t);if("LineString"!==s&&"Polygon"!==s)return;const a="LineString"===s?n:n[0];a&&o&&o.feature.coordinates&&(this.selectionPoints.create(a,s,t),o.feature.coordinates.midpoints&&this.midPoints.create(a,t,this.coordinatePrecision))}onLeftClick(t){const{clickedFeature:e,clickedMidPoint:i}=this.featuresAtMouseEvent.find(t,this.selected.length>0);if(this.selected.length&&i)this.midPoints.insert(i.id,this.coordinatePrecision);else if(e&&e.id)this.select(e.id,!0);else if(this.selected.length&&this.allowManualDeselection)return void this.deselect()}start(){this.setStarted(),this.setSelecting()}stop(){this.cleanUp(),this.setStarted(),this.setStopped()}onClick(t){"right"!==t.button?"left"===t.button&&this.onLeftClick(t):this.onRightClick(t)}canScale(t){return this.keyEvents.scale&&this.keyEvents.scale.every(e=>t.heldKeys.includes(e))}canRotate(t){return this.keyEvents.rotate&&this.keyEvents.rotate.every(e=>t.heldKeys.includes(e))}preventDefaultKeyEvent(t){const e=this.canRotate(t),i=this.canScale(t);(e||i)&&t.preventDefault()}onKeyDown(t){this.preventDefaultKeyEvent(t)}onKeyUp(t){if(this.preventDefaultKeyEvent(t),this.keyEvents.delete&&t.key===this.keyEvents.delete){if(!this.selected.length)return;this.onDeselect(this.selected[0]),this.deleteSelected(),this.selectionPoints.delete(),this.midPoints.delete()}else this.keyEvents.deselect&&t.key===this.keyEvents.deselect&&this.cleanUp()}cleanUp(){this.selected.length&&this.deselect()}onDragStart(t,e){if(!this.selected.length)return;const i=this.store.getPropertiesCopy(this.selected[0]),o=this.flags[i.mode];if(!(o&&o.feature&&(o.feature.draggable||o.feature.coordinates&&o.feature.coordinates.draggable||o.feature.coordinates&&o.feature.coordinates.resizable)))return;this.dragEventCount=0;const r=this.selected[0],s=this.dragCoordinate.getDraggableIndex(t,r);return o&&o.feature&&o.feature.coordinates&&(o.feature.coordinates.draggable||o.feature.coordinates.resizable)&&-1!==s?(this.setCursor(this.cursors.dragStart),o.feature.coordinates.resizable?this.dragCoordinateResizeFeature.startDragging(r,s):this.dragCoordinate.startDragging(r,s),void e(!1)):o&&o.feature&&o.feature.draggable&&this.dragFeature.canDrag(t,r)?(this.setCursor(this.cursors.dragStart),this.dragFeature.startDragging(t,r),void e(!1)):void 0}onDrag(t,e){const i=this.selected[0];if(!i)return;const o=this.store.getPropertiesCopy(i),r=this.flags[o.mode],s=!0===(r&&r.feature&&r.feature.selfIntersectable);if(this.dragEventCount++,this.dragEventCount%this.dragEventThrottle==0)return;const n=this.validations[o.mode];if(r&&r.feature&&r.feature.rotateable&&this.canRotate(t))return e(!1),void this.rotateFeature.rotate(t,i,n);if(r&&r.feature&&r.feature.scaleable&&this.canScale(t))return e(!1),void this.scaleFeature.scale(t,i,n);if(this.dragCoordinateResizeFeature.isDragging()&&r.feature&&r.feature.coordinates&&r.feature.coordinates.resizable){if("globe"===this.projection)throw new Error("Globe is currently unsupported projection for resizable");return e(!1),void this.dragCoordinateResizeFeature.drag(t,r.feature.coordinates.resizable,n)}this.dragCoordinate.isDragging()?this.dragCoordinate.drag(t,s,n):this.dragFeature.isDragging()?this.dragFeature.drag(t,n):e(!0)}onDragEnd(t,e){this.setCursor(this.cursors.dragEnd),this.dragCoordinate.isDragging()?this.onFinish(this.selected[0],{mode:this.mode,action:"dragCoordinate"}):this.dragFeature.isDragging()?this.onFinish(this.selected[0],{mode:this.mode,action:"dragFeature"}):this.dragCoordinateResizeFeature.isDragging()&&this.onFinish(this.selected[0],{mode:this.mode,action:"dragCoordinateResize"}),this.dragCoordinate.stopDragging(),this.dragFeature.stopDragging(),this.dragCoordinateResizeFeature.stopDragging(),this.rotateFeature.reset(),this.scaleFeature.reset(),e(!0)}onMouseMove(t){if(!this.selected.length)return void this.setCursor("unset");if(this.dragFeature.isDragging())return;let e=!1;this.midPoints.ids.forEach(i=>{if(e)return;const o=this.store.getGeometryCopy(i);this.pixelDistance.measure(t,o.coordinates)<this.pointerDistance&&(e=!0)});let i=!1;if(this.selectionPoints.ids.forEach(o=>{const r=this.store.getGeometryCopy(o);this.pixelDistance.measure(t,r.coordinates)<this.pointerDistance&&(e=!1,i=!0)}),e)return void this.setCursor(this.cursors.insertMidpoint);const{clickedFeature:o}=this.featuresAtMouseEvent.find(t,!0);this.setCursor(this.selected.length>0&&(o&&o.id===this.selected[0]||i)?this.cursors.pointerOver:"unset")}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});if(e.properties.mode===this.mode&&"Point"===e.geometry.type){if(e.properties.selectionPoint)return i.pointColor=this.getHexColorStylingValue(this.styles.selectionPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectionPointOutlineColor,i.pointOutlineColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.selectionPointWidth,i.pointWidth,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectionPointOutlineWidth,2,e),i.zIndex=30,i;if(e.properties.midPoint)return i.pointColor=this.getHexColorStylingValue(this.styles.midPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.midPointOutlineColor,i.pointOutlineColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.midPointWidth,4,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.midPointOutlineWidth,2,e),i.zIndex=40,i}else if(e.properties[G]){if("Polygon"===e.geometry.type)return i.polygonFillColor=this.getHexColorStylingValue(this.styles.selectedPolygonColor,i.polygonFillColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.selectedPolygonOutlineWidth,i.polygonOutlineWidth,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.selectedPolygonOutlineColor,i.polygonOutlineColor,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.selectedPolygonFillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i;if("LineString"===e.geometry.type)return i.lineStringColor=this.getHexColorStylingValue(this.styles.selectedLineStringColor,i.lineStringColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.selectedLineStringWidth,i.lineStringWidth,e),i.zIndex=10,i;if("Point"===e.geometry.type)return i.pointWidth=this.getNumericStylingValue(this.styles.selectedPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.selectedPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectedPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectedPointOutlineWidth,i.pointOutlineWidth,e),i.zIndex=10,i}return i}}class qt extends U{constructor(...t){super(...t),this.type=R.Static,this.mode="static"}start(){}stop(){}onKeyUp(){}onKeyDown(){}onClick(){}onDragStart(){}onDrag(){}onDragEnd(){}onMouseMove(){}cleanUp(){}styleFeature(){return t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0})}}function Qt(t,e,i,o,r){for(;o>i;){if(o-i>600){const s=o-i+1,n=e-i+1,a=Math.log(s),l=.5*Math.exp(2*a/3),h=.5*Math.sqrt(a*l*(s-l)/s)*(n-s/2<0?-1:1);Qt(t,e,Math.max(i,Math.floor(e-n*l/s+h)),Math.min(o,Math.floor(e+(s-n)*l/s+h)),r)}const s=t[e];let n=i,a=o;for(te(t,i,e),r(t[o],s)>0&&te(t,i,o);n<a;){for(te(t,n,a),n++,a--;r(t[n],s)<0;)n++;for(;r(t[a],s)>0;)a--}0===r(t[i],s)?te(t,i,a):(a++,te(t,a,o)),a<=e&&(i=a+1),e<=a&&(o=a-1)}}function te(t,e,i){const o=t[e];t[e]=t[i],t[i]=o}function ee(t,e){ie(t,0,t.children.length,e,t)}function ie(t,e,i,o,r){r||(r=de([])),r.minX=Infinity,r.minY=Infinity,r.maxX=-Infinity,r.maxY=-Infinity;for(let s=e;s<i;s++){const e=t.children[s];oe(r,t.leaf?o(e):e)}return r}function oe(t,e){return t.minX=Math.min(t.minX,e.minX),t.minY=Math.min(t.minY,e.minY),t.maxX=Math.max(t.maxX,e.maxX),t.maxY=Math.max(t.maxY,e.maxY),t}function re(t,e){return t.minX-e.minX}function se(t,e){return t.minY-e.minY}function ne(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function ae(t){return t.maxX-t.minX+(t.maxY-t.minY)}function le(t,e){const i=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),r=Math.min(t.maxX,e.maxX),s=Math.min(t.maxY,e.maxY);return Math.max(0,r-i)*Math.max(0,s-o)}function he(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function ce(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function de(t){return{children:t,height:1,leaf:!0,minX:Infinity,minY:Infinity,maxX:-Infinity,maxY:-Infinity}}function ue(t,e,i,o,r){const s=[e,i];for(;s.length;){if((i=s.pop())-(e=s.pop())<=o)continue;const n=e+Math.ceil((i-e)/o/2)*o;Qt(t,n,e,i,r),s.push(e,n,n,i)}}class ge{constructor(t){this._maxEntries=void 0,this._minEntries=void 0,this.data=void 0,this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(.4*this._maxEntries)),this.clear()}search(t){let e=this.data;const i=[];if(!ce(t,e))return i;const o=this.toBBox,r=[];for(;e;){for(let s=0;s<e.children.length;s++){const n=e.children[s],a=e.leaf?o(n):n;ce(t,a)&&(e.leaf?i.push(n):he(t,a)?this._all(n,i):r.push(n))}e=r.pop()}return i}collides(t){let e=this.data;if(ce(t,e)){const i=[];for(;e;){for(let o=0;o<e.children.length;o++){const r=e.children[o],s=e.leaf?this.toBBox(r):r;if(ce(t,s)){if(e.leaf||he(t,s))return!0;i.push(r)}}e=i.pop()}}return!1}load(t){if(t.length<this._minEntries){for(let e=0;e<t.length;e++)this.insert(t[e]);return}let e=this._build(t.slice(),0,t.length-1,0);if(this.data.children.length)if(this.data.height===e.height)this._splitRoot(this.data,e);else{if(this.data.height<e.height){const t=this.data;this.data=e,e=t}this._insert(e,this.data.height-e.height-1,!0)}else this.data=e}insert(t){this._insert(t,this.data.height-1)}clear(){this.data=de([])}remove(t){let e=this.data;const i=this.toBBox(t),o=[],r=[];let s,n,a=!1;for(;e||o.length;){if(e||(e=o.pop(),n=o[o.length-1],s=r.pop(),a=!0),e.leaf){const i=e.children.indexOf(t);-1!==i&&(e.children.splice(i,1),o.push(e),this._condense(o))}a||e.leaf||!he(e,i)?n?(s++,e=n.children[s],a=!1):e=null:(o.push(e),r.push(s),s=0,n=e,e=e.children[0])}}toBBox(t){return t}compareMinX(t,e){return t.minX-e.minX}compareMinY(t,e){return t.minY-e.minY}_all(t,e){const i=[];for(;t;)t.leaf?e.push(...t.children):i.push(...t.children),t=i.pop();return e}_build(t,e,i,o){const r=i-e+1;let s,n=this._maxEntries;if(r<=n)return s=de(t.slice(e,i+1)),ee(s,this.toBBox),s;o||(o=Math.ceil(Math.log(r)/Math.log(n)),n=Math.ceil(r/Math.pow(n,o-1))),s=de([]),s.leaf=!1,s.height=o;const a=Math.ceil(r/n),l=a*Math.ceil(Math.sqrt(n));ue(t,e,i,l,this.compareMinX);for(let r=e;r<=i;r+=l){const e=Math.min(r+l-1,i);ue(t,r,e,a,this.compareMinY);for(let i=r;i<=e;i+=a){const r=Math.min(i+a-1,e);s.children.push(this._build(t,i,r,o-1))}}return ee(s,this.toBBox),s}_chooseSubtree(t,e,i,o){for(;o.push(e),!e.leaf&&o.length-1!==i;){let i,o=Infinity,n=Infinity;for(let a=0;a<e.children.length;a++){const l=e.children[a],h=ne(l),c=(r=t,s=l,(Math.max(s.maxX,r.maxX)-Math.min(s.minX,r.minX))*(Math.max(s.maxY,r.maxY)-Math.min(s.minY,r.minY))-h);c<n?(n=c,o=h<o?h:o,i=l):c===n&&h<o&&(o=h,i=l)}e=i||e.children[0]}var r,s;return e}_insert(t,e,i){const o=i?t:this.toBBox(t),r=[],s=this._chooseSubtree(o,this.data,e,r);for(s.children.push(t),oe(s,o);e>=0&&r[e].children.length>this._maxEntries;)this._split(r,e),e--;this._adjustParentBBoxes(o,r,e)}_split(t,e){const i=t[e],o=i.children.length,r=this._minEntries;this._chooseSplitAxis(i,r,o);const s=this._chooseSplitIndex(i,r,o),n=de(i.children.splice(s,i.children.length-s));n.height=i.height,n.leaf=i.leaf,ee(i,this.toBBox),ee(n,this.toBBox),e?t[e-1].children.push(n):this._splitRoot(i,n)}_splitRoot(t,e){this.data=de([t,e]),this.data.height=t.height+1,this.data.leaf=!1,ee(this.data,this.toBBox)}_chooseSplitIndex(t,e,i){let o,r=Infinity,s=Infinity;for(let n=e;n<=i-e;n++){const e=ie(t,0,n,this.toBBox),a=ie(t,n,i,this.toBBox),l=le(e,a),h=ne(e)+ne(a);l<r?(r=l,o=n,s=h<s?h:s):l===r&&h<s&&(s=h,o=n)}return o||i-e}_chooseSplitAxis(t,e,i){const o=t.leaf?this.compareMinX:re,r=t.leaf?this.compareMinY:se;this._allDistMargin(t,e,i,o)<this._allDistMargin(t,e,i,r)&&t.children.sort(o)}_allDistMargin(t,e,i,o){t.children.sort(o);const r=this.toBBox,s=ie(t,0,e,r),n=ie(t,i-e,i,r);let a=ae(s)+ae(n);for(let o=e;o<i-e;o++){const e=t.children[o];oe(s,t.leaf?r(e):e),a+=ae(s)}for(let o=i-e-1;o>=e;o--){const e=t.children[o];oe(n,t.leaf?r(e):e),a+=ae(n)}return a}_adjustParentBBoxes(t,e,i){for(let o=i;o>=0;o--)oe(e[o],t)}_condense(t){for(let e,i=t.length-1;i>=0;i--)0===t[i].children.length?i>0?(e=t[i-1].children,e.splice(e.indexOf(t[i]),1)):this.clear():ee(t[i],this.toBBox)}}class pe{constructor(t){this.tree=void 0,this.idToNode=void 0,this.nodeToId=void 0,this.tree=new ge(t&&t.maxEntries?t.maxEntries:9),this.idToNode=new Map,this.nodeToId=new Map}setMaps(t,e){this.idToNode.set(t.id,e),this.nodeToId.set(e,t.id)}toBBox(t){const e=[],i=[];let o;if("Polygon"===t.geometry.type)o=t.geometry.coordinates[0];else if("LineString"===t.geometry.type)o=t.geometry.coordinates;else{if("Point"!==t.geometry.type)throw new Error("Not a valid feature to turn into a bounding box");o=[t.geometry.coordinates]}for(let t=0;t<o.length;t++)i.push(o[t][1]),e.push(o[t][0]);const r=Math.min(...i),s=Math.max(...i);return{minX:Math.min(...e),minY:r,maxX:Math.max(...e),maxY:s}}insert(t){if(this.idToNode.get(String(t.id)))throw new Error("Feature already exists");const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}load(t){const e=[],i=new Set;t.forEach(t=>{const o=this.toBBox(t);if(this.setMaps(t,o),i.has(String(t.id)))throw new Error(`Duplicate feature ID found ${t.id}`);i.add(String(t.id)),e.push(o)}),this.tree.load(e)}update(t){this.remove(t.id);const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}remove(t){const e=this.idToNode.get(t);if(!e)throw new Error(`${t} not inserted into the spatial index`);this.tree.remove(e)}clear(){this.tree.clear()}search(t){return this.tree.search(this.toBBox(t)).map(t=>this.nodeToId.get(t))}collides(t){return this.tree.collides(this.toBBox(t))}}const ye={getId:()=>"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}),isValidId:t=>"string"==typeof t&&36===t.length};class me{constructor(t){this.idStrategy=void 0,this.tracked=void 0,this.spatialIndex=void 0,this.store=void 0,this._onChange=()=>{},this.store={},this.spatialIndex=new pe,this.tracked=!t||!1!==t.tracked,this.idStrategy=t&&t.idStrategy?t.idStrategy:ye}clone(t){return JSON.parse(JSON.stringify(t))}getId(){return this.idStrategy.getId()}has(t){return Boolean(this.store[t])}load(t,e){if(0===t.length)return;const i=this.clone(t);i.forEach(t=>{null==t.id&&(t.id=this.idStrategy.getId()),this.tracked&&(t.properties.createdAt?T(t.properties.createdAt):t.properties.createdAt=+new Date,t.properties.updatedAt?T(t.properties.updatedAt):t.properties.updatedAt=+new Date)});const o=[];i.forEach(t=>{const i=t.id;if(e&&!e(t))throw new Error(`Feature ${i} is not valid: ${JSON.stringify(t)}`);if(this.has(i))throw new Error(`Feature already exists with this id: ${i}`);this.store[i]=t,o.push(i)}),this.spatialIndex.load(i),this._onChange(o,"create")}search(t,e){const i=this.spatialIndex.search(t).map(t=>this.store[t]);return this.clone(e?i.filter(e):i)}registerOnChange(t){this._onChange=(e,i)=>{t(e,i)}}getGeometryCopy(t){const e=this.store[t];if(!e)throw new Error(`No feature with this id (${t}), can not get geometry copy`);return this.clone(e.geometry)}getPropertiesCopy(t){const e=this.store[t];if(!e)throw new Error(`No feature with this id (${t}), can not get properties copy`);return this.clone(e.properties)}updateProperty(t){const e=[];t.forEach(({id:t,property:i,value:o})=>{const r=this.store[t];if(!r)throw new Error(`No feature with this (${t}), can not update geometry`);e.push(t),r.properties[i]=o,this.tracked&&(r.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(e,"update")}updateGeometry(t){const e=[];t.forEach(({id:t,geometry:i})=>{e.push(t);const o=this.store[t];if(!o)throw new Error(`No feature with this (${t}), can not update geometry`);o.geometry=this.clone(i),this.spatialIndex.update(o),this.tracked&&(o.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(e,"update")}create(e){const i=[];return e.forEach(({geometry:e,properties:o})=>{let r,s=t({},o);this.tracked&&(r=+new Date,o?(s.createdAt="number"==typeof o.createdAt?o.createdAt:r,s.updatedAt="number"==typeof o.updatedAt?o.updatedAt:r):s={createdAt:r,updatedAt:r});const n=this.getId(),a={id:n,type:"Feature",geometry:e,properties:s};this.store[n]=a,this.spatialIndex.insert(a),i.push(n)}),this._onChange&&this._onChange([...i],"create"),i}delete(t){t.forEach(t=>{if(!this.store[t])throw new Error("No feature with this id, can not delete");delete this.store[t],this.spatialIndex.remove(t)}),this._onChange&&this._onChange([...t],"delete")}copyAll(){return this.clone(Object.keys(this.store).map(t=>this.store[t]))}clear(){this.store={},this.spatialIndex.clear()}size(){return Object.keys(this.store).length}}function fe(t){const e=t.coordinates;let i=0;if(e&&e.length>0){i+=Math.abs(Pe(e[0]));for(let t=1;t<e.length;t++)i-=Math.abs(Pe(e[t]))}return i}const Ce=Y*Y/2,ve=Math.PI/180;function Pe(t){const e=t.length;if(e<=2)return 0;let i=0,o=0;for(;o<e;)i+=(t[o+2>=e?(o+2)%e:o+2][0]*ve-t[o][0]*ve)*Math.sin(t[o+1===e?0:o+1][1]*ve),o++;return i*Ce}const _e=(t,e)=>"Polygon"===t.geometry.type&&fe(t.geometry)>e,xe=(t,e)=>"Polygon"===t.geometry.type&&fe(t.geometry)<e,Se=t=>("Polygon"===t.geometry.type||"LineString"===t.geometry.type)&&!ot(t);function Me(t,e,i){const o=vt(t,e);let r=vt(e,i)-o;return r<0&&(r+=360),180-Math.abs(r-90-90)}class Ee extends U{constructor(e){super(e),this.mode="angled-rectangle",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=void 0,this.cursors=void 0,this.mouseMove=!1;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}}close(){if(void 0===this.currentId)return;const t=this.currentId;this.currentCoordinate=0,this.currentId=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;const e=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(1===this.currentCoordinate){const i=1/Math.pow(10,this.coordinatePrecision-1),r=Math.max(1e-6,i);o=[e[0],[t.lng,t.lat],[t.lng,t.lat-r],e[0]]}else if(2===this.currentCoordinate){const r=e[0],s=e[1],n=Lt(r,s,this.coordinatePrecision,this.project,this.unproject),a=Q(r[0],r[1]),l=Q(n[0],n[1]),h=Q(s[0],s[1]),c=Q(t.lng,t.lat),d=i(c,a)<i(c,h),u=Me(a,l,c),g=d?90-u:Me(a,l,c)-90,p=i(l,c),y=Math.cos(K(g))*p,m=vt(a,h)+("right"===function(t,e,i){const o=(i.x-e.x)*(t.y-e.y)-(i.y-e.y)*(t.x-e.x);return o>1e-10?"left":o<-1e-10?"right":"left"}(a,h,c)?-90:90),f=ft(a,y,m),C=ft(h,y,m),v=tt(f.x,f.y),P=tt(C.x,C.y);o=[e[0],e[1],[P.lng,P.lat],[v.lng,v.lat],e[0]]}o&&this.updatePolygonGeometry(this.currentId,o,W.Provisional)}updatePolygonGeometry(t,e,i){const o={type:"Polygon",coordinates:[e]};return!(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i})||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,0===this.currentCoordinate){const[e]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const e=this.store.getGeometryCopy(this.currentId);if(Et([t.lng,t.lat],e.coordinates[0][0]))return;if(!this.updatePolygonGeometry(this.currentId,[e.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],e.coordinates[0][0]],W.Commit))return;this.currentCoordinate++}else 2===this.currentCoordinate&&this.currentId&&this.close()}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return e.properties.mode===this.mode&&"Polygon"===e.geometry.type&&(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10),i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}}function be(t,e,i){return(e.x-t.x)*(i.y-t.y)-(e.y-t.y)*(i.x-t.x)<=0}class we extends U{constructor(e){super(e),this.mode="sector",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=void 0,this.direction=void 0,this.arcPoints=void 0,this.cursors=void 0,this.mouseMove=!1;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.arcPoints=(null==e?void 0:e.arcPoints)||64}close(){if(void 0===this.currentId)return;const t=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.direction=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;const o=this.store.getGeometryCopy(this.currentId).coordinates[0];let r;if(1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),i=Math.max(1e-6,e);r=[o[0],[t.lng,t.lat],[t.lng,t.lat-i],o[0]]}else if(2===this.currentCoordinate){const s=o[0],n=o[1],a=[t.lng,t.lat],l=Q(s[0],s[1]),h=Q(n[0],n[1]),c=Q(a[0],a[1]);if(void 0===this.direction){const t=be(l,h,c);this.direction=t?"clockwise":"anticlockwise"}const d=i(l,h),u=vt(l,h),g=vt(l,c),p=this.arcPoints,y=[s],m=Pt(u),f=Pt(g);let C;"anticlockwise"===this.direction?(C=f-m,C<0&&(C+=360)):(C=m-f,C<0&&(C+=360));const v=("anticlockwise"===this.direction?1:-1)*C/p;y.push(n);for(let t=0;t<=p;t++){const i=ft(l,d,m+t*v),{lng:o,lat:r}=tt(i.x,i.y),s=[e(o,this.coordinatePrecision),e(r,this.coordinatePrecision)];s[0]!==y[y.length-1][0]&&s[1]!==y[y.length-1][1]&&y.push(s)}y.push(s),r=[...y]}r&&this.updatePolygonGeometry(this.currentId,r,W.Provisional)}updatePolygonGeometry(t,e,i){const o={type:"Polygon",coordinates:[e]};return!(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i})||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,0===this.currentCoordinate){const[e]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const e=this.store.getGeometryCopy(this.currentId);if(Et([t.lng,t.lat],e.coordinates[0][0]))return;if(!this.updatePolygonGeometry(this.currentId,[e.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],e.coordinates[0][0]],W.Commit))return;this.currentCoordinate++}else 2===this.currentCoordinate&&this.currentId&&this.close()}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentId=void 0,this.direction=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return e.properties.mode===this.mode&&"Polygon"===e.geometry.type&&(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10),i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}}class Ie extends U{constructor(e){super(e),this.mode="sensor",this.currentCoordinate=0,this.currentId=void 0,this.currentInitialArcId=void 0,this.currentStartingPointId=void 0,this.keyEvents=void 0,this.direction=void 0,this.arcPoints=void 0,this.cursors=void 0,this.mouseMove=!1;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.arcPoints=(null==e?void 0:e.arcPoints)||64}close(){if(void 0===this.currentStartingPointId)return;const t=this.currentStartingPointId,e=this.currentInitialArcId,i=this.currentId;t&&this.store.delete([t]),e&&this.store.delete([e]),this.currentCoordinate=0,this.currentStartingPointId=void 0,this.currentInitialArcId=void 0,this.currentId=void 0,this.direction=void 0,"drawing"===this.state&&this.setStarted(),i&&this.onFinish(i,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0!==this.currentInitialArcId&&void 0!==this.currentStartingPointId&&0!==this.currentCoordinate)if(2===this.currentCoordinate){const o=this.store.getGeometryCopy(this.currentInitialArcId).coordinates,r=this.store.getGeometryCopy(this.currentStartingPointId).coordinates,s=o[0],n=[t.lng,t.lat],a=Q(s[0],s[1]),l=Q(n[0],n[1]),h=Q(r[0],r[1]),c=i(h,a);if(void 0===this.direction){const t=be(h,a,l);this.direction=t?"clockwise":"anticlockwise"}const d=vt(h,a),u=vt(h,l),g=this.arcPoints,p=[s],y=Pt(d),m=Pt(u);let f;"anticlockwise"===this.direction?(f=m-y,f<0&&(f+=360)):(f=y-m,f<0&&(f+=360));const C=("anticlockwise"===this.direction?1:-1)*f/g;for(let t=0;t<=g;t++){const i=ft(h,c,y+t*C),{lng:o,lat:r}=tt(i.x,i.y),s=[e(o,this.coordinatePrecision),e(r,this.coordinatePrecision)];s[0]!==p[p.length-1][0]&&s[1]!==p[p.length-1][1]&&p.push(s)}this.updateLineStringGeometry(this.currentInitialArcId,p,W.Provisional)}else if(3===this.currentCoordinate){const o=this.store.getGeometryCopy(this.currentInitialArcId).coordinates;if(o.length<2)return;if(!this.direction)return;const r=this.store.getGeometryCopy(this.currentStartingPointId).coordinates,s=o[0],n=o[o.length-1],a=Q(t.lng,t.lat),l=Q(s[0],s[1]),h=Q(n[0],n[1]),c=Q(r[0],r[1]),d=i(c,l),u=i(c,a)<d?l:a,g=vt(c,a),p=vt(c,l),y=vt(c,h),m=Pt(p),f=Pt(y),C=Pt(g);if(this.notInSector({normalizedCursor:C,normalizedStart:m,normalizedEnd:f,direction:this.direction}))return;const v=this.getDeltaBearing(this.direction,m,f),P=this.arcPoints,_=("anticlockwise"===this.direction?1:-1)*v/P,x=i(c,u),S=[];for(let t=0;t<=P;t++){const i=ft(c,x,m+t*_),{lng:r,lat:s}=tt(i.x,i.y),n=[e(r,this.coordinatePrecision),e(s,this.coordinatePrecision)];n[0]!==o[o.length-1][0]&&n[1]!==o[o.length-1][1]&&S.unshift(n)}o.push(...S),o.push(o[0]),this.currentId?this.updatePolygonGeometry(this.currentId,o,W.Provisional):[this.currentId]=this.store.create([{geometry:{type:"Polygon",coordinates:[o]},properties:{mode:this.mode}}])}}updateLineStringGeometry(t,e,i){const o={type:"LineString",coordinates:e};return!(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i})||(this.store.updateGeometry([{id:t,geometry:o}]),0))}updatePolygonGeometry(t,e,i){const o={type:"Polygon",coordinates:[e]};return!(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i})||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,0===this.currentCoordinate){const[e]=this.store.create([{geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:{mode:this.mode}}]);this.currentStartingPointId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentStartingPointId){const[e]=this.store.create([{geometry:{type:"LineString",coordinates:[[t.lng,t.lat],[t.lng,t.lat]]},properties:{mode:this.mode}}]);this.currentInitialArcId=e,this.currentCoordinate++}else 2===this.currentCoordinate&&this.currentStartingPointId?this.currentCoordinate++:3===this.currentCoordinate&&this.currentStartingPointId&&this.close()}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentStartingPointId&&this.store.delete([this.currentStartingPointId]),this.currentInitialArcId&&this.store.delete([this.currentInitialArcId]),this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentStartingPointId=void 0,this.direction=void 0,this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return e.properties.mode===this.mode&&("Polygon"===e.geometry.type?(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10):"LineString"===e.geometry.type?(i.lineStringColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.zIndex=10):"Point"===e.geometry.type&&(i.pointColor=this.getHexColorStylingValue(this.styles.centerPointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.centerPointWidth,i.pointWidth,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.centerPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.centerPointOutlineWidth,i.pointOutlineWidth,e),i.zIndex=20)),i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}getDeltaBearing(t,e,i){let o;return"anticlockwise"===t?(o=i-e,o<0&&(o+=360)):(o=e-i,o<0&&(o+=360)),o}notInSector({normalizedCursor:t,normalizedStart:e,normalizedEnd:i,direction:o}){return"clockwise"===o?e<=i?t>=e&&t<=i:t>=e||t<=i:e>=i?t<=e&&t>=i:t<=e||t>=i}}var Fe={__proto__:null,TerraDrawBaseDrawMode:U,TerraDrawBaseAdapter:r};class De{constructor(e){this._modes=void 0,this._mode=void 0,this._adapter=void 0,this._enabled=!1,this._store=void 0,this._eventListeners=void 0,this._instanceSelectMode=void 0,this._adapter=e.adapter,this._mode=new qt;const i=new Set,o=e.modes.reduce((t,e)=>{if(i.has(e.mode))throw new Error(`There is already a ${e.mode} mode provided`);return i.add(e.mode),t[e.mode]=e,t},{}),r=Object.keys(o);if(0===r.length)throw new Error("No modes provided");r.forEach(t=>{if(o[t].type===R.Select){if(this._instanceSelectMode)throw new Error("only one type of select mode can be provided");this._instanceSelectMode=t}}),this._modes=t({},o,{static:this._mode}),this._eventListeners={change:[],select:[],deselect:[],finish:[],ready:[]},this._store=new me({tracked:!!e.tracked,idStrategy:e.idStrategy?e.idStrategy:void 0});const s=t=>{const e=[],i=this._store.copyAll().filter(i=>!t.includes(i.id)||(e.push(i),!1));return{changed:e,unchanged:i}},n=(t,e)=>{this._enabled&&this._eventListeners.finish.forEach(i=>{i(t,e)})},a=(t,e)=>{if(!this._enabled)return;this._eventListeners.change.forEach(i=>{i(t,e)});const{changed:i,unchanged:o}=s(t);"create"===e?this._adapter.render({created:i,deletedIds:[],unchanged:o,updated:[]},this.getModeStyles()):"update"===e?this._adapter.render({created:[],deletedIds:[],unchanged:o,updated:i},this.getModeStyles()):"delete"===e?this._adapter.render({created:[],deletedIds:t,unchanged:o,updated:[]},this.getModeStyles()):"styling"===e&&this._adapter.render({created:[],deletedIds:[],unchanged:o,updated:[]},this.getModeStyles())},l=t=>{if(!this._enabled)return;this._eventListeners.select.forEach(e=>{e(t)});const{changed:e,unchanged:i}=s([t]);this._adapter.render({created:[],deletedIds:[],unchanged:i,updated:e},this.getModeStyles())},h=t=>{if(!this._enabled)return;this._eventListeners.deselect.forEach(t=>{t()});const{changed:e,unchanged:i}=s([t]);e&&this._adapter.render({created:[],deletedIds:[],unchanged:i,updated:e},this.getModeStyles())};Object.keys(this._modes).forEach(t=>{this._modes[t].register({mode:t,store:this._store,setCursor:this._adapter.setCursor.bind(this._adapter),project:this._adapter.project.bind(this._adapter),unproject:this._adapter.unproject.bind(this._adapter),setDoubleClickToZoom:this._adapter.setDoubleClickToZoom.bind(this._adapter),onChange:a,onSelect:l,onDeselect:h,onFinish:n,coordinatePrecision:this._adapter.getCoordinatePrecision()})})}checkEnabled(){if(!this._enabled)throw new Error("Terra Draw is not enabled")}getModeStyles(){const t={};return Object.keys(this._modes).forEach(e=>{t[e]=t=>this._instanceSelectMode&&t.properties[G]?this._modes[this._instanceSelectMode].styleFeature.bind(this._modes[this._instanceSelectMode])(t):this._modes[e].styleFeature.bind(this._modes[e])(t)}),t}featuresAtLocation({lng:t,lat:e},o){const r=o&&void 0!==o.pointerDistance?o.pointerDistance:30,s=!o||void 0===o.ignoreSelectFeatures||o.ignoreSelectFeatures,n=this._adapter.unproject.bind(this._adapter),a=this._adapter.project.bind(this._adapter),l=a(t,e),h=ut({unproject:n,point:l,pointerDistance:r});return this._store.search(h).filter(o=>{if(s&&(o.properties[N]||o.properties.selectionPoint))return!1;if("Point"===o.geometry.type){const t=o.geometry.coordinates,e=a(t[0],t[1]);return i(l,e)<r}if("LineString"===o.geometry.type){const t=o.geometry.coordinates;for(let e=0;e<t.length-1;e++){const i=t[e],o=t[e+1];if(Tt(l,a(i[0],i[1]),a(o[0],o[1]))<r)return!0}return!1}return!!At([t,e],o.geometry.coordinates)||void 0})}getSelectMode(){if(this.checkEnabled(),!this._instanceSelectMode)throw new Error("No select mode defined in instance");return this.getMode()!==this._instanceSelectMode&&this.setMode(this._instanceSelectMode),this._modes[this._instanceSelectMode]}setModeStyles(t,e){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._modes[t].styles=e}getSnapshot(){return this._store.copyAll()}clear(){this.checkEnabled(),this._adapter.clear()}get enabled(){return this._enabled}set enabled(t){throw new Error("Enabled is read only")}getMode(){return this._mode.mode}setMode(t){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._mode.stop(),this._mode=this._modes[t],this._mode.start()}removeFeatures(t){this.checkEnabled(),this._store.delete(t)}selectFeature(t){this.getSelectMode().selectFeature(t)}deselectFeature(t){this.getSelectMode().deselectFeature(t)}getFeatureId(){return this._store.getId()}hasFeature(t){return this._store.has(t)}addFeatures(t){this.checkEnabled(),0!==t.length&&this._store.load(t,t=>{if(Boolean(t&&"object"==typeof t&&"properties"in t&&"object"==typeof t.properties&&null!==t.properties&&"mode"in t.properties)){const e=this._modes[t.properties.mode];return!!e&&e.validateFeature.bind(e)(t)}return!1})}start(){this._enabled=!0,this._adapter.register({onReady:()=>{this._eventListeners.ready.forEach(t=>{t()})},getState:()=>this._mode.state,onClick:t=>{this._mode.onClick(t)},onMouseMove:t=>{this._mode.onMouseMove(t)},onKeyDown:t=>{this._mode.onKeyDown(t)},onKeyUp:t=>{this._mode.onKeyUp(t)},onDragStart:(t,e)=>{this._mode.onDragStart(t,e)},onDrag:(t,e)=>{this._mode.onDrag(t,e)},onDragEnd:(t,e)=>{this._mode.onDragEnd(t,e)},onClear:()=>{this._mode.cleanUp(),this._store.clear()}})}getFeaturesAtLngLat(t,e){const{lng:i,lat:o}=t;return this.featuresAtLocation({lng:i,lat:o},e)}getFeaturesAtPointerEvent(t,e){const i=this._adapter.getLngLatFromEvent.bind(this._adapter)(t);return null===i?[]:this.featuresAtLocation(i,e)}stop(){this._enabled=!1,this._adapter.unregister()}on(t,e){const i=this._eventListeners[t];i.includes(e)||i.push(e)}off(t,e){const i=this._eventListeners[t];i.includes(e)&&i.splice(i.indexOf(e),1)}}export{De as TerraDraw,Ee as TerraDrawAngledRectangleMode,B as TerraDrawArcGISMapsSDKAdapter,ht as TerraDrawCircleMode,Fe as TerraDrawExtend,ct as TerraDrawFreehandMode,s as TerraDrawGoogleMapsAdapter,n as TerraDrawLeafletAdapter,bt as TerraDrawLineStringMode,l as TerraDrawMapLibreGLAdapter,a as TerraDrawMapboxGLAdapter,L as TerraDrawOpenLayersAdapter,It as TerraDrawPointMode,Dt as TerraDrawPolygonMode,kt as TerraDrawRectangleMode,Ot as TerraDrawRenderMode,we as TerraDrawSectorMode,Zt as TerraDrawSelectMode,Ie as TerraDrawSensorMode,xe as ValidateMaxAreaSquareMeters,_e as ValidateMinAreaSquareMeters,Se as ValidateNotSelfIntersecting};
1
+ function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var o in i)Object.prototype.hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t},t.apply(this,arguments)}function e(t,e=9){const i=Math.pow(10,e);return Math.round(t*i)/i}const i=(t,e)=>{const{x:i,y:o}=t,{x:r,y:s}=e,n=r-i,a=s-o;return Math.sqrt(a*a+n*n)};class o{constructor({name:t,callback:e,unregister:i,register:o}){this.name=void 0,this.callback=void 0,this.registered=!1,this.register=void 0,this.unregister=void 0,this.name=t,this.register=()=>{this.registered||(this.registered=!0,o(e))},this.unregister=()=>{this.register&&(this.registered=!1,i(e))},this.callback=e}}class r{constructor(t){this._minPixelDragDistance=void 0,this._minPixelDragDistanceDrawing=void 0,this._minPixelDragDistanceSelecting=void 0,this._lastDrawEvent=void 0,this._coordinatePrecision=void 0,this._heldKeys=new Set,this._listeners=[],this._dragState="not-dragging",this._currentModeCallbacks=void 0,this._minPixelDragDistance="number"==typeof t.minPixelDragDistance?t.minPixelDragDistance:1,this._minPixelDragDistanceSelecting="number"==typeof t.minPixelDragDistanceSelecting?t.minPixelDragDistanceSelecting:1,this._minPixelDragDistanceDrawing="number"==typeof t.minPixelDragDistanceDrawing?t.minPixelDragDistanceDrawing:8,this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9}getButton(t){return-1===t.button?"neither":0===t.button?"left":1===t.button?"middle":2===t.button?"right":"neither"}getMapElementXYPosition(t){const e=this.getMapEventElement(),{left:i,top:o}=e.getBoundingClientRect();return{containerX:t.clientX-i,containerY:t.clientY-o}}getDrawEventFromEvent(t){const i=this.getLngLatFromEvent(t);if(!i)return null;const{lng:o,lat:r}=i,{containerX:s,containerY:n}=this.getMapElementXYPosition(t),a=this.getButton(t),l=Array.from(this._heldKeys);return{lng:e(o,this._coordinatePrecision),lat:e(r,this._coordinatePrecision),containerX:s,containerY:n,button:a,heldKeys:l}}register(t){this._currentModeCallbacks=t,this._listeners=this.getAdapterListeners(),this._listeners.forEach(t=>{t.register()})}getCoordinatePrecision(){return this._coordinatePrecision}getAdapterListeners(){return[new o({name:"pointerdown",callback:t=>{if(!this._currentModeCallbacks)return;if(!t.isPrimary)return;const e=this.getDrawEventFromEvent(t);e&&(this._dragState="pre-dragging",this._lastDrawEvent=e)},register:t=>{this.getMapEventElement().addEventListener("pointerdown",t)},unregister:t=>{this.getMapEventElement().removeEventListener("pointerdown",t)}}),new o({name:"pointermove",callback:t=>{if(!this._currentModeCallbacks)return;if(!t.isPrimary)return;t.preventDefault();const e=this.getDrawEventFromEvent(t);if(e)if("not-dragging"===this._dragState)this._currentModeCallbacks.onMouseMove(e),this._lastDrawEvent=e;else if("pre-dragging"===this._dragState){if(!this._lastDrawEvent)return;const t={x:this._lastDrawEvent.containerX,y:this._lastDrawEvent.containerY},o={x:e.containerX,y:e.containerY},r=this._currentModeCallbacks.getState(),s=i(t,o);let n=!1;if(n="drawing"===r?s<this._minPixelDragDistanceDrawing:"selecting"===r?s<this._minPixelDragDistanceSelecting:s<this._minPixelDragDistance,n)return;this._dragState="dragging",this._currentModeCallbacks.onDragStart(e,t=>{this.setDraggability.bind(this)(t)})}else"dragging"===this._dragState&&this._currentModeCallbacks.onDrag(e,t=>{this.setDraggability.bind(this)(t)})},register:t=>{this.getMapEventElement().addEventListener("pointermove",t)},unregister:t=>{this.getMapEventElement().removeEventListener("pointermove",t)}}),new o({name:"contextmenu",callback:t=>{this._currentModeCallbacks&&t.preventDefault()},register:t=>{this.getMapEventElement().addEventListener("contextmenu",t)},unregister:t=>{this.getMapEventElement().removeEventListener("contextmenu",t)}}),new o({name:"pointerup",callback:t=>{if(!this._currentModeCallbacks)return;if(t.target!==this.getMapEventElement())return;if(!t.isPrimary)return;const e=this.getDrawEventFromEvent(t);e&&("dragging"===this._dragState?this._currentModeCallbacks.onDragEnd(e,t=>{this.setDraggability.bind(this)(t)}):"not-dragging"!==this._dragState&&"pre-dragging"!==this._dragState||this._currentModeCallbacks.onClick(e),this._dragState="not-dragging",this.setDraggability(!0))},register:t=>{this.getMapEventElement().addEventListener("pointerup",t)},unregister:t=>{this.getMapEventElement().removeEventListener("pointerup",t)}}),new o({name:"keyup",callback:t=>{this._currentModeCallbacks&&(this._heldKeys.delete(t.key),this._currentModeCallbacks.onKeyUp({key:t.key,heldKeys:Array.from(this._heldKeys),preventDefault:()=>t.preventDefault()}))},register:t=>{this.getMapEventElement().addEventListener("keyup",t)},unregister:t=>{this.getMapEventElement().removeEventListener("keyup",t)}}),new o({name:"keydown",callback:t=>{this._currentModeCallbacks&&(this._heldKeys.add(t.key),this._currentModeCallbacks.onKeyDown({key:t.key,heldKeys:Array.from(this._heldKeys),preventDefault:()=>t.preventDefault()}))},register:t=>{this.getMapEventElement().addEventListener("keydown",t)},unregister:t=>{this.getMapEventElement().removeEventListener("keydown",t)}})]}unregister(){this._listeners.forEach(t=>{t.unregister()}),this.clear()}}class s extends r{constructor(t){if(super(t),this._cursor=void 0,this._cursorStyleSheet=void 0,this._lib=void 0,this._map=void 0,this._overlay=void 0,this._clickEventListener=void 0,this._mouseMoveEventListener=void 0,this.renderedFeatureIds=new Set,this._lib=t.lib,this._map=t.map,!this._map.getDiv().id)throw new Error("Google Map container div requires and id to be set");this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9}get _layers(){var t;return Boolean((null==(t=this.renderedFeatureIds)?void 0:t.size)>0)}circlePath(t,e,i){const o=2*i;return`M ${t} ${e} m -${i}, 0 a ${i},${i} 0 1,0 ${o},0 a ${i},${i} 0 1,0 -${o},0`}register(t){super.register(t),this._overlay=new this._lib.OverlayView,this._overlay.draw=function(){},this._overlay.onAdd=()=>{this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()},this._overlay.setMap(this._map),this._clickEventListener=this._map.data.addListener("click",t=>{const e=this._listeners.find(({name:t})=>"click"===t);e&&e.callback(t)}),this._mouseMoveEventListener=this._map.data.addListener("mousemove",t=>{const e=this._listeners.find(({name:t})=>"mousemove"===t);e&&e.callback(t)})}unregister(){var t,e,i;super.unregister(),null==(t=this._clickEventListener)||t.remove(),null==(e=this._mouseMoveEventListener)||e.remove(),null==(i=this._overlay)||i.setMap(null),this._overlay=void 0}getLngLatFromEvent(t){if(!this._overlay)throw new Error("cannot get overlay");const e=this._map.getBounds();if(!e)return null;const i=e.getNorthEast(),o=e.getSouthWest(),r=new this._lib.LatLngBounds(o,i),s=this._map.getDiv(),n=t.clientX-s.getBoundingClientRect().left,a=t.clientY-s.getBoundingClientRect().top,l=new this._lib.Point(n,a),h=this._overlay.getProjection();if(!h)return null;const c=h.fromContainerPixelToLatLng(l);return c&&r.contains(c)?{lng:c.lng(),lat:c.lat()}:null}getMapEventElement(){return this._map.getDiv().querySelector('div[style*="z-index: 3;"]')}project(t,e){if(!this._overlay)throw new Error("cannot get overlay");if(void 0===this._map.getBounds())throw new Error("cannot get bounds");const i=this._overlay.getProjection();if(void 0===i)throw new Error("cannot get projection");const o=i.fromLatLngToContainerPixel(new this._lib.LatLng(e,t));if(null===o)throw new Error("cannot project coordinates");return{x:o.x,y:o.y}}unproject(t,e){if(!this._overlay)throw new Error("cannot get overlay");const i=this._overlay.getProjection();if(void 0===i)throw new Error("cannot get projection");const o=i.fromContainerPixelToLatLng(new this._lib.Point(t,e));if(null===o)throw new Error("cannot unproject coordinates");return{lng:o.lng(),lat:o.lat()}}setCursor(t){if(t!==this._cursor){if(this._cursorStyleSheet&&(this._cursorStyleSheet.remove(),this._cursorStyleSheet=void 0),"unset"!==t){const e=this._map.getDiv(),i=document.querySelector(`#${e.id} .gm-style > div`);if(i){i.classList.add("terra-draw-google-maps");const e=document.createElement("style");e.innerHTML=`.terra-draw-google-maps { cursor: ${t} !important; }`,document.getElementsByTagName("head")[0].appendChild(e),this._cursorStyleSheet=e}}this._cursor=t}}setDoubleClickToZoom(t){this._map.setOptions(t?{disableDoubleClickZoom:!1}:{disableDoubleClickZoom:!0})}setDraggability(t){this._map.setOptions({draggable:t})}render(t,e){this._layers&&(t.deletedIds.forEach(t=>{const e=this._map.data.getFeatureById(t);e&&(this._map.data.remove(e),this.renderedFeatureIds.delete(t))}),t.updated.forEach(t=>{if(!t||!t.id)throw new Error("Feature is not valid");const e=this._map.data.getFeatureById(t.id);if(!e)throw new Error("Feature could not be found by Google Maps API");switch(e.forEachProperty((t,i)=>{e.setProperty(i,void 0)}),Object.keys(t.properties).forEach(i=>{e.setProperty(i,t.properties[i])}),t.geometry.type){case"Point":{const i=t.geometry.coordinates;e.setGeometry(new this._lib.Data.Point(new this._lib.LatLng(i[1],i[0])))}break;case"LineString":{const i=t.geometry.coordinates,o=[];for(let t=0;t<i.length;t++){const e=i[t],r=new this._lib.LatLng(e[1],e[0]);o.push(r)}e.setGeometry(new this._lib.Data.LineString(o))}break;case"Polygon":{const i=t.geometry.coordinates,o=[];for(let t=0;t<i.length;t++){const e=[];for(let o=0;o<i[t].length;o++){const r=new this._lib.LatLng(i[t][o][1],i[t][o][0]);e.push(r)}o.push(e)}e.setGeometry(new this._lib.Data.Polygon(o))}}}),t.created.forEach(t=>{this.renderedFeatureIds.add(t.id),this._map.data.addGeoJson(t)})),t.created.forEach(t=>{this.renderedFeatureIds.add(t.id)});const i={type:"FeatureCollection",features:[...t.created]};this._map.data.addGeoJson(i),this._map.data.setStyle(t=>{const i=t.getProperty("mode"),o=t.getGeometry();if(!o)throw new Error("Google Maps geometry not found");const r=o.getType(),s={};t.forEachProperty((t,e)=>{s[e]=t});const n=e[i]({type:"Feature",geometry:{type:r,coordinates:[]},properties:s});switch(r){case"Point":return{clickable:!1,icon:{path:this.circlePath(0,0,n.pointWidth),fillColor:n.pointColor,fillOpacity:1,strokeColor:n.pointOutlineColor,strokeWeight:n.pointOutlineWidth,rotation:0,scale:1}};case"LineString":return{strokeColor:n.lineStringColor,strokeWeight:n.lineStringWidth};case"Polygon":return{strokeColor:n.polygonOutlineColor,strokeWeight:n.polygonOutlineWidth,fillOpacity:n.polygonFillOpacity,fillColor:n.polygonFillColor}}throw Error("Unknown feature type")})}clearLayers(){this._layers&&(this._map.data.forEach(t=>{const e=t.getId();this.renderedFeatureIds.has(e)&&this._map.data.remove(t)}),this.renderedFeatureIds=new Set)}clear(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this.clearLayers())}getCoordinatePrecision(){return super.getCoordinatePrecision()}}class n extends r{constructor(t){super(t),this._lib=void 0,this._map=void 0,this._panes={},this._container=void 0,this._layers={},this._lib=t.lib,this._map=t.map,this._container=this._map.getContainer()}createPaneStyleSheet(t,e){const i=document.createElement("style");return i.innerHTML=`.leaflet-${t}-pane {z-index: ${e+600}`,document.getElementsByTagName("head")[0].appendChild(i),this._map.createPane(t),i}clearPanes(){Object.values(this._panes).forEach(t=>{t&&t.remove()}),this._panes={}}clearLayers(){Object.values(this._layers).forEach(t=>{this._map.removeLayer(t)}),this._layers={}}styleGeoJSONLayer(t){return{pointToLayer:(e,i)=>{if(!e.properties)throw new Error("Feature has no properties");if("string"!=typeof e.properties.mode)throw new Error("Feature mode is not a string");const o=(0,t[e.properties.mode])(e),r=String(o.zIndex);return this._panes[r]||(this._panes[r]=this.createPaneStyleSheet(r,o.zIndex)),this._lib.circleMarker(i,{radius:o.pointWidth,stroke:o.pointOutlineWidth||!1,color:o.pointOutlineColor,weight:o.pointOutlineWidth,fillOpacity:.8,fillColor:o.pointColor,pane:r,interactive:!1})},style:e=>{if(!e||!e.properties)return{};const i=e,o=(0,t[i.properties.mode])(i),r=String(o.zIndex);return this._panes[r]||(this._panes[r]=this.createPaneStyleSheet(r,o.zIndex)),"LineString"===i.geometry.type?{interactive:!1,color:o.lineStringColor,weight:o.lineStringWidth,pane:r}:"Polygon"===i.geometry.type?{interactive:!1,fillOpacity:o.polygonFillOpacity,fillColor:o.polygonFillColor,weight:o.polygonOutlineWidth,stroke:!0,color:o.polygonFillColor,pane:r}:{}}}}getLngLatFromEvent(t){const{containerX:e,containerY:i}=this.getMapElementXYPosition(t),o=this._map.containerPointToLatLng({x:e,y:i});return null===o.lng||isNaN(o.lng)||null===o.lat||isNaN(o.lat)?null:{lng:o.lng,lat:o.lat}}getMapEventElement(){return this._container}setDraggability(t){t?this._map.dragging.enable():this._map.dragging.disable()}project(t,e){const{x:i,y:o}=this._map.latLngToContainerPoint({lng:t,lat:e});return{x:i,y:o}}unproject(t,e){const{lng:i,lat:o}=this._map.containerPointToLatLng({x:t,y:e});return{lng:i,lat:o}}setCursor(t){"unset"===t?this.getMapEventElement().style.removeProperty("cursor"):this.getMapEventElement().style.cursor=t}setDoubleClickToZoom(t){t?this._map.doubleClickZoom.enable():this._map.doubleClickZoom.disable()}render(t,e){t.created.forEach(t=>{this._layers[t.id]=this._lib.geoJSON(t,this.styleGeoJSONLayer(e)),this._map.addLayer(this._layers[t.id])}),t.deletedIds.forEach(t=>{this._map.removeLayer(this._layers[t])}),t.updated.forEach(t=>{this._map.removeLayer(this._layers[t.id]),this._layers[t.id]=this._lib.geoJSON(t,this.styleGeoJSONLayer(e)),this._map.addLayer(this._layers[t.id])})}clear(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this.clearLayers(),this.clearPanes())}register(t){super.register(t),this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()}getCoordinatePrecision(){return super.getCoordinatePrecision()}unregister(){return super.unregister()}}class a extends r{constructor(t){super(t),this._nextRender=void 0,this._map=void 0,this._container=void 0,this._rendered=!1,this.changedIds={deletion:!1,points:!1,linestrings:!1,polygons:!1,styling:!1},this._map=t.map,this._container=this._map.getContainer()}clearLayers(){this._rendered&&(["point","linestring","polygon"].forEach(t=>{const e=`td-${t.toLowerCase()}`;this._map.removeLayer(e),"polygon"===t&&this._map.removeLayer(e+"-outline"),this._map.removeSource(e)}),this._rendered=!1,this._nextRender&&(cancelAnimationFrame(this._nextRender),this._nextRender=void 0))}_addGeoJSONSource(t,e){this._map.addSource(t,{type:"geojson",data:{type:"FeatureCollection",features:e},tolerance:0})}_addFillLayer(t){return this._map.addLayer({id:t,source:t,type:"fill",paint:{"fill-color":["get","polygonFillColor"],"fill-opacity":["get","polygonFillOpacity"]}})}_addFillOutlineLayer(t){return this._map.addLayer({id:t+"-outline",source:t,type:"line",paint:{"line-width":["get","polygonOutlineWidth"],"line-color":["get","polygonOutlineColor"]}})}_addLineLayer(t){return this._map.addLayer({id:t,source:t,type:"line",paint:{"line-width":["get","lineStringWidth"],"line-color":["get","lineStringColor"]}})}_addPointLayer(t){return this._map.addLayer({id:t,source:t,type:"circle",paint:{"circle-stroke-color":["get","pointOutlineColor"],"circle-stroke-width":["get","pointOutlineWidth"],"circle-radius":["get","pointWidth"],"circle-color":["get","pointColor"]}})}_addLayer(t,e){"Point"===e&&this._addPointLayer(t),"LineString"===e&&this._addLineLayer(t),"Polygon"===e&&(this._addFillLayer(t),this._addFillOutlineLayer(t))}_addGeoJSONLayer(t,e){const i=`td-${t.toLowerCase()}`;return this._addGeoJSONSource(i,e),this._addLayer(i,t),i}_setGeoJSONLayerData(t,e){const i=`td-${t.toLowerCase()}`;return this._map.getSource(i).setData({type:"FeatureCollection",features:e}),i}updateChangedIds(t){[...t.updated,...t.created].forEach(t=>{"Point"===t.geometry.type?this.changedIds.points=!0:"LineString"===t.geometry.type?this.changedIds.linestrings=!0:"Polygon"===t.geometry.type&&(this.changedIds.polygons=!0)}),t.deletedIds.length>0&&(this.changedIds.deletion=!0),0===t.created.length&&0===t.updated.length&&0===t.deletedIds.length&&(this.changedIds.styling=!0)}getLngLatFromEvent(t){const{left:e,top:i}=this._container.getBoundingClientRect();return this.unproject(t.clientX-e,t.clientY-i)}getMapEventElement(){return this._map.getCanvas()}setDraggability(t){t?(this._map.dragRotate.enable(),this._map.dragPan.enable()):(this._map.dragRotate.disable(),this._map.dragPan.disable())}project(t,e){const{x:i,y:o}=this._map.project({lng:t,lat:e});return{x:i,y:o}}unproject(t,e){const{lng:i,lat:o}=this._map.unproject({x:t,y:e});return{lng:i,lat:o}}setCursor(t){const e=this._map.getCanvas();"unset"===t?e.style.removeProperty("cursor"):e.style.cursor=t}setDoubleClickToZoom(t){t?this._map.doubleClickZoom.enable():this._map.doubleClickZoom.disable()}render(t,e){this.updateChangedIds(t),this._nextRender&&cancelAnimationFrame(this._nextRender),this._nextRender=requestAnimationFrame(()=>{const i=[...t.created,...t.updated,...t.unchanged],o=[],r=[],s=[];for(let t=0;t<i.length;t++){const n=i[t],{properties:a}=n,l=e[a.mode](n);"Point"===n.geometry.type?(a.pointColor=l.pointColor,a.pointOutlineColor=l.pointOutlineColor,a.pointOutlineWidth=l.pointOutlineWidth,a.pointWidth=l.pointWidth,o.push(n)):"LineString"===n.geometry.type?(a.lineStringColor=l.lineStringColor,a.lineStringWidth=l.lineStringWidth,r.push(n)):"Polygon"===n.geometry.type&&(a.polygonFillColor=l.polygonFillColor,a.polygonFillOpacity=l.polygonFillOpacity,a.polygonOutlineColor=l.polygonOutlineColor,a.polygonOutlineWidth=l.polygonOutlineWidth,s.push(n))}if(this._rendered){const t=this.changedIds.deletion||this.changedIds.styling,e=t||this.changedIds.linestrings,i=t||this.changedIds.polygons;let n;(t||this.changedIds.points)&&(n=this._setGeoJSONLayerData("Point",o)),e&&this._setGeoJSONLayerData("LineString",r),i&&this._setGeoJSONLayerData("Polygon",s),n&&this._map.moveLayer(n)}else{const t=this._addGeoJSONLayer("Point",o);this._addGeoJSONLayer("LineString",r),this._addGeoJSONLayer("Polygon",s),this._rendered=!0,t&&this._map.moveLayer(t)}this.changedIds={points:!1,linestrings:!1,polygons:!1,deletion:!1,styling:!1}})}clear(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this.clearLayers())}getCoordinatePrecision(){return super.getCoordinatePrecision()}unregister(){return super.unregister()}register(t){super.register(t),this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()}}class l extends r{constructor(t){super(t),this.mapboxglAdapter=void 0,this.mapboxglAdapter=new a(t)}register(t){this.mapboxglAdapter.register(t)}unregister(){this.mapboxglAdapter.unregister()}getCoordinatePrecision(){return this.mapboxglAdapter.getCoordinatePrecision()}getLngLatFromEvent(t){return this.mapboxglAdapter.getLngLatFromEvent(t)}getMapEventElement(){return this.mapboxglAdapter.getMapEventElement()}setDraggability(t){this.mapboxglAdapter.setDraggability(t)}project(t,e){return this.mapboxglAdapter.project(t,e)}unproject(t,e){return this.mapboxglAdapter.unproject(t,e)}setCursor(t){this.mapboxglAdapter.setCursor(t)}setDoubleClickToZoom(t){this.mapboxglAdapter.setDoubleClickToZoom(t)}render(t,e){this.mapboxglAdapter.render(t,e)}clear(){this.mapboxglAdapter.clear()}}const h={radians:6370997/(2*Math.PI),degrees:2*Math.PI*6370997/360,ft:.3048,m:1,"us-ft":1200/3937};var c=class{constructor(t){this.code_=t.code,this.units_=t.units,this.extent_=void 0!==t.extent?t.extent:null,this.worldExtent_=void 0!==t.worldExtent?t.worldExtent:null,this.axisOrientation_=void 0!==t.axisOrientation?t.axisOrientation:"enu",this.global_=void 0!==t.global&&t.global,this.canWrapX_=!(!this.global_||!this.extent_),this.getPointResolutionFunc_=t.getPointResolution,this.defaultTileGrid_=null,this.metersPerUnit_=t.metersPerUnit}canWrapX(){return this.canWrapX_}getCode(){return this.code_}getExtent(){return this.extent_}getUnits(){return this.units_}getMetersPerUnit(){return this.metersPerUnit_||h[this.units_]}getWorldExtent(){return this.worldExtent_}getAxisOrientation(){return this.axisOrientation_}isGlobal(){return this.global_}setGlobal(t){this.global_=t,this.canWrapX_=!(!t||!this.extent_)}getDefaultTileGrid(){return this.defaultTileGrid_}setDefaultTileGrid(t){this.defaultTileGrid_=t}setExtent(t){this.extent_=t,this.canWrapX_=!(!this.global_||!t)}setWorldExtent(t){this.worldExtent_=t}setGetPointResolution(t){this.getPointResolutionFunc_=t}getPointResolutionFunc(){return this.getPointResolutionFunc_}};const d=6378137,u=Math.PI*d,g=[-u,-u,u,u],p=[-180,-85,180,85],y=d*Math.log(Math.tan(Math.PI/2));class m extends c{constructor(t){super({code:t,units:"m",extent:g,global:!0,worldExtent:p,getPointResolution:function(t,e){return t/Math.cosh(e[1]/d)}})}}const f=[new m("EPSG:3857"),new m("EPSG:102100"),new m("EPSG:102113"),new m("EPSG:900913"),new m("http://www.opengis.net/def/crs/EPSG/0/3857"),new m("http://www.opengis.net/gml/srs/epsg.xml#3857")],C=[-180,-90,180,90],v=6378137*Math.PI/180;class P extends c{constructor(t,e){super({code:t,units:"degrees",extent:C,axisOrientation:e,global:!0,metersPerUnit:v,worldExtent:C})}}const _=[new P("CRS:84"),new P("EPSG:4326","neu"),new P("urn:ogc:def:crs:OGC:1.3:CRS84"),new P("urn:ogc:def:crs:OGC:2:84"),new P("http://www.opengis.net/def/crs/OGC/1.3/CRS84"),new P("http://www.opengis.net/gml/srs/epsg.xml#4326","neu"),new P("http://www.opengis.net/def/crs/EPSG/0/4326","neu")];let x={},S={};function M(t,e,i){const o=t.getCode(),r=e.getCode();o in S||(S[o]={}),S[o][r]=i}function E(t,e){if(void 0!==e)for(let i=0,o=t.length;i<o;++i)e[i]=t[i];else e=t.slice();return e}function b(t,e){if(void 0!==e&&t!==e){for(let i=0,o=t.length;i<o;++i)e[i]=t[i];t=e}return t}function w(t){!function(t,e){x[t]=e}(t.getCode(),t),M(t,t,E)}function I(t){return"string"==typeof t?x[e=t]||x[e.replace(/urn:(x-)?ogc:def:crs:EPSG:(.*:)?(\w+)$/,"EPSG:$3")]||null:t||null;var e}function F(t){!function(t){t.forEach(w)}(t),t.forEach(function(e){t.forEach(function(t){e!==t&&M(e,t,E)})})}function D(t,e,i){const o=function(t,e){return function(t,e){let i=function(t,e){let i;return t in S&&e in S[t]&&(i=S[t][e]),i}(t.getCode(),e.getCode());return i||(i=b),i}(I(t),I(e))}(e,i);return o(t,void 0,t.length)}var k,O,L,j;F(f),F(_),k=f,O=function(t,e,i){const o=t.length;i=i>1?i:2,void 0===e&&(e=i>2?t.slice():new Array(o));for(let r=0;r<o;r+=i){e[r]=u*t[r]/180;let i=d*Math.log(Math.tan(Math.PI*(+t[r+1]+90)/360));i>y?i=y:i<-y&&(i=-y),e[r+1]=i}return e},L=function(t,e,i){const o=t.length;i=i>1?i:2,void 0===e&&(e=i>2?t.slice():new Array(o));for(let r=0;r<o;r+=i)e[r]=180*t[r]/u,e[r+1]=360*Math.atan(Math.exp(t[r+1]/d))/Math.PI-90;return e},_.forEach(function(t){k.forEach(function(e){M(t,e,O),M(e,t,L)})});class W extends r{constructor(t){var e;super(t),this.stylingFunction=()=>({}),this._lib=void 0,this._map=void 0,this._container=void 0,this._projection=void 0,this._vectorSource=void 0,this._geoJSONReader=void 0,this.registeredLayerHandlers=void 0,this._map=t.map,this._lib=t.lib,this._geoJSONReader=new this._lib.GeoJSON,this._projection=()=>{var t;return null!=(t=this._lib.getUserProjection())?t:new c({code:"EPSG:3857"})},this._container=this._map.getViewport(),this._container.setAttribute("tabindex","0");const i=new this._lib.VectorSource({features:[]});this._vectorSource=i;const o=new this._lib.VectorLayer({source:i,style:t=>this.getStyles(t,this.stylingFunction()),zIndex:null!=(e=t.zIndex)?e:1e5});this._map.addLayer(o)}hexToRGB(t){return{r:parseInt(t.slice(1,3),16),g:parseInt(t.slice(3,5),16),b:parseInt(t.slice(5,7),16)}}getStyles(t,e){const i=t.getGeometry();if(i)return{Point:t=>{const i=t.getProperties(),o=e[i.mode]({type:"Feature",geometry:{type:"Point",coordinates:[]},properties:i});return new this._lib.Style({image:new this._lib.Circle({radius:o.pointWidth,fill:new this._lib.Fill({color:o.pointColor}),stroke:new this._lib.Stroke({color:o.pointOutlineColor,width:o.pointOutlineWidth})})})},LineString:t=>{const i=t.getProperties(),o=e[i.mode]({type:"Feature",geometry:{type:"LineString",coordinates:[]},properties:i});return new this._lib.Style({stroke:new this._lib.Stroke({color:o.lineStringColor,width:o.lineStringWidth})})},Polygon:t=>{const i=t.getProperties(),o=e[i.mode]({type:"Feature",geometry:{type:"Polygon",coordinates:[]},properties:i}),{r,g:s,b:n}=this.hexToRGB(o.polygonFillColor);return new this._lib.Style({stroke:new this._lib.Stroke({color:o.polygonOutlineColor,width:o.polygonOutlineWidth}),fill:new this._lib.Fill({color:`rgba(${r},${s},${n},${o.polygonFillOpacity})`})})}}[i.getType()](t)}clearLayers(){this._vectorSource&&this._vectorSource.clear()}addFeature(t){const e=this._geoJSONReader.readFeature(t,{dataProjection:"EPSG:4326",featureProjection:this._projection()});this._vectorSource.addFeature(e)}removeFeature(t){const e=this._vectorSource.getFeatureById(t);e&&this._vectorSource.removeFeature(e)}sortElementsByDOMOrder(t){return t.sort((t,e)=>{const i=t.compareDocumentPosition(e);return i&Node.DOCUMENT_POSITION_FOLLOWING?-1:i&Node.DOCUMENT_POSITION_PRECEDING?1:0})}getLngLatFromEvent(t){const{containerX:e,containerY:i}=this.getMapElementXYPosition(t);try{return this.unproject(e,i)}catch(t){return null}}getMapEventElement(){const t=Array.from(this._container.querySelectorAll("canvas")),e=this.sortElementsByDOMOrder(t);return e[e.length-1]}setDraggability(t){this._map.getInteractions().forEach(e=>{"DragPan"===e.constructor.name&&e.setActive(t)})}project(t,e){const[i,o]=this._map.getPixelFromCoordinate(D([t,e],"EPSG:4326",void 0!==(r=this._projection())?r:"EPSG:3857"));var r;return{x:i,y:o}}unproject(t,e){const[i,o]=function(t,e){const i=D(t,void 0!==e?e:"EPSG:3857","EPSG:4326"),o=i[0];return(o<-180||o>180)&&(i[0]=function(t,e){const i=t%360;return 360*i<0?i+360:i}(o+180)-180),i}(this._map.getCoordinateFromPixel([t,e]),this._projection());return{lng:i,lat:o}}setCursor(t){"unset"===t?this.getMapEventElement().style.removeProperty("cursor"):this.getMapEventElement().style.cursor=t}setDoubleClickToZoom(t){this._map.getInteractions().forEach(function(e){"DoubleClickZoom"===e.constructor.name&&e.setActive(t)})}render(t,e){this.stylingFunction=()=>e,t.deletedIds.forEach(t=>{this.removeFeature(t)}),t.updated.forEach(t=>{this.removeFeature(t.id),this.addFeature(t)}),t.created.forEach(t=>{this.addFeature(t)})}clear(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this.clearLayers())}register(t){if(super.register(t),!this.registeredLayerHandlers){const t=this._map.getLayerGroup().getLayers(),e=()=>{this._listeners.forEach(t=>{t.unregister()})},i=()=>{this._listeners=this.getAdapterListeners(),this._listeners.forEach(t=>{t.register()})};this.registeredLayerHandlers={addLayers:()=>{e(),this._map.once("rendercomplete",()=>{this._currentModeCallbacks&&i()})},removeLayers:()=>{e(),this._map.once("rendercomplete",()=>{this._currentModeCallbacks&&i()})}},t.on("add",()=>{e(),this._map.once("rendercomplete",()=>{this._currentModeCallbacks&&i()})}),t.on("remove",()=>{e(),this._map.once("rendercomplete",()=>{this._currentModeCallbacks&&i()})})}this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()}getCoordinatePrecision(){return super.getCoordinatePrecision()}unregister(){if(this.registeredLayerHandlers){const t=this._map.getLayerGroup();if(t){const e=t.getLayers();e&&(e.un("add",this.registeredLayerHandlers.addLayers),e.un("remove",this.registeredLayerHandlers.removeLayers))}}return super.unregister()}}class B extends r{constructor(t){super(t),this._lib=void 0,this._mapView=void 0,this._container=void 0,this._featureIdAttributeName="__tdId",this._featureLayerName="__terraDrawFeatures",this._featureLayer=void 0,this._dragEnabled=!0,this._zoomEnabled=!0,this._dragHandler=void 0,this._doubleClickHandler=void 0,this._mapView=t.map,this._lib=t.lib,this._container=this._mapView.container,this._featureLayer=new this._lib.GraphicsLayer({id:this._featureLayerName}),this._mapView.map.add(this._featureLayer)}register(t){super.register(t),this._dragHandler=this._mapView.on("drag",t=>{this._dragEnabled||t.stopPropagation()}),this._doubleClickHandler=this._mapView.on("double-click",t=>{this._zoomEnabled||t.stopPropagation()}),this._currentModeCallbacks&&this._currentModeCallbacks.onReady&&this._currentModeCallbacks.onReady()}unregister(){super.unregister(),this._dragHandler&&this._dragHandler.remove(),this._doubleClickHandler&&this._doubleClickHandler.remove()}getCoordinatePrecision(){return super.getCoordinatePrecision()}getLngLatFromEvent(t){const{containerX:e,containerY:i}=this.getMapElementXYPosition(t);return this.unproject(e,i)}getMapEventElement(){return this._container.querySelector(".esri-view-surface")}setDraggability(t){this._dragEnabled=t}project(t,e){const i=new this._lib.Point({longitude:t,latitude:e}),{x:o,y:r}=this._mapView.toScreen(i);return{x:o,y:r}}unproject(t,e){const{latitude:i,longitude:o}=this._mapView.toMap({x:t,y:e});return{lng:o,lat:i}}setCursor(t){"unset"===t?this.getMapEventElement().style.removeProperty("cursor"):this.getMapEventElement().style.cursor=t}setDoubleClickToZoom(t){this._zoomEnabled=t}render(t,e){t.created.forEach(t=>{this.addFeature(t,e)}),t.updated.forEach(t=>{this.removeFeatureById(t.id),this.addFeature(t,e)}),t.deletedIds.forEach(t=>{this.removeFeatureById(t)})}clear(){this._featureLayer.graphics.removeAll()}removeFeatureById(t){const e=this._featureLayer.graphics.find(e=>e.attributes[this._featureIdAttributeName]===t);this._featureLayer.remove(e)}addFeature(t,e){const{coordinates:i,type:o}=t.geometry,r=e[t.properties.mode](t);let s,n;switch(o){case"Point":n=new this._lib.Point({latitude:i[1],longitude:i[0]}),s=new this._lib.SimpleMarkerSymbol({color:this.getColorFromHex(r.pointColor),size:2*r.pointWidth+"px",outline:{color:this.getColorFromHex(r.pointOutlineColor),width:r.pointOutlineWidth+"px"}});break;case"LineString":n=new this._lib.Polyline({paths:[i]}),s=new this._lib.SimpleLineSymbol({color:this.getColorFromHex(r.lineStringColor),width:r.lineStringWidth+"px"});break;case"Polygon":n=new this._lib.Polygon({rings:i}),s=new this._lib.SimpleFillSymbol({color:this.getColorFromHex(r.polygonFillColor,r.polygonFillOpacity),outline:{color:this.getColorFromHex(r.polygonOutlineColor),width:r.polygonOutlineWidth+"px"}})}const a=new this._lib.Graphic({geometry:n,symbol:s,attributes:{[this._featureIdAttributeName]:t.id}});"Point"===o?this._featureLayer.graphics.add(a):this._featureLayer.graphics.add(a,0)}getColorFromHex(t,e){const i=this._lib.Color.fromHex(t);return e&&(i.a=e),i}}!function(t){t.Commit="commit",t.Provisional="provisional",t.Finish="finish"}(j||(j={}));const G="selected",N="midPoint",V="closingPoint";function A(t){return Boolean(t&&"object"==typeof t&&null!==t&&!Array.isArray(t))}function T(t){if(!function(t){return"number"==typeof t&&!isNaN(new Date(t).valueOf())}(t))throw new Error("updatedAt and createdAt are not valid timestamps");return!0}var R;!function(t){t.Drawing="drawing",t.Select="select",t.Static="static",t.Render="render"}(R||(R={}));class U{get state(){return this._state}set state(t){throw new Error("Please use the modes lifecycle methods")}get styles(){return this._styles}set styles(t){if("object"!=typeof t)throw new Error("Styling must be an object");this.onStyleChange([],"styling"),this._styles=t}registerBehaviors(t){}constructor(e){this._state=void 0,this._styles=void 0,this.behaviors=[],this.validate=void 0,this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.onStyleChange=void 0,this.store=void 0,this.setDoubleClickToZoom=void 0,this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this.projection=void 0,this.type=R.Drawing,this.mode="base",this._state="unregistered",this._styles=e&&e.styles?t({},e.styles):{},this.pointerDistance=e&&e.pointerDistance||40,this.validate=e&&e.validation,this.projection=e&&e.projection||"web-mercator"}setDrawing(){if("started"!==this._state)throw new Error("Mode must be unregistered or stopped to start");this._state="drawing"}setStarted(){if("stopped"!==this._state&&"registered"!==this._state&&"drawing"!==this._state&&"selecting"!==this._state)throw new Error("Mode must be unregistered or stopped to start");this._state="started",this.setDoubleClickToZoom(!1)}setStopped(){if("started"!==this._state)throw new Error("Mode must be started to be stopped");this._state="stopped",this.setDoubleClickToZoom(!0)}register(t){if("unregistered"!==this._state)throw new Error("Can not register unless mode is unregistered");this._state="registered",this.store=t.store,this.store.registerOnChange(t.onChange),this.setDoubleClickToZoom=t.setDoubleClickToZoom,this.project=t.project,this.unproject=t.unproject,this.onSelect=t.onSelect,this.onDeselect=t.onDeselect,this.setCursor=t.setCursor,this.onStyleChange=t.onChange,this.onFinish=t.onFinish,this.coordinatePrecision=t.coordinatePrecision,this.registerBehaviors({mode:t.mode,store:this.store,project:this.project,unproject:this.unproject,pointerDistance:this.pointerDistance,coordinatePrecision:t.coordinatePrecision,projection:this.projection})}validateFeature(t){if("unregistered"===this._state)throw new Error("Mode must be registered");const e=function(t,e){let i;if(A(t))if(null==t.id)i="Feature has no id";else if("string"!=typeof t.id&&"number"!=typeof t.id)i="Feature must be string or number as per GeoJSON spec";else if(e(t.id))if(A(t.geometry))if(A(t.properties))if("string"==typeof t.geometry.type&&["Polygon","LineString","Point"].includes(t.geometry.type))if(Array.isArray(t.geometry.coordinates)){if(!t.properties.mode||"string"!=typeof t.properties.mode)throw new Error("Feature does not have a valid mode property")}else i="Feature coordinates is not an array";else i="Feature is not Point, LineString or Polygon";else i="Feature has no properties";else i="Feature has no geometry";else i="Feature must match the id strategy (default is UUID4)";else i="Feature is not object";if(i)throw new Error(i);return!0}(t,this.store.idStrategy.isValidId);return this.validate?this.validate(t,{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:j.Provisional}):e}onFinish(t,e){}onDeselect(t){}onSelect(t){}onKeyDown(t){}onKeyUp(t){}onMouseMove(t){}onClick(t){}onDragStart(t,e){}onDrag(t,e){}onDragEnd(t,e){}getHexColorStylingValue(t,e,i){return this.getStylingValue(t,e,i)}getNumericStylingValue(t,e,i){return this.getStylingValue(t,e,i)}getStylingValue(t,e,i){return void 0===t?e:"function"==typeof t?t(i):t}}class X extends U{constructor(...t){super(...t),this.type=R.Select}}function z(t,e){const i=t=>t*Math.PI/180,o=i(t[1]),r=i(t[0]),s=i(e[1]),n=s-o,a=i(e[0])-r,l=Math.sin(n/2)*Math.sin(n/2)+Math.cos(o)*Math.cos(s)*Math.sin(a/2)*Math.sin(a/2);return 2*Math.atan2(Math.sqrt(l),Math.sqrt(1-l))*6371e3/1e3}const H=6371008.8;function Y(t){return t%360*Math.PI/180}function K(t){return t/(H/1e3)}function $(t){return t%(2*Math.PI)*180/Math.PI}const J=57.29577951308232,Z=.017453292519943295,q=6378137,Q=(t,e)=>({x:0===t?0:t*Z*q,y:0===e?0:Math.log(Math.tan(Math.PI/4+e*Z/2))*q}),tt=(t,e)=>({lng:0===t?0:J*(t/q),lat:0===e?0:(2*Math.atan(Math.exp(e/q))-Math.PI/2)*J});function et(t,e,i){const o=Y(t[0]),r=Y(t[1]),s=Y(i),n=K(e),a=Math.asin(Math.sin(r)*Math.cos(n)+Math.cos(r)*Math.sin(n)*Math.cos(s));return[$(o+Math.atan2(Math.sin(s)*Math.sin(n)*Math.cos(r),Math.cos(n)-Math.sin(r)*Math.sin(a))),$(a)]}function it(t){const{center:i,radiusKilometers:o,coordinatePrecision:r}=t,s=t.steps?t.steps:64,n=[];for(let t=0;t<s;t++){const a=et(i,o,-360*t/s);n.push([e(a[0],r),e(a[1],r)])}return n.push(n[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[n]},properties:{}}}function ot(t){const e={epsilon:0};let i;if("Polygon"===t.geometry.type)i=t.geometry.coordinates;else{if("LineString"!==t.geometry.type)throw new Error("Self intersects only accepts Polygons and LineStrings");i=[t.geometry.coordinates]}const o=[];for(let t=0;t<i.length;t++)for(let e=0;e<i[t].length-1;e++)for(let o=0;o<i.length;o++)for(let r=0;r<i[o].length-1;r++)s(t,e,o,r);return o.length>0;function r(t){return t<0-e.epsilon||t>1+e.epsilon}function s(t,e,s,n){const a=i[t][e],l=i[t][e+1],h=i[s][n],c=i[s][n+1],d=function(t,e,i,o){if(rt(t,i)||rt(t,o)||rt(e,i)||rt(o,i))return null;const r=t[0],s=t[1],n=e[0],a=e[1],l=i[0],h=i[1],c=o[0],d=o[1],u=(r-n)*(h-d)-(s-a)*(l-c);return 0===u?null:[((r*a-s*n)*(l-c)-(r-n)*(l*d-h*c))/u,((r*a-s*n)*(h-d)-(s-a)*(l*d-h*c))/u]}(a,l,h,c);if(null===d)return;let u,g;u=l[0]!==a[0]?(d[0]-a[0])/(l[0]-a[0]):(d[1]-a[1])/(l[1]-a[1]),g=c[0]!==h[0]?(d[0]-h[0])/(c[0]-h[0]):(d[1]-h[1])/(c[1]-h[1]),r(u)||r(g)||(d.toString(),o.push(d))}}function rt(t,e){return t[0]===e[0]&&t[1]===e[1]}function st(t,e){return 2===t.length&&"number"==typeof t[0]&&"number"==typeof t[1]&&Infinity!==t[0]&&Infinity!==t[1]&&(o=t[0])>=-180&&o<=180&&(i=t[1])>=-90&&i<=90&&nt(t[0])<=e&&nt(t[1])<=e;var i,o}function nt(t){let e=1,i=0;for(;Math.round(t*e)/e!==t;)e*=10,i++;return i}function at(t,e){return"Polygon"===t.geometry.type&&1===t.geometry.coordinates.length&&t.geometry.coordinates[0].length>=4&&t.geometry.coordinates[0].every(t=>st(t,e))&&(i=t.geometry.coordinates[0][0])[0]===(o=t.geometry.coordinates[0][t.geometry.coordinates[0].length-1])[0]&&i[1]===o[1];var i,o}function lt(t,e){return at(t,e)&&!ot(t)}class ht extends U{constructor(e){var i;super(e),this.mode="circle",this.center=void 0,this.clickCount=0,this.currentCircleId=void 0,this.keyEvents=void 0,this.cursors=void 0,this.startingRadiusKilometers=1e-5;const o={start:"crosshair"};if(this.cursors=e&&e.cursors?t({},o,e.cursors):o,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.startingRadiusKilometers=null!=(i=null==e?void 0:e.startingRadiusKilometers)?i:1e-5,this.validate=null==e?void 0:e.validation}close(){if(void 0===this.currentCircleId)return;const t=this.currentCircleId;if(this.validate&&t){const e=this.store.getGeometryCopy(t);if(!this.validate({type:"Feature",id:t,geometry:e,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:j.Finish}))return}this.center=void 0,this.currentCircleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){if(0===this.clickCount){this.center=[t.lng,t.lat];const e=it({center:this.center,radiusKilometers:this.startingRadiusKilometers,coordinatePrecision:this.coordinatePrecision}),[i]=this.store.create([{geometry:e.geometry,properties:{mode:this.mode,radiusKilometers:this.startingRadiusKilometers}}]);this.currentCircleId=i,this.clickCount++,this.setDrawing()}else 1===this.clickCount&&this.center&&void 0!==this.currentCircleId&&this.updateCircle(t),this.close()}onMouseMove(t){this.updateCircle(t)}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentCircleId;this.center=void 0,this.currentCircleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t])}catch(t){}}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Polygon"===e.geometry.type&&e.properties.mode===this.mode?(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i):i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&lt(t,this.coordinatePrecision)}updateCircle(t){if(1===this.clickCount&&this.center&&this.currentCircleId){const i=z(this.center,[t.lng,t.lat]);let o;if("web-mercator"===this.projection){const r=function(t,e){const i=1e3*z(t,e);if(0===i)return 1;const{x:o,y:r}=Q(t[0],t[1]),{x:s,y:n}=Q(e[0],e[1]);return Math.sqrt(Math.pow(s-o,2)+Math.pow(n-r,2))/i}(this.center,[t.lng,t.lat]);o=function(t){const{center:i,radiusKilometers:o,coordinatePrecision:r}=t,s=t.steps?t.steps:64,n=1e3*o,[a,l]=i,{x:h,y:c}=Q(a,l),d=[];for(let t=0;t<s;t++){const i=360*t/s*Math.PI/180,o=n*Math.cos(i),a=n*Math.sin(i),[l,u]=[h+o,c+a],{lng:g,lat:p}=tt(l,u);d.push([e(g,r),e(p,r)])}return d.push(d[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[d]},properties:{}}}({center:this.center,radiusKilometers:i*r,coordinatePrecision:this.coordinatePrecision})}else{if("globe"!==this.projection)throw new Error("Invalid projection");o=it({center:this.center,radiusKilometers:i,coordinatePrecision:this.coordinatePrecision})}if(this.validate&&!this.validate({type:"Feature",id:this.currentCircleId,geometry:o.geometry,properties:{radiusKilometers:i}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:j.Provisional}))return;this.store.updateGeometry([{id:this.currentCircleId,geometry:o.geometry}]),this.store.updateProperty([{id:this.currentCircleId,property:"radiusKilometers",value:i}])}}}class ct extends U{constructor(e){super(e),this.mode="freehand",this.startingClick=!1,this.currentId=void 0,this.closingPointId=void 0,this.minDistance=void 0,this.keyEvents=void 0,this.cursors=void 0,this.preventPointsNearClose=void 0;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,this.preventPointsNearClose=e&&e.preventPointsNearClose||!0,this.minDistance=e&&e.minDistance||20,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.validate=null==e?void 0:e.validation}close(){if(void 0===this.currentId)return;const t=this.currentId;if(this.validate&&t){const e=this.store.getGeometryCopy(t);if(!this.validate({type:"Feature",id:t,geometry:e,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:j.Finish}))return}this.closingPointId&&this.store.delete([this.closingPointId]),this.startingClick=!1,this.currentId=void 0,this.closingPointId=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(void 0===this.currentId||!1===this.startingClick)return;const e=this.store.getGeometryCopy(this.currentId),o=e.coordinates[0].length-2,[r,s]=e.coordinates[0][o],{x:n,y:a}=this.project(r,s),l=i({x:n,y:a},{x:t.containerX,y:t.containerY}),[h,c]=e.coordinates[0][0],{x:d,y:u}=this.project(h,c);if(i({x:d,y:u},{x:t.containerX,y:t.containerY})<this.pointerDistance){if(this.setCursor(this.cursors.close),this.preventPointsNearClose)return}else this.setCursor(this.cursors.start);if(l<this.minDistance)return;e.coordinates[0].pop();const g={type:"Polygon",coordinates:[[...e.coordinates[0],[t.lng,t.lat],e.coordinates[0][0]]]};this.validate&&!this.validate({type:"Feature",id:this.currentId,geometry:g,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:j.Provisional})||this.store.updateGeometry([{id:this.currentId,geometry:g}])}onClick(t){if(!1===this.startingClick){const[e,i]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}},{geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:{mode:this.mode}}]);return this.currentId=e,this.closingPointId=i,this.startingClick=!0,void this.setDrawing()}this.close()}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentId,e=this.closingPointId;this.closingPointId=void 0,this.currentId=void 0,this.startingClick=!1,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t]),void 0!==e&&this.store.delete([e])}catch(t){}}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Polygon"===e.geometry.type&&e.properties.mode===this.mode?(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i):"Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode?(i.pointWidth=this.getNumericStylingValue(this.styles.closingPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.closingPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.closingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.closingPointOutlineWidth,2,e),i.zIndex=40,i):i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}}class dt{constructor({store:t,mode:e,project:i,unproject:o,pointerDistance:r,coordinatePrecision:s,projection:n}){this.store=void 0,this.mode=void 0,this.project=void 0,this.unproject=void 0,this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.projection=void 0,this.store=t,this.mode=e,this.project=i,this.unproject=o,this.pointerDistance=r,this.coordinatePrecision=s,this.projection=n}}function ut({unproject:t,point:e,pointerDistance:i}){const o=i/2,{x:r,y:s}=e;return{type:"Feature",properties:{},geometry:{type:"Polygon",coordinates:[[t(r-o,s-o),t(r+o,s-o),t(r+o,s+o),t(r-o,s+o),t(r-o,s-o)].map(t=>[t.lng,t.lat])]}}}class gt extends dt{constructor(t){super(t)}create(t){const{containerX:e,containerY:i}=t;return ut({unproject:this.unproject,point:{x:e,y:i},pointerDistance:this.pointerDistance})}}class pt extends dt{constructor(t){super(t)}measure(t,e){const{x:o,y:r}=this.project(e[0],e[1]);return i({x:o,y:r},{x:t.containerX,y:t.containerY})}}class yt extends dt{constructor(t,e,i){super(t),this.config=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.getSnappableCoordinateFirstClick=t=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode)),this.getSnappableCoordinate=(t,e)=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e)),this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),r={coord:void 0,minDist:Infinity};return o.forEach(e=>{let i;if("Polygon"===e.geometry.type)i=e.geometry.coordinates[0];else{if("LineString"!==e.geometry.type)return;i=e.geometry.coordinates}i.forEach(e=>{const i=this.pixelDistance.measure(t,e);i<r.minDist&&i<this.pointerDistance&&(r.coord=e,r.minDist=i)})}),r.coord}}function mt(t,e,i){const o=Y(t[0]),r=Y(t[1]),s=Y(i),n=K(e),a=Math.asin(Math.sin(r)*Math.cos(n)+Math.cos(r)*Math.sin(n)*Math.cos(s));return[$(o+Math.atan2(Math.sin(s)*Math.sin(n)*Math.cos(r),Math.cos(n)-Math.sin(r)*Math.sin(a))),$(a)]}function ft({x:t,y:e},i,o){const r=Y(o);return{x:t+i*Math.cos(r),y:e+i*Math.sin(r)}}function Ct(t,e){const i=Y(t[0]),o=Y(e[0]),r=Y(t[1]),s=Y(e[1]),n=Math.sin(o-i)*Math.cos(s),a=Math.cos(r)*Math.sin(s)-Math.sin(r)*Math.cos(s)*Math.cos(o-i);return $(Math.atan2(n,a))}function vt({x:t,y:e},{x:i,y:o}){let r=Math.atan2(o-e,i-t);return r*=180/Math.PI,r>180?r-=360:r<-180&&(r+=360),r}function Pt(t){return(t+360)%360}function _t(t,e,i){const o=[],r=t.length;let s,n,a,l=0;for(let r=0;r<t.length&&!(e>=l&&r===t.length-1);r++){if(l>e&&0===o.length){if(s=e-l,!s)return o.push(t[r]),o;n=Ct(t[r],t[r-1])-180,a=mt(t[r],s,n),o.push(a)}if(l>=i)return s=i-l,s?(n=Ct(t[r],t[r-1])-180,a=mt(t[r],s,n),o.push(a),o):(o.push(t[r]),o);if(l>=e&&o.push(t[r]),r===t.length-1)return o;l+=z(t[r],t[r+1])}if(l<e&&t.length===r)throw new Error("Start position is beyond line");const h=t[t.length-1];return[h,h]}function xt(t){return t*(Math.PI/180)}function St(t){return t*(180/Math.PI)}class Mt extends dt{constructor(t){super(t),this.config=void 0,this.config=t}generateInsertionCoordinates(t,e,i){const o=[t,e];let r=0;for(let t=0;t<o.length-1;t++)r+=z(o[0],o[1]);if(r<=i)return o;let s=r/i-1;Number.isInteger(s)||(s=Math.floor(s)+1);const n=[];for(let t=0;t<s;t++){const e=_t(o,i*t,i*(t+1));n.push(e)}const a=[];for(let t=0;t<n.length;t++)a.push(n[t][1]);return this.limitCoordinates(a)}generateInsertionGeodesicCoordinates(t,e,i){const o=z(t,e),r=function(t,e,i){const o=[],r=xt(t[1]),s=xt(t[0]),n=xt(e[1]),a=xt(e[0]);i+=1;const l=2*Math.asin(Math.sqrt(Math.sin((n-r)/2)**2+Math.cos(r)*Math.cos(n)*Math.sin((a-s)/2)**2));if(0===l||isNaN(l))return o;for(let t=0;t<=i;t++){const e=t/i,h=Math.sin((1-e)*l)/Math.sin(l),c=Math.sin(e*l)/Math.sin(l),d=h*Math.cos(r)*Math.cos(s)+c*Math.cos(n)*Math.cos(a),u=h*Math.cos(r)*Math.sin(s)+c*Math.cos(n)*Math.sin(a),g=h*Math.sin(r)+c*Math.sin(n);if(isNaN(d)||isNaN(u)||isNaN(g))continue;const p=Math.atan2(g,Math.sqrt(d**2+u**2)),y=Math.atan2(u,d);isNaN(p)||isNaN(y)||o.push([St(y),St(p)])}return o.slice(1,-1)}(t,e,Math.floor(o/i));return this.limitCoordinates(r)}limitCoordinates(t){return t.map(t=>[e(t[0],this.config.coordinatePrecision),e(t[1],this.config.coordinatePrecision)])}}function Et(t,e){return t[0]===e[0]&&t[1]===e[1]}class bt extends U{constructor(e){super(e),this.mode="linestring",this.currentCoordinate=0,this.currentId=void 0,this.closingPointId=void 0,this.keyEvents=void 0,this.snappingEnabled=void 0,this.cursors=void 0,this.mouseMove=!1,this.insertCoordinates=void 0,this.lastCommitedCoordinates=void 0,this.snapping=void 0,this.insertPoint=void 0;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,this.snappingEnabled=!(!e||void 0===e.snapping)&&e.snapping,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.validate=null==e?void 0:e.validation,this.insertCoordinates=null==e?void 0:e.insertCoordinates}close(){if(void 0===this.currentId)return;const t=this.store.getGeometryCopy(this.currentId);t.coordinates.pop(),this.updateGeometries([...t.coordinates],void 0,j.Commit);const e=this.currentId;this.closingPointId&&this.store.delete([this.closingPointId]),this.currentCoordinate=0,this.currentId=void 0,this.closingPointId=void 0,this.lastCommitedCoordinates=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(e,{mode:this.mode,action:"draw"})}updateGeometries(t,e,i){if(!this.currentId)return;const o={type:"LineString",coordinates:t};if(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i}))return;const r=[{id:this.currentId,geometry:o}];this.closingPointId&&e&&r.push({id:this.closingPointId,geometry:{type:"Point",coordinates:e}}),"commit"===i&&(this.lastCommitedCoordinates=o.coordinates),this.store.updateGeometry(r)}generateInsertCoordinates(t,e){if(!this.insertCoordinates||!this.lastCommitedCoordinates)throw new Error("Not able to insert coordinates");if("amount"!==this.insertCoordinates.strategy)throw new Error("Strategy does not exist");const i=z(t,e)/(this.insertCoordinates.value+1);let o=[];return"globe"===this.projection?o=this.insertPoint.generateInsertionGeodesicCoordinates(t,e,i):"web-mercator"===this.projection&&(o=this.insertPoint.generateInsertionCoordinates(t,e,i)),o}createLine(t){const[e]=this.store.create([{geometry:{type:"LineString",coordinates:[t,t]},properties:{mode:this.mode}}]);this.lastCommitedCoordinates=[t,t],this.currentId=e,this.currentCoordinate++,this.setDrawing()}firstUpdateToLine(t){if(!this.currentId)return;const e=this.store.getGeometryCopy(this.currentId).coordinates,[i]=this.store.create([{geometry:{type:"Point",coordinates:[...t]},properties:{mode:this.mode}}]);this.closingPointId=i,this.setCursor(this.cursors.close);const o=[...e,t];this.updateGeometries(o,void 0,j.Commit),this.currentCoordinate++}updateToLine(t,e){if(!this.currentId)return;const o=this.store.getGeometryCopy(this.currentId).coordinates,[r,s]=this.lastCommitedCoordinates?this.lastCommitedCoordinates[this.lastCommitedCoordinates.length-1]:o[o.length-2],{x:n,y:a}=this.project(r,s);if(i({x:n,y:a},{x:e.x,y:e.y})<this.pointerDistance)return void this.close();this.setCursor(this.cursors.close);const l=[...o,t];this.updateGeometries(l,o[o.length-1],j.Commit),this.currentCoordinate++}registerBehaviors(t){this.snapping=new yt(t,new pt(t),new gt(t)),this.insertPoint=new Mt(t)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;const e=this.store.getGeometryCopy(this.currentId).coordinates;e.pop();const o=this.snappingEnabled&&this.snapping.getSnappableCoordinate(t,this.currentId)||[t.lng,t.lat];if(this.closingPointId){const[o,r]=e[e.length-1],{x:s,y:n}=this.project(o,r);i({x:s,y:n},{x:t.containerX,y:t.containerY})<this.pointerDistance&&this.setCursor(this.cursors.close)}let r=[...e,o];if(this.insertCoordinates&&this.currentId&&this.lastCommitedCoordinates){const t=this.lastCommitedCoordinates[this.lastCommitedCoordinates.length-1],e=o;if(!Et(t,e)){const i=this.generateInsertCoordinates(t,e);r=[...this.lastCommitedCoordinates.slice(0,-1),...i,o]}}this.updateGeometries(r,void 0,j.Provisional)}onClick(t){this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1;const e=this.currentId&&this.snappingEnabled&&this.snapping.getSnappableCoordinate(t,this.currentId)||[t.lng,t.lat];0===this.currentCoordinate?this.createLine(e):1===this.currentCoordinate&&this.currentId?this.firstUpdateToLine(e):this.currentId&&this.updateToLine(e,{x:t.containerX,y:t.containerY})}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp(),t.key===this.keyEvents.finish&&this.close()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentId,e=this.closingPointId;this.closingPointId=void 0,this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t]),void 0!==e&&this.store.delete([e])}catch(t){}}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"LineString"===e.geometry.type&&e.properties.mode===this.mode?(i.lineStringColor=this.getHexColorStylingValue(this.styles.lineStringColor,i.lineStringColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.lineStringWidth,i.lineStringWidth,e),i.zIndex=10,i):"Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode?(i.pointColor=this.getHexColorStylingValue(this.styles.closingPointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.closingPointWidth,i.pointWidth,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.closingPointOutlineColor,"#ffffff",e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.closingPointOutlineWidth,2,e),i.zIndex=40,i):i}validateFeature(t){return!!super.validateFeature(t)&&"LineString"===t.geometry.type&&t.properties.mode===this.mode&&t.geometry.coordinates.length>=2}}function wt(t,e){return"Point"===t.geometry.type&&st(t.geometry.coordinates,e)}class It extends U{constructor(e){super(e),this.mode="point",this.cursors=void 0;const i={create:"crosshair"};this.cursors=e&&e.cursors?t({},i,e.cursors):i}start(){this.setStarted(),this.setCursor(this.cursors.create)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){if(!this.store)throw new Error("Mode must be registered first");const e={type:"Point",coordinates:[t.lng,t.lat]},i={mode:this.mode};if(this.validate&&!this.validate({type:"Feature",geometry:e,properties:i},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:j.Finish}))return;const[o]=this.store.create([{geometry:e,properties:i}]);this.onFinish(o,{mode:this.mode,action:"draw"})}onMouseMove(){}onKeyDown(){}onKeyUp(){}cleanUp(){}onDragStart(){}onDrag(){}onDragEnd(){}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode&&(i.pointWidth=this.getNumericStylingValue(this.styles.pointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.pointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.pointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.pointOutlineWidth,2,e),i.zIndex=30),i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&wt(t,this.coordinatePrecision)}}class Ft extends dt{constructor(t,e){super(t),this.config=void 0,this.pixelDistance=void 0,this._startEndPoints=[],this.config=t,this.pixelDistance=e}get ids(){return this._startEndPoints.concat()}set ids(t){}create(t,e){if(this.ids.length)throw new Error("Opening and closing points already created");if(t.length<=3)throw new Error("Requires at least 4 coordinates");this._startEndPoints=this.store.create([{geometry:{type:"Point",coordinates:t[0]},properties:{mode:e,[V]:!0}},{geometry:{type:"Point",coordinates:t[t.length-2]},properties:{mode:e,[V]:!0}}])}delete(){this.ids.length&&(this.store.delete(this.ids),this._startEndPoints=[])}update(t){if(2!==this.ids.length)throw new Error("No closing points to update");this.store.updateGeometry([{id:this.ids[0],geometry:{type:"Point",coordinates:t[0]}},{id:this.ids[1],geometry:{type:"Point",coordinates:t[t.length-3]}}])}isClosingPoint(t){const e=this.store.getGeometryCopy(this.ids[0]),i=this.store.getGeometryCopy(this.ids[1]),o=this.pixelDistance.measure(t,e.coordinates),r=this.pixelDistance.measure(t,i.coordinates);return{isClosing:o<this.pointerDistance,isPreviousClosing:r<this.pointerDistance}}}class Dt extends U{constructor(e){super(e),this.mode="polygon",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=void 0,this.snappingEnabled=void 0,this.snapping=void 0,this.pixelDistance=void 0,this.closingPoints=void 0,this.cursors=void 0,this.mouseMove=!1;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,this.snappingEnabled=!(!e||void 0===e.snapping)&&e.snapping,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}}close(){if(void 0===this.currentId)return;const t=this.store.getGeometryCopy(this.currentId).coordinates[0];if(t.length<5)return;if(!this.updatePolygonGeometry([...t.slice(0,-2),t[0]],j.Finish))return;const e=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.closingPoints.delete(),"drawing"===this.state&&this.setStarted(),this.onFinish(e,{mode:this.mode,action:"draw"})}registerBehaviors(t){this.pixelDistance=new pt(t),this.snapping=new yt(t,this.pixelDistance,new gt(t)),this.closingPoints=new Ft(t,this.pixelDistance)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0,i=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(e&&(t.lng=e[0],t.lat=e[1]),1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),r=Math.max(1e-6,e);o=[i[0],[t.lng,t.lat],[t.lng,t.lat-r],i[0]]}else if(2===this.currentCoordinate)o=[i[0],i[1],[t.lng,t.lat],i[0]];else{const{isClosing:e,isPreviousClosing:r}=this.closingPoints.isClosingPoint(t);r||e?(this.setCursor(this.cursors.close),o=[...i.slice(0,-2),i[0],i[0]]):o=[...i.slice(0,-2),[t.lng,t.lat],i[0]]}this.updatePolygonGeometry(o,j.Provisional)}updatePolygonGeometry(t,e){if(!this.currentId)return!1;const i={type:"Polygon",coordinates:[t]};return!(this.validate&&!this.validate({type:"Feature",geometry:i},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e})||(this.store.updateGeometry([{id:this.currentId,geometry:i}]),0))}onClick(t){if(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,0===this.currentCoordinate){const e=this.snappingEnabled?this.snapping.getSnappableCoordinateFirstClick(t):void 0;e&&(t.lng=e[0],t.lat=e[1]);const[i]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentId=i,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0;e&&(t.lng=e[0],t.lat=e[1]);const i=this.store.getGeometryCopy(this.currentId);if(Et([t.lng,t.lat],i.coordinates[0][0]))return;if(!this.updatePolygonGeometry([i.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]],j.Commit))return;this.currentCoordinate++}else if(2===this.currentCoordinate&&this.currentId){const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0;e&&(t.lng=e[0],t.lat=e[1]);const i=this.store.getGeometryCopy(this.currentId).coordinates[0];if(Et([t.lng,t.lat],i[1]))return;if(!this.updatePolygonGeometry([i[0],i[1],[t.lng,t.lat],[t.lng,t.lat],i[0]],j.Commit))return;2===this.currentCoordinate&&this.closingPoints.create(i,"polygon"),this.currentCoordinate++}else if(this.currentId){const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0,i=this.store.getGeometryCopy(this.currentId).coordinates[0],{isClosing:o,isPreviousClosing:r}=this.closingPoints.isClosingPoint(t);if(r||o)this.close();else{if(e&&(t.lng=e[0],t.lat=e[1]),Et([t.lng,t.lat],i[this.currentCoordinate-1]))return;const o=function(t=[[[0,0],[0,1],[1,1],[1,0],[0,0]]]){return{type:"Feature",geometry:{type:"Polygon",coordinates:t},properties:{}}}([[...i.slice(0,-1),[t.lng,t.lat],i[0]]]);if(!this.updatePolygonGeometry(o.geometry.coordinates[0],j.Commit))return;this.currentCoordinate++,this.closingPoints.ids.length&&this.closingPoints.update(o.geometry.coordinates[0])}}}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(){this.setCursor("unset")}onDrag(){}onDragEnd(){this.setCursor(this.cursors.start)}cleanUp(){const t=this.currentId;this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t]),this.closingPoints.ids.length&&this.closingPoints.delete()}catch(t){}}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});if(e.properties.mode===this.mode){if("Polygon"===e.geometry.type)return i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i;if("Point"===e.geometry.type)return i.pointWidth=this.getNumericStylingValue(this.styles.closingPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.closingPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.closingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.closingPointOutlineWidth,2,e),i.zIndex=30,i}return i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}}class kt extends U{constructor(e){super(e),this.mode="rectangle",this.center=void 0,this.clickCount=0,this.currentRectangleId=void 0,this.keyEvents=void 0,this.cursors=void 0;const i={start:"crosshair"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}}updateRectangle(t,e){if(1===this.clickCount&&this.center&&this.currentRectangleId){const i=this.store.getGeometryCopy(this.currentRectangleId).coordinates[0][0],o={type:"Polygon",coordinates:[[i,[t.lng,i[1]],[t.lng,t.lat],[i[0],t.lat],i]]};if(this.validate&&!this.validate({id:this.currentRectangleId,geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e}))return;this.store.updateGeometry([{id:this.currentRectangleId,geometry:o}])}}close(){const t=this.currentRectangleId;this.center=void 0,this.currentRectangleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted(),t&&this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){if(0===this.clickCount){this.center=[t.lng,t.lat];const[e]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentRectangleId=e,this.clickCount++,this.setDrawing()}else this.updateRectangle(t,j.Finish),this.close()}onMouseMove(t){this.updateRectangle(t,j.Provisional)}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentRectangleId;this.center=void 0,this.currentRectangleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted(),void 0!==t&&this.store.delete([t])}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Polygon"===e.geometry.type&&e.properties.mode===this.mode?(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i):i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&lt(t,this.coordinatePrecision)}}class Ot extends U{constructor(t){super({styles:t.styles}),this.type=R.Render,this.mode="render",this.mode=t.modeName}registerBehaviors(t){this.mode=t.mode}start(){this.setStarted()}stop(){this.setStopped()}onKeyUp(){}onKeyDown(){}onClick(){}onDragStart(){}onDrag(){}onDragEnd(){}onMouseMove(){}cleanUp(){}styleFeature(t){return{pointColor:this.getHexColorStylingValue(this.styles.pointColor,"#3f97e0",t),pointWidth:this.getNumericStylingValue(this.styles.pointWidth,6,t),pointOutlineColor:this.getHexColorStylingValue(this.styles.pointOutlineColor,"#ffffff",t),pointOutlineWidth:this.getNumericStylingValue(this.styles.pointOutlineWidth,0,t),polygonFillColor:this.getHexColorStylingValue(this.styles.polygonFillColor,"#3f97e0",t),polygonFillOpacity:this.getNumericStylingValue(this.styles.polygonFillOpacity,.3,t),polygonOutlineColor:this.getHexColorStylingValue(this.styles.polygonOutlineColor,"#3f97e0",t),polygonOutlineWidth:this.getNumericStylingValue(this.styles.polygonOutlineWidth,4,t),lineStringWidth:this.getNumericStylingValue(this.styles.lineStringWidth,4,t),lineStringColor:this.getHexColorStylingValue(this.styles.lineStringColor,"#3f97e0",t),zIndex:this.getNumericStylingValue(this.styles.zIndex,0,t)}}validateFeature(t){return super.validateFeature(t)&&(wt(t,this.coordinatePrecision)||at(t,this.coordinatePrecision)||function(t,e){return"LineString"===t.geometry.type&&t.geometry.coordinates.length>=2&&t.geometry.coordinates.every(t=>st(t,e))}(t,this.coordinatePrecision))}}function Lt(t,e){const i=t,o=e,r=Y(i[1]),s=Y(o[1]);let n=Y(o[0]-i[0]);n>Math.PI&&(n-=2*Math.PI),n<-Math.PI&&(n+=2*Math.PI);const a=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(r/2+Math.PI/4)),l=($(Math.atan2(n,a))+360)%360;return l>180?-(360-l):l}function jt(t,e,i){let o=e;e<0&&(o=-Math.abs(o));const r=o/H,s=t[0]*Math.PI/180,n=Y(t[1]),a=Y(i),l=r*Math.cos(a);let h=n+l;Math.abs(h)>Math.PI/2&&(h=h>0?Math.PI-h:-Math.PI-h);const c=Math.log(Math.tan(h/2+Math.PI/4)/Math.tan(n/2+Math.PI/4)),d=Math.abs(c)>1e-11?l/c:Math.cos(n),u=[(180*(s+r*Math.sin(a)/d)/Math.PI+540)%360-180,180*h/Math.PI];return u[0]+=u[0]-t[0]>180?-360:t[0]-u[0]>180?360:0,u}function Wt(t,i,o,r,s){const n=r(t[0],t[1]),a=r(i[0],i[1]),{lng:l,lat:h}=s((n.x+a.x)/2,(n.y+a.y)/2);return[e(l,o),e(h,o)]}function Bt(t,i,o){const r=jt(t,1e3*z(t,i)/2,Lt(t,i));return[e(r[0],o),e(r[1],o)]}function Gt({featureCoords:t,precision:e,unproject:i,project:o,projection:r}){const s=[];for(let n=0;n<t.length-1;n++){let a;if("web-mercator"===r)a=Wt(t[n],t[n+1],e,o,i);else{if("globe"!==r)throw new Error("Invalid projection");a=Bt(t[n],t[n+1],e)}s.push(a)}return s}class Nt extends dt{constructor(t,e){super(t),this.config=void 0,this.selectionPointBehavior=void 0,this._midPoints=[],this.config=t,this.selectionPointBehavior=e}get ids(){return this._midPoints.concat()}set ids(t){}insert(t,e){const i=this.store.getGeometryCopy(t),{midPointFeatureId:o,midPointSegment:r}=this.store.getPropertiesCopy(t),s=this.store.getGeometryCopy(o),n="Polygon"===s.type?s.coordinates[0]:s.coordinates;n.splice(r+1,0,i.coordinates),s.coordinates="Polygon"===s.type?[n]:n,this.store.updateGeometry([{id:o,geometry:s}]),this.store.delete([...this._midPoints,...this.selectionPointBehavior.ids]),this.create(n,o,e),this.selectionPointBehavior.create(n,s.type,o)}create(t,e,i){if(!this.store.has(e))throw new Error("Store does not have feature with this id");this._midPoints=this.store.create(function(t,e,i,o,r,s){return Gt({featureCoords:t,precision:i,project:o,unproject:r,projection:s}).map((t,i)=>({geometry:{type:"Point",coordinates:t},properties:e(i)}))}(t,t=>({mode:this.mode,[N]:!0,midPointSegment:t,midPointFeatureId:e}),i,this.config.project,this.config.unproject,this.projection))}delete(){this._midPoints.length&&(this.store.delete(this._midPoints),this._midPoints=[])}getUpdated(t){if(0!==this._midPoints.length)return Gt({featureCoords:t,precision:this.coordinatePrecision,project:this.config.project,unproject:this.config.unproject,projection:this.config.projection}).map((t,e)=>({id:this._midPoints[e],geometry:{type:"Point",coordinates:t}}))}}class Vt extends dt{constructor(t){super(t),this._selectionPoints=[]}get ids(){return this._selectionPoints.concat()}set ids(t){}create(t,e,i){this._selectionPoints=this.store.create(function(t,e,i){const o=[],r="Polygon"===e?t.length-1:t.length;for(let e=0;e<r;e++)o.push({geometry:{type:"Point",coordinates:t[e]},properties:i(e)});return o}(t,e,t=>({mode:this.mode,selectionPoint:!0,selectionPointFeatureId:i,index:t})))}delete(){this.ids.length&&(this.store.delete(this.ids),this._selectionPoints=[])}getUpdated(t){if(0!==this._selectionPoints.length)return this._selectionPoints.map((e,i)=>({id:e,geometry:{type:"Point",coordinates:t[i]}}))}getOneUpdated(t,e){if(void 0!==this._selectionPoints[t])return{id:this._selectionPoints[t],geometry:{type:"Point",coordinates:e}}}}function At(t,e){let i=!1;for(let n=0,a=e.length;n<a;n++){const a=e[n];for(let e=0,n=a.length,l=n-1;e<n;l=e++)(r=a[e])[1]>(o=t)[1]!=(s=a[l])[1]>o[1]&&o[0]<(s[0]-r[0])*(o[1]-r[1])/(s[1]-r[1])+r[0]&&(i=!i)}var o,r,s;return i}const Tt=(t,e,i)=>{const o=t=>t*t,r=(t,e)=>o(t.x-e.x)+o(t.y-e.y);return Math.sqrt(((t,e,i)=>{const o=r(e,i);if(0===o)return r(t,e);let s=((t.x-e.x)*(i.x-e.x)+(t.y-e.y)*(i.y-e.y))/o;return s=Math.max(0,Math.min(1,s)),r(t,{x:e.x+s*(i.x-e.x),y:e.y+s*(i.y-e.y)})})(t,e,i))};class Rt extends dt{constructor(t,e,i){super(t),this.config=void 0,this.createClickBoundingBox=void 0,this.pixelDistance=void 0,this.config=t,this.createClickBoundingBox=e,this.pixelDistance=i}find(t,e){let i,o,r,s,n=Infinity,a=Infinity,l=Infinity;const h=this.createClickBoundingBox.create(t),c=this.store.search(h);for(let h=0;h<c.length;h++){const d=c[h],u=d.geometry;if("Point"===u.type){if(d.properties.selectionPoint||!e&&d.properties[N])continue;const o=this.pixelDistance.measure(t,u.coordinates);d.properties[N]&&o<this.pointerDistance&&o<l?(l=o,r=d):!d.properties[N]&&o<this.pointerDistance&&o<n&&(n=o,i=d)}else if("LineString"===u.type){if(i)continue;for(let e=0;e<u.coordinates.length-1;e++){const i=u.coordinates[e],r=u.coordinates[e+1],s=Tt({x:t.containerX,y:t.containerY},this.project(i[0],i[1]),this.project(r[0],r[1]));s<this.pointerDistance&&s<a&&(a=s,o=d)}}else if("Polygon"===u.type){if(i||o)continue;At([t.lng,t.lat],u.coordinates)&&(s=d)}}return{clickedFeature:i||o||s,clickedMidPoint:r}}}class Ut extends dt{constructor(t,e,i,o){super(t),this.config=void 0,this.featuresAtMouseEvent=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.draggedFeatureId=null,this.dragPosition=void 0,this.config=t,this.featuresAtMouseEvent=e,this.selectionPoints=i,this.midPoints=o}startDragging(t,e){this.draggedFeatureId=e,this.dragPosition=[t.lng,t.lat]}stopDragging(){this.draggedFeatureId=null,this.dragPosition=void 0}isDragging(){return null!==this.draggedFeatureId}canDrag(t,e){const{clickedFeature:i}=this.featuresAtMouseEvent.find(t,!0);return!(!i||i.id!==e)}drag(t,i){if(!this.draggedFeatureId)return;const o=this.store.getGeometryCopy(this.draggedFeatureId),r=[t.lng,t.lat];if("Polygon"===o.type||"LineString"===o.type){let s,n;if("Polygon"===o.type?(s=o.coordinates[0],n=s.length-1):(s=o.coordinates,n=s.length),!this.dragPosition)return!1;for(let t=0;t<n;t++){const i=s[t],o=[this.dragPosition[0]-r[0],this.dragPosition[1]-r[1]],n=e(i[0]-o[0],this.config.coordinatePrecision),a=e(i[1]-o[1],this.config.coordinatePrecision);if(n>180||n<-180||a>90||a<-90)return!1;s[t]=[n,a]}"Polygon"===o.type&&(s[s.length-1]=[s[0][0],s[0][1]]);const a=this.selectionPoints.getUpdated(s)||[],l=this.midPoints.getUpdated(s)||[];if(i&&!i({type:"Feature",id:this.draggedFeatureId,geometry:o,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:j.Provisional}))return!1;this.store.updateGeometry([{id:this.draggedFeatureId,geometry:o},...a,...l]),this.dragPosition=[t.lng,t.lat]}else"Point"===o.type&&(this.store.updateGeometry([{id:this.draggedFeatureId,geometry:{type:"Point",coordinates:r}}]),this.dragPosition=[t.lng,t.lat])}}class Xt extends dt{constructor(t,e,i,o){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.draggedCoordinate={id:null,index:-1},this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o}getClosestCoordinate(t,e){const i={dist:Infinity,index:-1,isFirstOrLastPolygonCoord:!1};let o;if("LineString"===e.type)o=e.coordinates;else{if("Polygon"!==e.type)return i;o=e.coordinates[0]}for(let r=0;r<o.length;r++){const s=this.pixelDistance.measure(t,o[r]);if(s<this.pointerDistance&&s<i.dist){const t="Polygon"===e.type&&(r===o.length-1||0===r);i.dist=s,i.index=t?0:r,i.isFirstOrLastPolygonCoord=t}}return i}getDraggableIndex(t,e){const i=this.store.getGeometryCopy(e),o=this.getClosestCoordinate(t,i);return-1===o.index?-1:o.index}drag(t,e,i){if(!this.draggedCoordinate.id)return!1;const o=this.draggedCoordinate.index,r=this.store.getGeometryCopy(this.draggedCoordinate.id),s="LineString"===r.type?r.coordinates:r.coordinates[0],n=[t.lng,t.lat];if(t.lng>180||t.lng<-180||t.lat>90||t.lat<-90)return!1;if("Polygon"!==r.type||o!==s.length-1&&0!==o)s[o]=n;else{const t=s.length-1;s[0]=n,s[t]=n}const a=this.selectionPoints.getOneUpdated(o,n),l=a?[a]:[],h=this.midPoints.getUpdated(s)||[];return!("Point"!==r.type&&!e&&ot({type:"Feature",geometry:r,properties:{}})||i&&!i({type:"Feature",id:this.draggedCoordinate.id,geometry:r,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:j.Provisional})||(this.store.updateGeometry([{id:this.draggedCoordinate.id,geometry:r},...l,...h]),0))}isDragging(){return null!==this.draggedCoordinate.id}startDragging(t,e){this.draggedCoordinate={id:t,index:e}}stopDragging(){this.draggedCoordinate={id:null,index:-1}}}function zt(t){let e=0,i=0,o=0;return("Polygon"===t.geometry.type?t.geometry.coordinates[0].slice(0,-1):t.geometry.coordinates).forEach(t=>{e+=t[0],i+=t[1],o++},!0),[e/o,i/o]}function Ht(t,e){t[0]+=t[0]-e[0]>180?-360:e[0]-t[0]>180?360:0;const i=H,o=e[1]*Math.PI/180,r=t[1]*Math.PI/180,s=r-o;let n=Math.abs(t[0]-e[0])*Math.PI/180;n>Math.PI&&(n-=2*Math.PI);const a=Math.log(Math.tan(r/2+Math.PI/4)/Math.tan(o/2+Math.PI/4)),l=Math.abs(a)>1e-11?s/a:Math.cos(o);return Math.sqrt(s*s+l*l*n*n)*i}function Yt(t){const e=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(t=>{const{x:e,y:i}=Q(t[0],t[1]);return[e,i]});return"Polygon"===t.geometry.type?function(t){let e=0,i=0,o=0;const r=t.length;for(let s=0;s<r-1;s++){const[r,n]=t[s],[a,l]=t[s+1],h=r*l-a*n;e+=h,i+=(r+a)*h,o+=(n+l)*h}return e/=2,i/=6*e,o/=6*e,{x:i,y:o}}(e):function(t){const e=t.length;let i=0,o=0;for(let r=0;r<e;r++){const[e,s]=t[r];i+=e,o+=s}return{x:i/e,y:o/e}}(e)}class Kt extends dt{constructor(t,e,i){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.lastBearing=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i}reset(){this.lastBearing=void 0}rotate(t,i,o){const r=this.store.getGeometryCopy(i);if("Polygon"!==r.type&&"LineString"!==r.type)return;const s=[t.lng,t.lat];let n;const a={type:"Feature",geometry:r,properties:{}};if("web-mercator"===this.config.projection){if(n=vt(Yt(a),Q(t.lng,t.lat)),!this.lastBearing)return void(this.lastBearing=n);((t,e)=>{if(0===e||360===e||-360===e)return t;const i=.017453292519943295*e,o=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(([t,e])=>Q(t,e)),r=o.reduce((t,e)=>({x:t.x+e.x,y:t.y+e.y}),{x:0,y:0});r.x/=o.length,r.y/=o.length;const s=o.map(t=>({x:r.x+(t.x-r.x)*Math.cos(i)-(t.y-r.y)*Math.sin(i),y:r.y+(t.x-r.x)*Math.sin(i)+(t.y-r.y)*Math.cos(i)})).map(({x:t,y:e})=>[tt(t,e).lng,tt(t,e).lat]);"Polygon"===t.geometry.type?t.geometry.coordinates[0]=s:t.geometry.coordinates=s})(a,-(this.lastBearing-n))}else{if("globe"!==this.config.projection)throw new Error("Unsupported projection");if(n=Lt(zt({type:"Feature",geometry:r,properties:{}}),s),!this.lastBearing)return void(this.lastBearing=n+180);!function(t,e){if(0===e||360===e||-360===e)return t;const i=zt(t);("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{const o=Lt(i,t)+e,r=Ht(i,t),s=jt(i,r,o);t[0]=s[0],t[1]=s[1]})}(a,-(this.lastBearing-(n+180)))}const l="Polygon"===r.type?r.coordinates[0]:r.coordinates;l.forEach(t=>{t[0]=e(t[0],this.coordinatePrecision),t[1]=e(t[1],this.coordinatePrecision)});const h=this.midPoints.getUpdated(l)||[],c=this.selectionPoints.getUpdated(l)||[];if(o&&!o({id:i,type:"Feature",geometry:r,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:j.Provisional}))return!1;this.store.updateGeometry([{id:i,geometry:r},...c,...h]),"web-mercator"===this.projection?this.lastBearing=n:"globe"===this.projection&&(this.lastBearing=n+180)}}class $t extends dt{constructor(t,e,i){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.lastDistance=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i}reset(){this.lastDistance=void 0}scale(t,o,r){const s=this.store.getGeometryCopy(o);if("Polygon"!==s.type&&"LineString"!==s.type)return;const n=[t.lng,t.lat],a={type:"Feature",geometry:s,properties:{}};let l;const h=Yt(a);if("web-mercator"===this.config.projection){const e=Q(t.lng,t.lat);l=i(h,e)}else{if("globe"!==this.config.projection)throw new Error("Invalid projection");l=z(zt({type:"Feature",geometry:s,properties:{}}),n)}if(!this.lastDistance)return void(this.lastDistance=l);const c=1-(this.lastDistance-l)/l;if("web-mercator"===this.config.projection){const{lng:t,lat:e}=tt(h.x,h.y);!function(t,e,i){if(1===e)return t;const o=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(([t,e])=>Q(t,e)),r=Q(i[0],i[1]),s=o.map(t=>({x:r.x+(t.x-r.x)*e,y:r.y+(t.y-r.y)*e})).map(({x:t,y:e})=>[tt(t,e).lng,tt(t,e).lat]);"Polygon"===t.geometry.type?t.geometry.coordinates[0]=s:t.geometry.coordinates=s}(a,c,[t,e])}else"globe"===this.config.projection&&function(t,e,i,o="xy"){1===e||("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{const r=Ht(i,t),s=Lt(i,t),n=jt(i,r*e,s);"x"!==o&&"xy"!==o||(t[0]=n[0]),"y"!==o&&"xy"!==o||(t[1]=n[1])})}(a,c,zt(a));const d="Polygon"===s.type?s.coordinates[0]:s.coordinates;d.forEach(t=>{t[0]=e(t[0],this.coordinatePrecision),t[1]=e(t[1],this.coordinatePrecision)});const u=this.midPoints.getUpdated(d)||[],g=this.selectionPoints.getUpdated(d)||[];if(r&&!r({id:o,type:"Feature",geometry:s,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:j.Provisional}))return!1;this.store.updateGeometry([{id:o,geometry:s},...g,...u]),this.lastDistance=l}}class Jt extends dt{constructor(t,e,i,o){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.minimumScale=1e-4,this.draggedCoordinate={id:null,index:-1},this.boundingBoxMaps={opposite:{0:4,1:5,2:6,3:7,4:0,5:1,6:2,7:3}},this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o}getClosestCoordinate(t,e){const i={dist:Infinity,index:-1,isFirstOrLastPolygonCoord:!1};let o;if("LineString"===e.type)o=e.coordinates;else{if("Polygon"!==e.type)return i;o=e.coordinates[0]}for(let r=0;r<o.length;r++){const s=this.pixelDistance.measure(t,o[r]);if(s<this.pointerDistance&&s<i.dist){const t="Polygon"===e.type&&(r===o.length-1||0===r);i.dist=s,i.index=t?0:r,i.isFirstOrLastPolygonCoord=t}}return i}isValidDragWebMercator(t,e,i){switch(t){case 0:if(e<=0||i>=0)return!1;break;case 1:if(i>=0)return!1;break;case 2:if(e>=0||i>=0)return!1;break;case 3:if(e>=0)return!1;break;case 4:if(e>=0||i<=0)return!1;break;case 5:if(i<=0)return!1;break;case 6:if(e<=0||i<=0)return!1;break;case 7:if(e<=0)return!1}return!0}getSelectedFeatureDataWebMercator(){if(!this.draggedCoordinate.id||-1===this.draggedCoordinate.index)return null;const t=this.getFeature(this.draggedCoordinate.id);if(!t)return null;const e=this.getNormalisedCoordinates(t.geometry);return{boundingBox:this.getBBoxWebMercator(e),feature:t,updatedCoords:e,selectedCoordinate:e[this.draggedCoordinate.index]}}centerWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{feature:i,boundingBox:o,updatedCoords:r,selectedCoordinate:s}=e,n=Yt(i);if(!n)return null;const a=Q(s[0],s[1]),{closestBBoxIndex:l}=this.getIndexesWebMercator(o,a),h=Q(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:l,updatedCoords:r,webMercatorCursor:h,webMercatorSelected:a,webMercatorOrigin:n}),r}centerFixedWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{feature:i,boundingBox:o,updatedCoords:r,selectedCoordinate:s}=e,n=Yt(i);if(!n)return null;const a=Q(s[0],s[1]),{closestBBoxIndex:l}=this.getIndexesWebMercator(o,a),h=Q(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:l,updatedCoords:r,webMercatorCursor:h,webMercatorSelected:a,webMercatorOrigin:n}),r}scaleFixedWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:o,webMercatorCursor:r,updatedCoords:s}){if(!this.isValidDragWebMercator(t,e.x-r.x,e.y-r.y))return null;let n=i(e,r)/i(e,o);return n<0&&(n=this.minimumScale),this.performWebMercatorScale(s,e.x,e.y,n,n),s}oppositeFixedWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{boundingBox:i,updatedCoords:o,selectedCoordinate:r}=e,s=Q(r[0],r[1]),{oppositeBboxIndex:n,closestBBoxIndex:a}=this.getIndexesWebMercator(i,s),l={x:i[n][0],y:i[n][1]},h=Q(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:a,updatedCoords:o,webMercatorCursor:h,webMercatorSelected:s,webMercatorOrigin:l}),o}oppositeWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{boundingBox:i,updatedCoords:o,selectedCoordinate:r}=e,s=Q(r[0],r[1]),{oppositeBboxIndex:n,closestBBoxIndex:a}=this.getIndexesWebMercator(i,s),l={x:i[n][0],y:i[n][1]},h=Q(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:a,updatedCoords:o,webMercatorCursor:h,webMercatorSelected:s,webMercatorOrigin:l}),o}scaleWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:i,webMercatorCursor:o,updatedCoords:r}){const s=e.x-o.x,n=e.y-o.y;if(!this.isValidDragWebMercator(t,s,n))return null;let a=1;0!==s&&1!==t&&5!==t&&(a=1-(e.x-i.x-s)/s);let l=1;return 0!==n&&3!==t&&7!==t&&(l=1-(e.y-i.y-n)/n),this.validateScale(a,l)?(a<0&&(a=this.minimumScale),l<0&&(l=this.minimumScale),this.performWebMercatorScale(r,e.x,e.y,a,l),r):null}getFeature(t){if(null===this.draggedCoordinate.id)return null;const e=this.store.getGeometryCopy(t);return"Polygon"!==e.type&&"LineString"!==e.type?null:{type:"Feature",geometry:e,properties:{}}}getNormalisedCoordinates(t){return"Polygon"===t.type?t.coordinates[0]:t.coordinates}validateScale(t,e){const i=!isNaN(t)&&e<Number.MAX_SAFE_INTEGER,o=!isNaN(e)&&e<Number.MAX_SAFE_INTEGER;return i&&o}performWebMercatorScale(t,e,i,o,r){t.forEach(t=>{const{x:s,y:n}=Q(t[0],t[1]),a=e+(s-e)*o,l=i+(n-i)*r,{lng:h,lat:c}=tt(a,l);t[0]=h,t[1]=c})}getBBoxWebMercator(t){const e=[Infinity,Infinity,-Infinity,-Infinity];(t=t.map(t=>{const{x:e,y:i}=Q(t[0],t[1]);return[e,i]})).forEach(([t,i])=>{t<e[0]&&(e[0]=t),i<e[1]&&(e[1]=i),t>e[2]&&(e[2]=t),i>e[3]&&(e[3]=i)});const[i,o,r,s]=e;return[[i,s],[(i+r)/2,s],[r,s],[r,s+(o-s)/2],[r,o],[(i+r)/2,o],[i,o],[i,s+(o-s)/2]]}getIndexesWebMercator(t,e){let o,r=Infinity;for(let s=0;s<t.length;s++){const n=i({x:e.x,y:e.y},{x:t[s][0],y:t[s][1]});n<r&&(o=s,r=n)}if(void 0===o)throw new Error("No closest coordinate found");return{oppositeBboxIndex:this.boundingBoxMaps.opposite[o],closestBBoxIndex:o}}isDragging(){return null!==this.draggedCoordinate.id}startDragging(t,e){this.draggedCoordinate={id:t,index:e}}stopDragging(){this.draggedCoordinate={id:null,index:-1}}getDraggableIndex(t,e){const i=this.store.getGeometryCopy(e),o=this.getClosestCoordinate(t,i);return-1===o.index?-1:o.index}drag(t,i,o){if(!this.draggedCoordinate.id)return!1;const r=this.getFeature(this.draggedCoordinate.id);if(!r)return!1;let s=null;if("center"===i?s=this.centerWebMercatorDrag(t):"opposite"===i?s=this.oppositeWebMercatorDrag(t):"center-fixed"===i?s=this.centerFixedWebMercatorDrag(t):"opposite-fixed"===i&&(s=this.oppositeFixedWebMercatorDrag(t)),!s)return!1;for(let t=0;t<s.length;t++){const i=s[t];if(i[0]=e(i[0],this.coordinatePrecision),i[1]=e(i[1],this.coordinatePrecision),!st(i,this.coordinatePrecision))return!1}const n=this.midPoints.getUpdated(s)||[],a=this.selectionPoints.getUpdated(s)||[],l={type:r.geometry.type,coordinates:"Polygon"===r.geometry.type?[s]:s};return!(o&&!o({id:this.draggedCoordinate.id,type:"Feature",geometry:l,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:j.Provisional})||(this.store.updateGeometry([{id:this.draggedCoordinate.id,geometry:l},...a,...n]),0))}}class Zt extends X{constructor(e){var i;super(e),this.mode="select",this.allowManualDeselection=!0,this.dragEventThrottle=5,this.dragEventCount=0,this.selected=[],this.flags=void 0,this.keyEvents=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.featuresAtMouseEvent=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.dragFeature=void 0,this.dragCoordinate=void 0,this.rotateFeature=void 0,this.scaleFeature=void 0,this.dragCoordinateResizeFeature=void 0,this.cursors=void 0,this.validations={},this.flags=e&&e.flags?e.flags:{};const o={pointerOver:"move",dragStart:"move",dragEnd:"move",insertMidpoint:"crosshair"};if(this.cursors=e&&e.cursors?t({},o,e.cursors):o,null===(null==e?void 0:e.keyEvents))this.keyEvents={deselect:null,delete:null,rotate:null,scale:null};else{const i={deselect:"Escape",delete:"Delete",rotate:["Control","r"],scale:["Control","s"]};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}if(this.dragEventThrottle=e&&void 0!==e.dragEventThrottle&&e.dragEventThrottle||5,this.allowManualDeselection=null==(i=null==e?void 0:e.allowManualDeselection)||i,e&&e.flags&&e.flags)for(const t in e.flags){const i=e.flags[t].feature;i&&i.validation&&(this.validations[t]=i.validation)}}selectFeature(t){this.select(t,!1)}setSelecting(){if("started"!==this._state)throw new Error("Mode must be started to move to selecting state");this._state="selecting"}registerBehaviors(t){this.pixelDistance=new pt(t),this.clickBoundingBox=new gt(t),this.featuresAtMouseEvent=new Rt(t,this.clickBoundingBox,this.pixelDistance),this.selectionPoints=new Vt(t),this.midPoints=new Nt(t,this.selectionPoints),this.rotateFeature=new Kt(t,this.selectionPoints,this.midPoints),this.scaleFeature=new $t(t,this.selectionPoints,this.midPoints),this.dragFeature=new Ut(t,this.featuresAtMouseEvent,this.selectionPoints,this.midPoints),this.dragCoordinate=new Xt(t,this.pixelDistance,this.selectionPoints,this.midPoints),this.dragCoordinateResizeFeature=new Jt(t,this.pixelDistance,this.selectionPoints,this.midPoints)}deselectFeature(){this.deselect()}deselect(){const t=this.selected.filter(t=>this.store.has(t)).map(t=>({id:t,property:G,value:!1}));this.store.updateProperty(t),this.onDeselect(this.selected[0]),this.selected=[],this.selectionPoints.delete(),this.midPoints.delete()}deleteSelected(){this.store.delete(this.selected),this.selected=[]}onRightClick(t){if(!this.selectionPoints.ids.length)return;let e,i=Infinity;if(this.selectionPoints.ids.forEach(o=>{const r=this.store.getGeometryCopy(o),s=this.pixelDistance.measure(t,r.coordinates);s<this.pointerDistance&&s<i&&(i=s,e=this.store.getPropertiesCopy(o))}),!e)return;const o=e.selectionPointFeatureId,r=e.index,s=this.store.getPropertiesCopy(o),n=this.flags[s.mode],a=this.validations[s.mode];if(!(n&&n.feature&&n.feature.coordinates&&n.feature.coordinates.deletable))return;const l=this.store.getGeometryCopy(o);let h;if("Polygon"===l.type){if(h=l.coordinates[0],h.length<=4)return}else if("LineString"===l.type&&(h=l.coordinates,h.length<=3))return;if(h){if("Polygon"===l.type&&0===r||r===h.length-1?(h.shift(),h.pop(),h.push([h[0][0],h[0][1]])):h.splice(r,1),a&&!a({id:o,type:"Feature",geometry:l,properties:s},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:j.Commit}))return;this.store.delete([...this.midPoints.ids,...this.selectionPoints.ids]),this.store.updateGeometry([{id:o,geometry:l}]),this.selectionPoints.create(h,l.type,o),n&&n.feature&&n.feature.coordinates&&n.feature.coordinates.midpoints&&this.midPoints.create(h,o,this.coordinatePrecision)}}select(t,e=!0){if(this.selected[0]===t)return;const{mode:i}=this.store.getPropertiesCopy(t),o=this.flags[i];if(!o||!o.feature)return;const r=this.selected[0];if(r){if(r===t)return;this.deselect()}e&&this.setCursor(this.cursors.pointerOver),this.selected=[t],this.store.updateProperty([{id:t,property:"selected",value:!0}]),this.onSelect(t);const{type:s,coordinates:n}=this.store.getGeometryCopy(t);if("LineString"!==s&&"Polygon"!==s)return;const a="LineString"===s?n:n[0];a&&o&&o.feature.coordinates&&(this.selectionPoints.create(a,s,t),o.feature.coordinates.midpoints&&this.midPoints.create(a,t,this.coordinatePrecision))}onLeftClick(t){const{clickedFeature:e,clickedMidPoint:i}=this.featuresAtMouseEvent.find(t,this.selected.length>0);if(this.selected.length&&i)this.midPoints.insert(i.id,this.coordinatePrecision);else if(e&&e.id)this.select(e.id,!0);else if(this.selected.length&&this.allowManualDeselection)return void this.deselect()}start(){this.setStarted(),this.setSelecting()}stop(){this.cleanUp(),this.setStarted(),this.setStopped()}onClick(t){"right"!==t.button?"left"===t.button&&this.onLeftClick(t):this.onRightClick(t)}canScale(t){return this.keyEvents.scale&&this.keyEvents.scale.every(e=>t.heldKeys.includes(e))}canRotate(t){return this.keyEvents.rotate&&this.keyEvents.rotate.every(e=>t.heldKeys.includes(e))}preventDefaultKeyEvent(t){const e=this.canRotate(t),i=this.canScale(t);(e||i)&&t.preventDefault()}onKeyDown(t){this.preventDefaultKeyEvent(t)}onKeyUp(t){if(this.preventDefaultKeyEvent(t),this.keyEvents.delete&&t.key===this.keyEvents.delete){if(!this.selected.length)return;this.onDeselect(this.selected[0]),this.deleteSelected(),this.selectionPoints.delete(),this.midPoints.delete()}else this.keyEvents.deselect&&t.key===this.keyEvents.deselect&&this.cleanUp()}cleanUp(){this.selected.length&&this.deselect()}onDragStart(t,e){if(!this.selected.length)return;const i=this.store.getPropertiesCopy(this.selected[0]),o=this.flags[i.mode];if(!(o&&o.feature&&(o.feature.draggable||o.feature.coordinates&&o.feature.coordinates.draggable||o.feature.coordinates&&o.feature.coordinates.resizable)))return;this.dragEventCount=0;const r=this.selected[0],s=this.dragCoordinate.getDraggableIndex(t,r);return o&&o.feature&&o.feature.coordinates&&(o.feature.coordinates.draggable||o.feature.coordinates.resizable)&&-1!==s?(this.setCursor(this.cursors.dragStart),o.feature.coordinates.resizable?this.dragCoordinateResizeFeature.startDragging(r,s):this.dragCoordinate.startDragging(r,s),void e(!1)):o&&o.feature&&o.feature.draggable&&this.dragFeature.canDrag(t,r)?(this.setCursor(this.cursors.dragStart),this.dragFeature.startDragging(t,r),void e(!1)):void 0}onDrag(t,e){const i=this.selected[0];if(!i)return;const o=this.store.getPropertiesCopy(i),r=this.flags[o.mode],s=!0===(r&&r.feature&&r.feature.selfIntersectable);if(this.dragEventCount++,this.dragEventCount%this.dragEventThrottle==0)return;const n=this.validations[o.mode];if(r&&r.feature&&r.feature.rotateable&&this.canRotate(t))return e(!1),void this.rotateFeature.rotate(t,i,n);if(r&&r.feature&&r.feature.scaleable&&this.canScale(t))return e(!1),void this.scaleFeature.scale(t,i,n);if(this.dragCoordinateResizeFeature.isDragging()&&r.feature&&r.feature.coordinates&&r.feature.coordinates.resizable){if("globe"===this.projection)throw new Error("Globe is currently unsupported projection for resizable");return e(!1),void this.dragCoordinateResizeFeature.drag(t,r.feature.coordinates.resizable,n)}this.dragCoordinate.isDragging()?this.dragCoordinate.drag(t,s,n):this.dragFeature.isDragging()?this.dragFeature.drag(t,n):e(!0)}onDragEnd(t,e){this.setCursor(this.cursors.dragEnd),this.dragCoordinate.isDragging()?this.onFinish(this.selected[0],{mode:this.mode,action:"dragCoordinate"}):this.dragFeature.isDragging()?this.onFinish(this.selected[0],{mode:this.mode,action:"dragFeature"}):this.dragCoordinateResizeFeature.isDragging()&&this.onFinish(this.selected[0],{mode:this.mode,action:"dragCoordinateResize"}),this.dragCoordinate.stopDragging(),this.dragFeature.stopDragging(),this.dragCoordinateResizeFeature.stopDragging(),this.rotateFeature.reset(),this.scaleFeature.reset(),e(!0)}onMouseMove(t){if(!this.selected.length)return void this.setCursor("unset");if(this.dragFeature.isDragging())return;let e=!1;this.midPoints.ids.forEach(i=>{if(e)return;const o=this.store.getGeometryCopy(i);this.pixelDistance.measure(t,o.coordinates)<this.pointerDistance&&(e=!0)});let i=!1;if(this.selectionPoints.ids.forEach(o=>{const r=this.store.getGeometryCopy(o);this.pixelDistance.measure(t,r.coordinates)<this.pointerDistance&&(e=!1,i=!0)}),e)return void this.setCursor(this.cursors.insertMidpoint);const{clickedFeature:o}=this.featuresAtMouseEvent.find(t,!0);this.setCursor(this.selected.length>0&&(o&&o.id===this.selected[0]||i)?this.cursors.pointerOver:"unset")}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});if(e.properties.mode===this.mode&&"Point"===e.geometry.type){if(e.properties.selectionPoint)return i.pointColor=this.getHexColorStylingValue(this.styles.selectionPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectionPointOutlineColor,i.pointOutlineColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.selectionPointWidth,i.pointWidth,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectionPointOutlineWidth,2,e),i.zIndex=30,i;if(e.properties.midPoint)return i.pointColor=this.getHexColorStylingValue(this.styles.midPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.midPointOutlineColor,i.pointOutlineColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.midPointWidth,4,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.midPointOutlineWidth,2,e),i.zIndex=40,i}else if(e.properties[G]){if("Polygon"===e.geometry.type)return i.polygonFillColor=this.getHexColorStylingValue(this.styles.selectedPolygonColor,i.polygonFillColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.selectedPolygonOutlineWidth,i.polygonOutlineWidth,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.selectedPolygonOutlineColor,i.polygonOutlineColor,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.selectedPolygonFillOpacity,i.polygonFillOpacity,e),i.zIndex=10,i;if("LineString"===e.geometry.type)return i.lineStringColor=this.getHexColorStylingValue(this.styles.selectedLineStringColor,i.lineStringColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.selectedLineStringWidth,i.lineStringWidth,e),i.zIndex=10,i;if("Point"===e.geometry.type)return i.pointWidth=this.getNumericStylingValue(this.styles.selectedPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.selectedPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectedPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectedPointOutlineWidth,i.pointOutlineWidth,e),i.zIndex=10,i}return i}}class qt extends U{constructor(...t){super(...t),this.type=R.Static,this.mode="static"}start(){}stop(){}onKeyUp(){}onKeyDown(){}onClick(){}onDragStart(){}onDrag(){}onDragEnd(){}onMouseMove(){}cleanUp(){}styleFeature(){return t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0})}}function Qt(t,e,i,o,r){for(;o>i;){if(o-i>600){const s=o-i+1,n=e-i+1,a=Math.log(s),l=.5*Math.exp(2*a/3),h=.5*Math.sqrt(a*l*(s-l)/s)*(n-s/2<0?-1:1);Qt(t,e,Math.max(i,Math.floor(e-n*l/s+h)),Math.min(o,Math.floor(e+(s-n)*l/s+h)),r)}const s=t[e];let n=i,a=o;for(te(t,i,e),r(t[o],s)>0&&te(t,i,o);n<a;){for(te(t,n,a),n++,a--;r(t[n],s)<0;)n++;for(;r(t[a],s)>0;)a--}0===r(t[i],s)?te(t,i,a):(a++,te(t,a,o)),a<=e&&(i=a+1),e<=a&&(o=a-1)}}function te(t,e,i){const o=t[e];t[e]=t[i],t[i]=o}function ee(t,e){ie(t,0,t.children.length,e,t)}function ie(t,e,i,o,r){r||(r=de([])),r.minX=Infinity,r.minY=Infinity,r.maxX=-Infinity,r.maxY=-Infinity;for(let s=e;s<i;s++){const e=t.children[s];oe(r,t.leaf?o(e):e)}return r}function oe(t,e){return t.minX=Math.min(t.minX,e.minX),t.minY=Math.min(t.minY,e.minY),t.maxX=Math.max(t.maxX,e.maxX),t.maxY=Math.max(t.maxY,e.maxY),t}function re(t,e){return t.minX-e.minX}function se(t,e){return t.minY-e.minY}function ne(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function ae(t){return t.maxX-t.minX+(t.maxY-t.minY)}function le(t,e){const i=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),r=Math.min(t.maxX,e.maxX),s=Math.min(t.maxY,e.maxY);return Math.max(0,r-i)*Math.max(0,s-o)}function he(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function ce(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function de(t){return{children:t,height:1,leaf:!0,minX:Infinity,minY:Infinity,maxX:-Infinity,maxY:-Infinity}}function ue(t,e,i,o,r){const s=[e,i];for(;s.length;){if((i=s.pop())-(e=s.pop())<=o)continue;const n=e+Math.ceil((i-e)/o/2)*o;Qt(t,n,e,i,r),s.push(e,n,n,i)}}class ge{constructor(t){this._maxEntries=void 0,this._minEntries=void 0,this.data=void 0,this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(.4*this._maxEntries)),this.clear()}search(t){let e=this.data;const i=[];if(!ce(t,e))return i;const o=this.toBBox,r=[];for(;e;){for(let s=0;s<e.children.length;s++){const n=e.children[s],a=e.leaf?o(n):n;ce(t,a)&&(e.leaf?i.push(n):he(t,a)?this._all(n,i):r.push(n))}e=r.pop()}return i}collides(t){let e=this.data;if(ce(t,e)){const i=[];for(;e;){for(let o=0;o<e.children.length;o++){const r=e.children[o],s=e.leaf?this.toBBox(r):r;if(ce(t,s)){if(e.leaf||he(t,s))return!0;i.push(r)}}e=i.pop()}}return!1}load(t){if(t.length<this._minEntries){for(let e=0;e<t.length;e++)this.insert(t[e]);return}let e=this._build(t.slice(),0,t.length-1,0);if(this.data.children.length)if(this.data.height===e.height)this._splitRoot(this.data,e);else{if(this.data.height<e.height){const t=this.data;this.data=e,e=t}this._insert(e,this.data.height-e.height-1,!0)}else this.data=e}insert(t){this._insert(t,this.data.height-1)}clear(){this.data=de([])}remove(t){let e=this.data;const i=this.toBBox(t),o=[],r=[];let s,n,a=!1;for(;e||o.length;){if(e||(e=o.pop(),n=o[o.length-1],s=r.pop(),a=!0),e.leaf){const i=e.children.indexOf(t);-1!==i&&(e.children.splice(i,1),o.push(e),this._condense(o))}a||e.leaf||!he(e,i)?n?(s++,e=n.children[s],a=!1):e=null:(o.push(e),r.push(s),s=0,n=e,e=e.children[0])}}toBBox(t){return t}compareMinX(t,e){return t.minX-e.minX}compareMinY(t,e){return t.minY-e.minY}_all(t,e){const i=[];for(;t;)t.leaf?e.push(...t.children):i.push(...t.children),t=i.pop();return e}_build(t,e,i,o){const r=i-e+1;let s,n=this._maxEntries;if(r<=n)return s=de(t.slice(e,i+1)),ee(s,this.toBBox),s;o||(o=Math.ceil(Math.log(r)/Math.log(n)),n=Math.ceil(r/Math.pow(n,o-1))),s=de([]),s.leaf=!1,s.height=o;const a=Math.ceil(r/n),l=a*Math.ceil(Math.sqrt(n));ue(t,e,i,l,this.compareMinX);for(let r=e;r<=i;r+=l){const e=Math.min(r+l-1,i);ue(t,r,e,a,this.compareMinY);for(let i=r;i<=e;i+=a){const r=Math.min(i+a-1,e);s.children.push(this._build(t,i,r,o-1))}}return ee(s,this.toBBox),s}_chooseSubtree(t,e,i,o){for(;o.push(e),!e.leaf&&o.length-1!==i;){let i,o=Infinity,n=Infinity;for(let a=0;a<e.children.length;a++){const l=e.children[a],h=ne(l),c=(r=t,s=l,(Math.max(s.maxX,r.maxX)-Math.min(s.minX,r.minX))*(Math.max(s.maxY,r.maxY)-Math.min(s.minY,r.minY))-h);c<n?(n=c,o=h<o?h:o,i=l):c===n&&h<o&&(o=h,i=l)}e=i||e.children[0]}var r,s;return e}_insert(t,e,i){const o=i?t:this.toBBox(t),r=[],s=this._chooseSubtree(o,this.data,e,r);for(s.children.push(t),oe(s,o);e>=0&&r[e].children.length>this._maxEntries;)this._split(r,e),e--;this._adjustParentBBoxes(o,r,e)}_split(t,e){const i=t[e],o=i.children.length,r=this._minEntries;this._chooseSplitAxis(i,r,o);const s=this._chooseSplitIndex(i,r,o),n=de(i.children.splice(s,i.children.length-s));n.height=i.height,n.leaf=i.leaf,ee(i,this.toBBox),ee(n,this.toBBox),e?t[e-1].children.push(n):this._splitRoot(i,n)}_splitRoot(t,e){this.data=de([t,e]),this.data.height=t.height+1,this.data.leaf=!1,ee(this.data,this.toBBox)}_chooseSplitIndex(t,e,i){let o,r=Infinity,s=Infinity;for(let n=e;n<=i-e;n++){const e=ie(t,0,n,this.toBBox),a=ie(t,n,i,this.toBBox),l=le(e,a),h=ne(e)+ne(a);l<r?(r=l,o=n,s=h<s?h:s):l===r&&h<s&&(s=h,o=n)}return o||i-e}_chooseSplitAxis(t,e,i){const o=t.leaf?this.compareMinX:re,r=t.leaf?this.compareMinY:se;this._allDistMargin(t,e,i,o)<this._allDistMargin(t,e,i,r)&&t.children.sort(o)}_allDistMargin(t,e,i,o){t.children.sort(o);const r=this.toBBox,s=ie(t,0,e,r),n=ie(t,i-e,i,r);let a=ae(s)+ae(n);for(let o=e;o<i-e;o++){const e=t.children[o];oe(s,t.leaf?r(e):e),a+=ae(s)}for(let o=i-e-1;o>=e;o--){const e=t.children[o];oe(n,t.leaf?r(e):e),a+=ae(n)}return a}_adjustParentBBoxes(t,e,i){for(let o=i;o>=0;o--)oe(e[o],t)}_condense(t){for(let e,i=t.length-1;i>=0;i--)0===t[i].children.length?i>0?(e=t[i-1].children,e.splice(e.indexOf(t[i]),1)):this.clear():ee(t[i],this.toBBox)}}class pe{constructor(t){this.tree=void 0,this.idToNode=void 0,this.nodeToId=void 0,this.tree=new ge(t&&t.maxEntries?t.maxEntries:9),this.idToNode=new Map,this.nodeToId=new Map}setMaps(t,e){this.idToNode.set(t.id,e),this.nodeToId.set(e,t.id)}toBBox(t){const e=[],i=[];let o;if("Polygon"===t.geometry.type)o=t.geometry.coordinates[0];else if("LineString"===t.geometry.type)o=t.geometry.coordinates;else{if("Point"!==t.geometry.type)throw new Error("Not a valid feature to turn into a bounding box");o=[t.geometry.coordinates]}for(let t=0;t<o.length;t++)i.push(o[t][1]),e.push(o[t][0]);const r=Math.min(...i),s=Math.max(...i);return{minX:Math.min(...e),minY:r,maxX:Math.max(...e),maxY:s}}insert(t){if(this.idToNode.get(String(t.id)))throw new Error("Feature already exists");const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}load(t){const e=[],i=new Set;t.forEach(t=>{const o=this.toBBox(t);if(this.setMaps(t,o),i.has(String(t.id)))throw new Error(`Duplicate feature ID found ${t.id}`);i.add(String(t.id)),e.push(o)}),this.tree.load(e)}update(t){this.remove(t.id);const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}remove(t){const e=this.idToNode.get(t);if(!e)throw new Error(`${t} not inserted into the spatial index`);this.tree.remove(e)}clear(){this.tree.clear()}search(t){return this.tree.search(this.toBBox(t)).map(t=>this.nodeToId.get(t))}collides(t){return this.tree.collides(this.toBBox(t))}}const ye={getId:()=>"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}),isValidId:t=>"string"==typeof t&&36===t.length};class me{constructor(t){this.idStrategy=void 0,this.tracked=void 0,this.spatialIndex=void 0,this.store=void 0,this._onChange=()=>{},this.store={},this.spatialIndex=new pe,this.tracked=!t||!1!==t.tracked,this.idStrategy=t&&t.idStrategy?t.idStrategy:ye}clone(t){return JSON.parse(JSON.stringify(t))}getId(){return this.idStrategy.getId()}has(t){return Boolean(this.store[t])}load(t,e){if(0===t.length)return;const i=this.clone(t);i.forEach(t=>{null==t.id&&(t.id=this.idStrategy.getId()),this.tracked&&(t.properties.createdAt?T(t.properties.createdAt):t.properties.createdAt=+new Date,t.properties.updatedAt?T(t.properties.updatedAt):t.properties.updatedAt=+new Date)});const o=[];i.forEach(t=>{const i=t.id;if(e&&!e(t))throw new Error(`Feature ${i} is not valid: ${JSON.stringify(t)}`);if(this.has(i))throw new Error(`Feature already exists with this id: ${i}`);this.store[i]=t,o.push(i)}),this.spatialIndex.load(i),this._onChange(o,"create")}search(t,e){const i=this.spatialIndex.search(t).map(t=>this.store[t]);return this.clone(e?i.filter(e):i)}registerOnChange(t){this._onChange=(e,i)=>{t(e,i)}}getGeometryCopy(t){const e=this.store[t];if(!e)throw new Error(`No feature with this id (${t}), can not get geometry copy`);return this.clone(e.geometry)}getPropertiesCopy(t){const e=this.store[t];if(!e)throw new Error(`No feature with this id (${t}), can not get properties copy`);return this.clone(e.properties)}updateProperty(t){const e=[];t.forEach(({id:t,property:i,value:o})=>{const r=this.store[t];if(!r)throw new Error(`No feature with this (${t}), can not update geometry`);e.push(t),r.properties[i]=o,this.tracked&&(r.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(e,"update")}updateGeometry(t){const e=[];t.forEach(({id:t,geometry:i})=>{e.push(t);const o=this.store[t];if(!o)throw new Error(`No feature with this (${t}), can not update geometry`);o.geometry=this.clone(i),this.spatialIndex.update(o),this.tracked&&(o.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(e,"update")}create(e){const i=[];return e.forEach(({geometry:e,properties:o})=>{let r,s=t({},o);this.tracked&&(r=+new Date,o?(s.createdAt="number"==typeof o.createdAt?o.createdAt:r,s.updatedAt="number"==typeof o.updatedAt?o.updatedAt:r):s={createdAt:r,updatedAt:r});const n=this.getId(),a={id:n,type:"Feature",geometry:e,properties:s};this.store[n]=a,this.spatialIndex.insert(a),i.push(n)}),this._onChange&&this._onChange([...i],"create"),i}delete(t){t.forEach(t=>{if(!this.store[t])throw new Error("No feature with this id, can not delete");delete this.store[t],this.spatialIndex.remove(t)}),this._onChange&&this._onChange([...t],"delete")}copyAll(){return this.clone(Object.keys(this.store).map(t=>this.store[t]))}clear(){this.store={},this.spatialIndex.clear()}size(){return Object.keys(this.store).length}}function fe(t){const e=t.coordinates;let i=0;if(e&&e.length>0){i+=Math.abs(Pe(e[0]));for(let t=1;t<e.length;t++)i-=Math.abs(Pe(e[t]))}return i}const Ce=H*H/2,ve=Math.PI/180;function Pe(t){const e=t.length;if(e<=2)return 0;let i=0,o=0;for(;o<e;)i+=(t[o+2>=e?(o+2)%e:o+2][0]*ve-t[o][0]*ve)*Math.sin(t[o+1===e?0:o+1][1]*ve),o++;return i*Ce}const _e=(t,e)=>"Polygon"===t.geometry.type&&fe(t.geometry)>e,xe=(t,e)=>"Polygon"===t.geometry.type&&fe(t.geometry)<e,Se=t=>("Polygon"===t.geometry.type||"LineString"===t.geometry.type)&&!ot(t);function Me(t,e,i){const o=vt(t,e);let r=vt(e,i)-o;return r<0&&(r+=360),180-Math.abs(r-90-90)}class Ee extends U{constructor(e){super(e),this.mode="angled-rectangle",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=void 0,this.cursors=void 0,this.mouseMove=!1;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}}close(){if(void 0===this.currentId)return;const t=this.currentId;this.currentCoordinate=0,this.currentId=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;const e=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(1===this.currentCoordinate){const i=1/Math.pow(10,this.coordinatePrecision-1),r=Math.max(1e-6,i);o=[e[0],[t.lng,t.lat],[t.lng,t.lat-r],e[0]]}else if(2===this.currentCoordinate){const r=e[0],s=e[1],n=Wt(r,s,this.coordinatePrecision,this.project,this.unproject),a=Q(r[0],r[1]),l=Q(n[0],n[1]),h=Q(s[0],s[1]),c=Q(t.lng,t.lat),d=i(c,a)<i(c,h),u=Me(a,l,c),g=d?90-u:Me(a,l,c)-90,p=i(l,c),y=Math.cos(Y(g))*p,m=vt(a,h)+("right"===function(t,e,i){const o=(i.x-e.x)*(t.y-e.y)-(i.y-e.y)*(t.x-e.x);return o>1e-10?"left":o<-1e-10?"right":"left"}(a,h,c)?-90:90),f=ft(a,y,m),C=ft(h,y,m),v=tt(f.x,f.y),P=tt(C.x,C.y);o=[e[0],e[1],[P.lng,P.lat],[v.lng,v.lat],e[0]]}o&&this.updatePolygonGeometry(this.currentId,o,j.Provisional)}updatePolygonGeometry(t,e,i){const o={type:"Polygon",coordinates:[e]};return!(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i})||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,0===this.currentCoordinate){const[e]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const e=this.store.getGeometryCopy(this.currentId);if(Et([t.lng,t.lat],e.coordinates[0][0]))return;if(!this.updatePolygonGeometry(this.currentId,[e.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],e.coordinates[0][0]],j.Commit))return;this.currentCoordinate++}else 2===this.currentCoordinate&&this.currentId&&this.close()}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return e.properties.mode===this.mode&&"Polygon"===e.geometry.type&&(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10),i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}}function be(t,e,i){return(e.x-t.x)*(i.y-t.y)-(e.y-t.y)*(i.x-t.x)<=0}class we extends U{constructor(e){super(e),this.mode="sector",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=void 0,this.direction=void 0,this.arcPoints=void 0,this.cursors=void 0,this.mouseMove=!1;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.arcPoints=(null==e?void 0:e.arcPoints)||64}close(){if(void 0===this.currentId)return;const t=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.direction=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;const o=this.store.getGeometryCopy(this.currentId).coordinates[0];let r;if(1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),i=Math.max(1e-6,e);r=[o[0],[t.lng,t.lat],[t.lng,t.lat-i],o[0]]}else if(2===this.currentCoordinate){const s=o[0],n=o[1],a=[t.lng,t.lat],l=Q(s[0],s[1]),h=Q(n[0],n[1]),c=Q(a[0],a[1]);if(void 0===this.direction){const t=be(l,h,c);this.direction=t?"clockwise":"anticlockwise"}const d=i(l,h),u=vt(l,h),g=vt(l,c),p=this.arcPoints,y=[s],m=Pt(u),f=Pt(g);let C;"anticlockwise"===this.direction?(C=f-m,C<0&&(C+=360)):(C=m-f,C<0&&(C+=360));const v=("anticlockwise"===this.direction?1:-1)*C/p;y.push(n);for(let t=0;t<=p;t++){const i=ft(l,d,m+t*v),{lng:o,lat:r}=tt(i.x,i.y),s=[e(o,this.coordinatePrecision),e(r,this.coordinatePrecision)];s[0]!==y[y.length-1][0]&&s[1]!==y[y.length-1][1]&&y.push(s)}y.push(s),r=[...y]}r&&this.updatePolygonGeometry(this.currentId,r,j.Provisional)}updatePolygonGeometry(t,e,i){const o={type:"Polygon",coordinates:[e]};return!(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i})||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,0===this.currentCoordinate){const[e]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const e=this.store.getGeometryCopy(this.currentId);if(Et([t.lng,t.lat],e.coordinates[0][0]))return;if(!this.updatePolygonGeometry(this.currentId,[e.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],e.coordinates[0][0]],j.Commit))return;this.currentCoordinate++}else 2===this.currentCoordinate&&this.currentId&&this.close()}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentId=void 0,this.direction=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return e.properties.mode===this.mode&&"Polygon"===e.geometry.type&&(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10),i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}}class Ie extends U{constructor(e){super(e),this.mode="sensor",this.currentCoordinate=0,this.currentId=void 0,this.currentInitialArcId=void 0,this.currentStartingPointId=void 0,this.keyEvents=void 0,this.direction=void 0,this.arcPoints=void 0,this.cursors=void 0,this.mouseMove=!1;const i={start:"crosshair",close:"pointer"};if(this.cursors=e&&e.cursors?t({},i,e.cursors):i,null===(null==e?void 0:e.keyEvents))this.keyEvents={cancel:null,finish:null};else{const i={cancel:"Escape",finish:"Enter"};this.keyEvents=e&&e.keyEvents?t({},i,e.keyEvents):i}this.arcPoints=(null==e?void 0:e.arcPoints)||64}close(){if(void 0===this.currentStartingPointId)return;const t=this.currentStartingPointId,e=this.currentInitialArcId,i=this.currentId;t&&this.store.delete([t]),e&&this.store.delete([e]),this.currentCoordinate=0,this.currentStartingPointId=void 0,this.currentInitialArcId=void 0,this.currentId=void 0,this.direction=void 0,"drawing"===this.state&&this.setStarted(),i&&this.onFinish(i,{mode:this.mode,action:"draw"})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0!==this.currentInitialArcId&&void 0!==this.currentStartingPointId&&0!==this.currentCoordinate)if(2===this.currentCoordinate){const o=this.store.getGeometryCopy(this.currentInitialArcId).coordinates,r=this.store.getGeometryCopy(this.currentStartingPointId).coordinates,s=o[0],n=[t.lng,t.lat],a=Q(s[0],s[1]),l=Q(n[0],n[1]),h=Q(r[0],r[1]),c=i(h,a);if(void 0===this.direction){const t=be(h,a,l);this.direction=t?"clockwise":"anticlockwise"}const d=vt(h,a),u=vt(h,l),g=this.arcPoints,p=[s],y=Pt(d),m=Pt(u);let f;"anticlockwise"===this.direction?(f=m-y,f<0&&(f+=360)):(f=y-m,f<0&&(f+=360));const C=("anticlockwise"===this.direction?1:-1)*f/g;for(let t=0;t<=g;t++){const i=ft(h,c,y+t*C),{lng:o,lat:r}=tt(i.x,i.y),s=[e(o,this.coordinatePrecision),e(r,this.coordinatePrecision)];s[0]!==p[p.length-1][0]&&s[1]!==p[p.length-1][1]&&p.push(s)}this.updateLineStringGeometry(this.currentInitialArcId,p,j.Provisional)}else if(3===this.currentCoordinate){const o=this.store.getGeometryCopy(this.currentInitialArcId).coordinates;if(o.length<2)return;if(!this.direction)return;const r=this.store.getGeometryCopy(this.currentStartingPointId).coordinates,s=o[0],n=o[o.length-1],a=Q(t.lng,t.lat),l=Q(s[0],s[1]),h=Q(n[0],n[1]),c=Q(r[0],r[1]),d=i(c,l),u=i(c,a)<d?l:a,g=vt(c,a),p=vt(c,l),y=vt(c,h),m=Pt(p),f=Pt(y),C=Pt(g);if(this.notInSector({normalizedCursor:C,normalizedStart:m,normalizedEnd:f,direction:this.direction}))return;const v=this.getDeltaBearing(this.direction,m,f),P=this.arcPoints,_=("anticlockwise"===this.direction?1:-1)*v/P,x=i(c,u),S=[];for(let t=0;t<=P;t++){const i=ft(c,x,m+t*_),{lng:r,lat:s}=tt(i.x,i.y),n=[e(r,this.coordinatePrecision),e(s,this.coordinatePrecision)];n[0]!==o[o.length-1][0]&&n[1]!==o[o.length-1][1]&&S.unshift(n)}o.push(...S),o.push(o[0]),this.currentId?this.updatePolygonGeometry(this.currentId,o,j.Provisional):[this.currentId]=this.store.create([{geometry:{type:"Polygon",coordinates:[o]},properties:{mode:this.mode}}])}}updateLineStringGeometry(t,e,i){const o={type:"LineString",coordinates:e};return!(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i})||(this.store.updateGeometry([{id:t,geometry:o}]),0))}updatePolygonGeometry(t,e,i){const o={type:"Polygon",coordinates:[e]};return!(this.validate&&!this.validate({type:"Feature",geometry:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i})||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,0===this.currentCoordinate){const[e]=this.store.create([{geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:{mode:this.mode}}]);this.currentStartingPointId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentStartingPointId){const[e]=this.store.create([{geometry:{type:"LineString",coordinates:[[t.lng,t.lat],[t.lng,t.lat]]},properties:{mode:this.mode}}]);this.currentInitialArcId=e,this.currentCoordinate++}else 2===this.currentCoordinate&&this.currentStartingPointId?this.currentCoordinate++:3===this.currentCoordinate&&this.currentStartingPointId&&this.close()}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentStartingPointId&&this.store.delete([this.currentStartingPointId]),this.currentInitialArcId&&this.store.delete([this.currentInitialArcId]),this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentStartingPointId=void 0,this.direction=void 0,this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return e.properties.mode===this.mode&&("Polygon"===e.geometry.type?(i.polygonFillColor=this.getHexColorStylingValue(this.styles.fillColor,i.polygonFillColor,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=10):"LineString"===e.geometry.type?(i.lineStringColor=this.getHexColorStylingValue(this.styles.outlineColor,i.polygonOutlineColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.outlineWidth,i.polygonOutlineWidth,e),i.zIndex=10):"Point"===e.geometry.type&&(i.pointColor=this.getHexColorStylingValue(this.styles.centerPointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.centerPointWidth,i.pointWidth,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.centerPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.centerPointOutlineWidth,i.pointOutlineWidth,e),i.zIndex=20)),i}validateFeature(t){return!!super.validateFeature(t)&&t.properties.mode===this.mode&&at(t,this.coordinatePrecision)}getDeltaBearing(t,e,i){let o;return"anticlockwise"===t?(o=i-e,o<0&&(o+=360)):(o=e-i,o<0&&(o+=360)),o}notInSector({normalizedCursor:t,normalizedStart:e,normalizedEnd:i,direction:o}){return"clockwise"===o?e<=i?t>=e&&t<=i:t>=e||t<=i:e>=i?t<=e&&t>=i:t<=e||t>=i}}var Fe={__proto__:null,TerraDrawBaseDrawMode:U,TerraDrawBaseAdapter:r};class De{constructor(e){this._modes=void 0,this._mode=void 0,this._adapter=void 0,this._enabled=!1,this._store=void 0,this._eventListeners=void 0,this._instanceSelectMode=void 0,this._adapter=e.adapter,this._mode=new qt;const i=new Set,o=e.modes.reduce((t,e)=>{if(i.has(e.mode))throw new Error(`There is already a ${e.mode} mode provided`);return i.add(e.mode),t[e.mode]=e,t},{}),r=Object.keys(o);if(0===r.length)throw new Error("No modes provided");r.forEach(t=>{if(o[t].type===R.Select){if(this._instanceSelectMode)throw new Error("only one type of select mode can be provided");this._instanceSelectMode=t}}),this._modes=t({},o,{static:this._mode}),this._eventListeners={change:[],select:[],deselect:[],finish:[],ready:[]},this._store=new me({tracked:!!e.tracked,idStrategy:e.idStrategy?e.idStrategy:void 0});const s=t=>{const e=[],i=this._store.copyAll().filter(i=>!t.includes(i.id)||(e.push(i),!1));return{changed:e,unchanged:i}},n=(t,e)=>{this._enabled&&this._eventListeners.finish.forEach(i=>{i(t,e)})},a=(t,e)=>{if(!this._enabled)return;this._eventListeners.change.forEach(i=>{i(t,e)});const{changed:i,unchanged:o}=s(t);"create"===e?this._adapter.render({created:i,deletedIds:[],unchanged:o,updated:[]},this.getModeStyles()):"update"===e?this._adapter.render({created:[],deletedIds:[],unchanged:o,updated:i},this.getModeStyles()):"delete"===e?this._adapter.render({created:[],deletedIds:t,unchanged:o,updated:[]},this.getModeStyles()):"styling"===e&&this._adapter.render({created:[],deletedIds:[],unchanged:o,updated:[]},this.getModeStyles())},l=t=>{if(!this._enabled)return;this._eventListeners.select.forEach(e=>{e(t)});const{changed:e,unchanged:i}=s([t]);this._adapter.render({created:[],deletedIds:[],unchanged:i,updated:e},this.getModeStyles())},h=t=>{if(!this._enabled)return;this._eventListeners.deselect.forEach(t=>{t()});const{changed:e,unchanged:i}=s([t]);e&&this._adapter.render({created:[],deletedIds:[],unchanged:i,updated:e},this.getModeStyles())};Object.keys(this._modes).forEach(t=>{this._modes[t].register({mode:t,store:this._store,setCursor:this._adapter.setCursor.bind(this._adapter),project:this._adapter.project.bind(this._adapter),unproject:this._adapter.unproject.bind(this._adapter),setDoubleClickToZoom:this._adapter.setDoubleClickToZoom.bind(this._adapter),onChange:a,onSelect:l,onDeselect:h,onFinish:n,coordinatePrecision:this._adapter.getCoordinatePrecision()})})}checkEnabled(){if(!this._enabled)throw new Error("Terra Draw is not enabled")}getModeStyles(){const t={};return Object.keys(this._modes).forEach(e=>{t[e]=t=>this._instanceSelectMode&&t.properties[G]?this._modes[this._instanceSelectMode].styleFeature.bind(this._modes[this._instanceSelectMode])(t):this._modes[e].styleFeature.bind(this._modes[e])(t)}),t}featuresAtLocation({lng:t,lat:e},o){const r=o&&void 0!==o.pointerDistance?o.pointerDistance:30,s=!o||void 0===o.ignoreSelectFeatures||o.ignoreSelectFeatures,n=this._adapter.unproject.bind(this._adapter),a=this._adapter.project.bind(this._adapter),l=a(t,e),h=ut({unproject:n,point:l,pointerDistance:r});return this._store.search(h).filter(o=>{if(s&&(o.properties[N]||o.properties.selectionPoint))return!1;if("Point"===o.geometry.type){const t=o.geometry.coordinates,e=a(t[0],t[1]);return i(l,e)<r}if("LineString"===o.geometry.type){const t=o.geometry.coordinates;for(let e=0;e<t.length-1;e++){const i=t[e],o=t[e+1];if(Tt(l,a(i[0],i[1]),a(o[0],o[1]))<r)return!0}return!1}return!!At([t,e],o.geometry.coordinates)||void 0})}getSelectMode(){if(this.checkEnabled(),!this._instanceSelectMode)throw new Error("No select mode defined in instance");return this.getMode()!==this._instanceSelectMode&&this.setMode(this._instanceSelectMode),this._modes[this._instanceSelectMode]}setModeStyles(t,e){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._modes[t].styles=e}getSnapshot(){return this._store.copyAll()}clear(){this.checkEnabled(),this._adapter.clear()}get enabled(){return this._enabled}set enabled(t){throw new Error("Enabled is read only")}getMode(){return this._mode.mode}setMode(t){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._mode.stop(),this._mode=this._modes[t],this._mode.start()}removeFeatures(t){this.checkEnabled(),this._store.delete(t)}selectFeature(t){this.getSelectMode().selectFeature(t)}deselectFeature(t){this.getSelectMode().deselectFeature(t)}getFeatureId(){return this._store.getId()}hasFeature(t){return this._store.has(t)}addFeatures(t){this.checkEnabled(),0!==t.length&&this._store.load(t,t=>{if(Boolean(t&&"object"==typeof t&&"properties"in t&&"object"==typeof t.properties&&null!==t.properties&&"mode"in t.properties)){const e=this._modes[t.properties.mode];return!!e&&e.validateFeature.bind(e)(t)}return!1})}start(){this._enabled=!0,this._adapter.register({onReady:()=>{this._eventListeners.ready.forEach(t=>{t()})},getState:()=>this._mode.state,onClick:t=>{this._mode.onClick(t)},onMouseMove:t=>{this._mode.onMouseMove(t)},onKeyDown:t=>{this._mode.onKeyDown(t)},onKeyUp:t=>{this._mode.onKeyUp(t)},onDragStart:(t,e)=>{this._mode.onDragStart(t,e)},onDrag:(t,e)=>{this._mode.onDrag(t,e)},onDragEnd:(t,e)=>{this._mode.onDragEnd(t,e)},onClear:()=>{this._mode.cleanUp(),this._store.clear()}})}getFeaturesAtLngLat(t,e){const{lng:i,lat:o}=t;return this.featuresAtLocation({lng:i,lat:o},e)}getFeaturesAtPointerEvent(t,e){const i=this._adapter.getLngLatFromEvent.bind(this._adapter)(t);return null===i?[]:this.featuresAtLocation(i,e)}stop(){this._enabled=!1,this._adapter.unregister()}on(t,e){const i=this._eventListeners[t];i.includes(e)||i.push(e)}off(t,e){const i=this._eventListeners[t];i.includes(e)&&i.splice(i.indexOf(e),1)}}export{De as TerraDraw,Ee as TerraDrawAngledRectangleMode,B as TerraDrawArcGISMapsSDKAdapter,ht as TerraDrawCircleMode,Fe as TerraDrawExtend,ct as TerraDrawFreehandMode,s as TerraDrawGoogleMapsAdapter,n as TerraDrawLeafletAdapter,bt as TerraDrawLineStringMode,l as TerraDrawMapLibreGLAdapter,a as TerraDrawMapboxGLAdapter,W as TerraDrawOpenLayersAdapter,It as TerraDrawPointMode,Dt as TerraDrawPolygonMode,kt as TerraDrawRectangleMode,Ot as TerraDrawRenderMode,we as TerraDrawSectorMode,Zt as TerraDrawSelectMode,Ie as TerraDrawSensorMode,xe as ValidateMaxAreaSquareMeters,_e as ValidateMinAreaSquareMeters,Se as ValidateNotSelfIntersecting};
2
2
  //# sourceMappingURL=terra-draw.modern.js.map