terra-draw 1.9.0 → 1.9.1
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.
- package/dist/modes/line-snapping.behavior.d.ts +2 -2
- package/dist/terra-draw.cjs +1 -1
- package/dist/terra-draw.cjs.map +1 -1
- package/dist/terra-draw.modern.js +1 -1
- package/dist/terra-draw.modern.js.map +1 -1
- package/dist/terra-draw.module.js +1 -1
- package/dist/terra-draw.module.js.map +1 -1
- package/dist/terra-draw.umd.js +1 -1
- package/dist/terra-draw.umd.js.map +1 -1
- package/dist/test/mock-behavior-config.d.ts +1 -1
- package/package.json +1 -1
|
@@ -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)({}).hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t},t.apply(null,arguments)}var e;!function(t){t.Commit="commit",t.Provisional="provisional",t.Finish="finish"}(e||(e={}));const i={SELECTED:"selected",MID_POINT:"midPoint",SELECTION_POINT_FEATURE_ID:"selectionPointFeatureId",SELECTION_POINT:"selectionPoint"},o="currentlyDrawing",s="edited",n="closingPoint",r="snappingPoint",a="coordinatePoint",d="coordinatePointFeatureId",l="coordinatePointIds",h=10;function c(t){return Boolean(t&&"object"==typeof t&&null!==t&&!Array.isArray(t))}function u(t){return Boolean(t&&"object"==typeof t&&"properties"in t&&"object"==typeof t.properties&&null!==t.properties&&"mode"in t.properties)}function p(t){return!!function(t){return"number"==typeof t&&!isNaN(new Date(t).valueOf())}(t)}const g="Feature is not a Polygon",y="Feature mode property does not match the mode being added to";var f;!function(t){t.Drawing="drawing",t.Select="select",t.Static="static",t.Render="render"}(f||(f={}));const m={rightClick:!0,contextMenu:!1,leftClick:!0,onDragStart:!0,onDrag:!0,onDragEnd:!0};class v{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&&this.onStyleChange([],"styling"),this._styles=t}registerBehaviors(t){}constructor(t,e=!1){this._state="unregistered",this._styles={},this.pointerEvents=m,this.behaviors=[],this.validate=void 0,this.pointerDistance=40,this.coordinatePrecision=void 0,this.onStyleChange=void 0,this.store=void 0,this.projection="web-mercator",this.setDoubleClickToZoom=void 0,this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this.type=f.Drawing,this.mode="base",e||this.updateOptions(t)}updateOptions(e){null!=e&&e.styles&&(this.styles=t({},this._styles,e.styles)),null!=e&&e.pointerDistance&&(this.pointerDistance=e.pointerDistance),null!=e&&e.validation&&(this.validate=e&&e.validation),null!=e&&e.projection&&(this.projection=e.projection),void 0!==(null==e?void 0:e.pointerEvents)&&(this.pointerEvents=e.pointerEvents)}allowPointerEvent(t,e){return"boolean"==typeof t?t:"function"!=typeof t||t(e)}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){return this.performFeatureValidation(t)}afterFeatureAdded(t){}performFeatureValidation(t){if("unregistered"===this._state)throw new Error("Mode must be registered");const i=function(t,e){let i;if(c(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(c(t.geometry))if(c(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)return{valid:!1,reason:"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";return i?{valid:!1,reason:i}:{valid:!0}}(t,this.store.idStrategy.isValidId);if(this.validate){const o=this.validate(t,{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Provisional});return{valid:i.valid&&o.valid,reason:o.reason}}return{valid:i.valid,reason:i.reason}}validateModeFeature(t,e){const i=this.performFeatureValidation(t);return i.valid?t.properties.mode!==this.mode?{valid:!1,reason:y}:e(t):{valid:!1,reason:i.reason}}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 C extends v{constructor(...t){super(...t),this.type=f.Select}}function P(t,e){const i=t=>t*Math.PI/180,o=i(t[1]),s=i(t[0]),n=i(e[1]),r=n-o,a=i(e[0])-s,d=Math.sin(r/2)*Math.sin(r/2)+Math.cos(o)*Math.cos(n)*Math.sin(a/2)*Math.sin(a/2);return 2*Math.atan2(Math.sqrt(d),Math.sqrt(1-d))*6371e3/1e3}const I=6371008.8;function x(t){return t%360*Math.PI/180}function M(t){return t/6371.0088}function S(t){return t%(2*Math.PI)*180/Math.PI}function E(t,e=9){const i=Math.pow(10,e);return Math.round(t*i)/i}const F=57.29577951308232,w=.017453292519943295,D=6378137,b=(t,e)=>({x:0===t?0:t*w*D,y:0===e?0:Math.log(Math.tan(Math.PI/4+e*w/2))*D}),k=(t,e)=>({lng:0===t?0:F*(t/D),lat:0===e?0:(2*Math.atan(Math.exp(e/D))-Math.PI/2)*F});function O(t,e,i){const o=x(t[0]),s=x(t[1]),n=x(i),r=M(e),a=Math.asin(Math.sin(s)*Math.cos(r)+Math.cos(s)*Math.sin(r)*Math.cos(n));return[S(o+Math.atan2(Math.sin(n)*Math.sin(r)*Math.cos(s),Math.cos(r)-Math.sin(s)*Math.sin(a))),S(a)]}function _(t){const{center:e,radiusKilometers:i,coordinatePrecision:o}=t,s=t.steps?t.steps:64,n=[];for(let t=0;t<s;t++){const r=O(e,i,-360*t/s);n.push([E(r[0],o),E(r[1],o)])}return n.push(n[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[n]},properties:{}}}function j(t){let e;if("Polygon"===t.geometry.type)e=t.geometry.coordinates;else{if("LineString"!==t.geometry.type)throw new Error("Self intersects only accepts Polygons and LineStrings");e=[t.geometry.coordinates]}const i=[];for(let t=0;t<e.length;t++)for(let i=0;i<e[t].length-1;i++)for(let o=0;o<e.length;o++)for(let n=0;n<e[o].length-1;n++)s(t,i,o,n);return i.length>0;function o(t){return t<0||t>1}function s(t,s,n,r){const a=e[t][s],d=e[t][s+1],l=e[n][r],h=e[n][r+1],c=function(t,e,i,o){if(B(t,i)||B(t,o)||B(e,i)||B(o,i))return null;const s=t[0],n=t[1],r=e[0],a=e[1],d=i[0],l=i[1],h=o[0],c=o[1],u=(s-r)*(l-c)-(n-a)*(d-h);return 0===u?null:[((s*a-n*r)*(d-h)-(s-r)*(d*c-l*h))/u,((s*a-n*r)*(l-c)-(n-a)*(d*c-l*h))/u]}(a,d,l,h);if(null===c)return;let u,p;u=d[0]!==a[0]?(c[0]-a[0])/(d[0]-a[0]):(c[1]-a[1])/(d[1]-a[1]),p=h[0]!==l[0]?(c[0]-l[0])/(h[0]-l[0]):(c[1]-l[1])/(h[1]-l[1]),o(u)||o(p)||(c.toString(),i.push(c))}}function B(t,e){return t[0]===e[0]&&t[1]===e[1]}function W(t,e){return N(t[0])<=e&&N(t[1])<=e}function G(t){return 2===t.length&&"number"==typeof t[0]&&"number"==typeof t[1]&&Infinity!==t[0]&&Infinity!==t[1]&&(i=t[0])>=-180&&i<=180&&(e=t[1])>=-90&&e<=90;var e,i}function N(t){let e=1,i=0;for(;Math.round(t*e)/e!==t;)e*=10,i++;return i}const L="Feature has holes",T="Feature has less than 4 coordinates",V="Feature has invalid coordinates",U="Feature coordinates are not closed";function A(t,e){if("Polygon"!==t.geometry.type)return{valid:!1,reason:"Feature is not a Polygon"};if(1!==t.geometry.coordinates.length)return{valid:!1,reason:L};if(t.geometry.coordinates[0].length<4)return{valid:!1,reason:T};for(let i=0;i<t.geometry.coordinates[0].length;i++){if(!G(t.geometry.coordinates[0][i]))return{valid:!1,reason:V};if(!W(t.geometry.coordinates[0][i],e))return{valid:!1,reason:"Feature has coordinates with excessive precision"}}return(i=t.geometry.coordinates[0][0])[0]!==(o=t.geometry.coordinates[0][t.geometry.coordinates[0].length-1])[0]||i[1]!==o[1]?{valid:!1,reason:U}:{valid:!0};var i,o}function R(t,e){const i=A(t,e);return i.valid?j(t)?{valid:!1,reason:"Feature intersects itself"}:{valid:!0}:i}const K={cancel:"Escape",finish:"Enter"},z={start:"crosshair"};class X extends v{constructor(t){super(t,!0),this.mode="circle",this.center=void 0,this.clickCount=0,this.currentCircleId=void 0,this.keyEvents=K,this.cursors=z,this.startingRadiusKilometers=1e-5,this.cursorMovedAfterInitialCursorDown=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.startingRadiusKilometers&&(this.startingRadiusKilometers=e.startingRadiusKilometers)}close(){if(void 0===this.currentCircleId)return;this.store.updateProperty([{id:this.currentCircleId,property:o,value:void 0}]);const t=this.currentCircleId;if(this.validate&&t){const i=this.store.getGeometryCopy(t);if(!this.validate({type:"Feature",id:t,geometry:i,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Finish}).valid)return}this.cursorMovedAfterInitialCursorDown=!1,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("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t))if(0===this.clickCount){this.center=[t.lng,t.lat];const e=_({center:this.center,radiusKilometers:this.startingRadiusKilometers,coordinatePrecision:this.coordinatePrecision}),[i]=this.store.create([{geometry:e.geometry,properties:{mode:this.mode,radiusKilometers:this.startingRadiusKilometers,[o]:!0}}]);this.currentCircleId=i,this.clickCount++,this.cursorMovedAfterInitialCursorDown=!1,this.setDrawing()}else 1===this.clickCount&&this.center&&void 0!==this.currentCircleId&&this.cursorMovedAfterInitialCursorDown&&this.updateCircle(t),this.close()}onMouseMove(t){this.cursorMovedAfterInitialCursorDown=!0,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=h,i):i}validateFeature(t){return this.validateModeFeature(t,t=>R(t,this.coordinatePrecision))}updateCircle(t){if(1===this.clickCount&&this.center&&this.currentCircleId){const i=P(this.center,[t.lng,t.lat]);let o;if("web-mercator"===this.projection){const e=function(t,e){const i=1e3*P(t,e);if(0===i)return 1;const{x:o,y:s}=b(t[0],t[1]),{x:n,y:r}=b(e[0],e[1]);return Math.sqrt(Math.pow(n-o,2)+Math.pow(r-s,2))/i}(this.center,[t.lng,t.lat]);o=function(t){const{center:e,radiusKilometers:i,coordinatePrecision:o}=t,s=t.steps?t.steps:64,n=1e3*i,[r,a]=e,{x:d,y:l}=b(r,a),h=[];for(let t=0;t<s;t++){const e=360*t/s*Math.PI/180,i=n*Math.cos(e),r=n*Math.sin(e),[a,c]=[d+i,l+r],{lng:u,lat:p}=k(a,c);h.push([E(u,o),E(p,o)])}return h.push(h[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[h]},properties:{}}}({center:this.center,radiusKilometers:i*e,coordinatePrecision:this.coordinatePrecision})}else{if("globe"!==this.projection)throw new Error("Invalid projection");o=_({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:e.Provisional}).valid)return;this.store.updateGeometry([{id:this.currentCircleId,geometry:o.geometry}]),this.store.updateProperty([{id:this.currentCircleId,property:"radiusKilometers",value:i}])}}}const Y=(t,e)=>{const{x:i,y:o}=t,{x:s,y:n}=e,r=s-i,a=n-o;return Math.sqrt(a*a+r*r)};function H(t){const e=function(t){const e=t.coordinates[0];let i=0;for(let t=0;t<e.length-1;t++){const[o,s]=e[t],[n,r]=e[t+1];i+=(n-o)*(r+s)}return i<0}(t);if(!e)return{type:"Polygon",coordinates:[t.coordinates[0].reverse()]}}const q={cancel:"Escape",finish:"Enter"},$={start:"crosshair",close:"pointer"};class Z extends v{constructor(t){super(t,!0),this.mode="freehand",this.startingClick=!1,this.currentId=void 0,this.closingPointId=void 0,this.minDistance=20,this.keyEvents=q,this.cursors=$,this.preventPointsNearClose=!0,this.autoClose=!1,this.autoCloseTimeout=500,this.hasLeftStartingPoint=!1,this.preventNewFeature=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.minDistance&&(this.minDistance=e.minDistance),void 0!==(null==e?void 0:e.preventPointsNearClose)&&(this.preventPointsNearClose=e.preventPointsNearClose),void 0!==(null==e?void 0:e.autoClose)&&(this.autoClose=e.autoClose),null!=e&&e.autoCloseTimeout&&(this.autoCloseTimeout=e.autoCloseTimeout),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors))}close(){if(void 0===this.currentId)return;if(this.currentId){const t=H(this.store.getGeometryCopy(this.currentId));t&&this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}])}const t=this.currentId;if(this.validate&&t){const i=this.store.getGeometryCopy(t);if(!this.validate({type:"Feature",id:t,geometry:i,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Finish}).valid)return}this.closingPointId&&this.store.delete([this.closingPointId]),this.startingClick=!1,this.currentId=void 0,this.closingPointId=void 0,this.hasLeftStartingPoint=!1,"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 i=this.store.getGeometryCopy(this.currentId),o=i.coordinates[0].length-2,[s,n]=i.coordinates[0][o],{x:r,y:a}=this.project(s,n),d=Y({x:r,y:a},{x:t.containerX,y:t.containerY}),[l,h]=i.coordinates[0][0],{x:c,y:u}=this.project(l,h);if(Y({x:c,y:u},{x:t.containerX,y:t.containerY})<this.pointerDistance){if(this.autoClose&&this.hasLeftStartingPoint&&(this.preventNewFeature=!0,setTimeout(()=>{this.preventNewFeature=!1},this.autoCloseTimeout),this.close()),this.setCursor(this.cursors.close),this.preventPointsNearClose)return}else this.hasLeftStartingPoint=!0,this.setCursor(this.cursors.start);if(d<this.minDistance)return;i.coordinates[0].pop();const p={type:"Polygon",coordinates:[[...i.coordinates[0],[t.lng,t.lat],i.coordinates[0][0]]]};this.validate&&!this.validate({type:"Feature",id:this.currentId,geometry:p,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Provisional}).valid||this.store.updateGeometry([{id:this.currentId,geometry:p}])}onClick(t){if("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t)){if(this.preventNewFeature)return;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,[o]:!0}},{geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:{mode:this.mode,[n]:!0}}]);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&&!0===this.startingClick&&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=h,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=50,i):i}validateFeature(t){return this.validateModeFeature(t,t=>A(t,this.coordinatePrecision))}}class J{constructor({store:t,mode:e,project:i,unproject:o,pointerDistance:s,coordinatePrecision:n,projection:r}){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=s,this.coordinatePrecision=n,this.projection=r}}function Q({unproject:t,point:e,pointerDistance:i}){const o=i/2,{x:s,y:n}=e;return{type:"Feature",properties:{},geometry:{type:"Polygon",coordinates:[[t(s-o,n-o),t(s+o,n-o),t(s+o,n+o),t(s-o,n+o),t(s-o,n-o)].map(t=>[t.lng,t.lat])]}}}class tt extends J{constructor(t){super(t)}create(t){const{containerX:e,containerY:i}=t;return Q({unproject:this.unproject,point:{x:e,y:i},pointerDistance:this.pointerDistance})}}class et extends J{constructor(t){super(t)}measure(t,e){const{x:i,y:o}=this.project(e[0],e[1]);return Y({x:i,y:o},{x:t.containerX,y:t.containerY})}}class it extends J{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)).coordinate,this.getSnappableCoordinate=(t,e)=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e)).coordinate,this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),s={featureId:void 0,featureCoordinateIndex:void 0,coordinate: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((i,o)=>{const n=this.pixelDistance.measure(t,i);n<s.minDist&&n<this.pointerDistance&&(s.coordinate=i,s.minDist=n,s.featureId=e.id,s.featureCoordinateIndex=o)})}),s}}function ot(t,e,i){const o=x(t[0]),s=x(t[1]),n=x(i),r=M(e),a=Math.asin(Math.sin(s)*Math.cos(r)+Math.cos(s)*Math.sin(r)*Math.cos(n));return[S(o+Math.atan2(Math.sin(n)*Math.sin(r)*Math.cos(s),Math.cos(r)-Math.sin(s)*Math.sin(a))),S(a)]}function st({x:t,y:e},i,o){const s=x(o);return{x:t+i*Math.cos(s),y:e+i*Math.sin(s)}}function nt(t,e){const i=x(t[0]),o=x(e[0]),s=x(t[1]),n=x(e[1]),r=Math.sin(o-i)*Math.cos(n),a=Math.cos(s)*Math.sin(n)-Math.sin(s)*Math.cos(n)*Math.cos(o-i);return S(Math.atan2(r,a))}function rt({x:t,y:e},{x:i,y:o}){const s=i-t,n=o-e;if(0===s&&0===n)return 0;let r=Math.atan2(n,s);return r*=180/Math.PI,r>180?r-=360:r<-180&&(r+=360),r}function at(t){return(t+360)%360}function dt(t,e,i){const o=[],s=t.length;let n,r,a,d=0;for(let s=0;s<t.length&&!(e>=d&&s===t.length-1);s++){if(d>e&&0===o.length){if(n=e-d,!n)return o.push(t[s]),o;r=nt(t[s],t[s-1])-180,a=ot(t[s],n,r),o.push(a)}if(d>=i)return n=i-d,n?(r=nt(t[s],t[s-1])-180,a=ot(t[s],n,r),o.push(a),o):(o.push(t[s]),o);if(d>=e&&o.push(t[s]),s===t.length-1)return o;d+=P(t[s],t[s+1])}if(d<e&&t.length===s)throw new Error("Start position is beyond line");const l=t[t.length-1];return[l,l]}function lt(t){return t*(Math.PI/180)}function ht(t){return t*(180/Math.PI)}class ct extends J{constructor(t){super(t),this.config=void 0,this.config=t}generateInsertionCoordinates(t,e,i){const o=[t,e];let s=0;for(let t=0;t<o.length-1;t++)s+=P(o[0],o[1]);if(s<=i)return o;let n=s/i-1;Number.isInteger(n)||(n=Math.floor(n)+1);const r=[];for(let t=0;t<n;t++){const e=dt(o,i*t,i*(t+1));r.push(e)}const a=[];for(let t=0;t<r.length;t++)a.push(r[t][1]);return this.limitCoordinates(a)}generateInsertionGeodesicCoordinates(t,e,i){const o=P(t,e),s=function(t,e,i){const o=[],s=lt(t[1]),n=lt(t[0]),r=lt(e[1]),a=lt(e[0]);i+=1;const d=2*Math.asin(Math.sqrt(Math.sin((r-s)/2)**2+Math.cos(s)*Math.cos(r)*Math.sin((a-n)/2)**2));if(0===d||isNaN(d))return o;for(let t=0;t<=i;t++){const e=t/i,l=Math.sin((1-e)*d)/Math.sin(d),h=Math.sin(e*d)/Math.sin(d),c=l*Math.cos(s)*Math.cos(n)+h*Math.cos(r)*Math.cos(a),u=l*Math.cos(s)*Math.sin(n)+h*Math.cos(r)*Math.sin(a),p=l*Math.sin(s)+h*Math.sin(r);if(isNaN(c)||isNaN(u)||isNaN(p))continue;const g=Math.atan2(p,Math.sqrt(c**2+u**2)),y=Math.atan2(u,c);isNaN(g)||isNaN(y)||o.push([ht(y),ht(g)])}return o.slice(1,-1)}(t,e,Math.floor(o/i));return this.limitCoordinates(s)}limitCoordinates(t){return t.map(t=>[E(t[0],this.config.coordinatePrecision),E(t[1],this.config.coordinatePrecision)])}}function ut(t,e){return t[0]===e[0]&&t[1]===e[1]}function pt(t,e){if("LineString"!==t.geometry.type)return{valid:!1,reason:"Feature is not a LineString"};if(t.geometry.coordinates.length<2)return{valid:!1,reason:"Feature has less than 2 coordinates"};for(let i=0;i<t.geometry.coordinates.length;i++){if(!G(t.geometry.coordinates[i]))return{valid:!1,reason:"Feature has invalid coordinates"};if(!W(t.geometry.coordinates[i],e))return{valid:!1,reason:"Feature has coordinates with excessive precision"}}return{valid:!0}}function gt(t){return Math.sqrt(Math.pow(t[0],2)+Math.pow(t[1],2)+Math.pow(t[2],2))}function yt(t,e){const i=function(t,e){const[i,o,s]=t,[n,r,a]=e;return i*n+o*r+s*a}(t,e)/(gt(t)*gt(e));return Math.acos(Math.min(Math.max(i,-1),1))}function ft(t){const e=x(t[1]),i=x(t[0]);return[Math.cos(e)*Math.cos(i),Math.cos(e)*Math.sin(i),Math.sin(e)]}function mt(t){const[e,i,o]=t,s=S(Math.asin(o));return[S(Math.atan2(i,e)),s]}function vt(t,e,i){const o=ft(t),s=ft(e),n=ft(i),[r,a,d]=n,[l,h,c]=function(t,e){const[i,o,s]=t,[n,r,a]=e;return[o*a-s*r,s*n-i*a,i*r-o*n]}(o,s),u=h*d-c*a,p=c*r-l*d,g=l*a-h*r,y=g*h-p*c,f=u*c-g*l,m=p*l-u*h,v=1/Math.sqrt(Math.pow(y,2)+Math.pow(f,2)+Math.pow(m,2)),C=[y*v,f*v,m*v],I=[-1*y*v,-1*f*v,-1*m*v],x=yt(o,s),M=yt(o,C),S=yt(s,C),E=yt(o,I),F=yt(s,I);let w;return w=M<E&&M<F||S<E&&S<F?C:I,yt(o,w)>x||yt(s,w)>x?P(mt(w),mt(o))<=P(mt(w),mt(s))?[mt(o),!0,!1]:[mt(s),!1,!0]:[mt(w),!1,!1]}function Ct(t,e,i){const o=e.x-t.x,s=e.y-t.y,n=Math.max(0,Math.min(1,((i.x-t.x)*o+(i.y-t.y)*s)/(o*o+s*s)));return{x:t.x+n*o,y:t.y+n*s}}class Pt extends J{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)).coordinate,this.getSnappableCoordinate=(t,e)=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e)).coordinate,this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),s={featureId:void 0,featureCoordinateIndex:void 0,coordinate:void 0,minDistance: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}const o=[];for(let t=0;t<i.length-1;t++)o.push([i[t],i[t+1]]);let n;const r=[t.lng,t.lat];if("web-mercator"===this.config.projection?n=function(t,e){let i=[Infinity,Infinity],o=Infinity,s=0;for(let n of e){const r=n[0],a=n[1];let d,l=Infinity;const h=b(r[0],r[1]),c=b(a[0],a[1]),u=b(t[0],t[1]);if(r[0]===t[0]&&r[1]===t[1])d=r;else if(a[0]===t[0]&&a[1]===t[1])d=a;else{const{x:t,y:e}=Ct(h,c,u),{lng:i,lat:o}=k(t,e);d=[i,o]}d&&(l=Y(u,b(d[0],d[1])),l<o&&(i=d,o=l,s=e.indexOf(n)))}return Infinity===o?void 0:{coordinate:i,lineIndex:s,distance:o}}(r,o):"globe"===this.config.projection&&(n=function(t,e){let i=[Infinity,Infinity],o=Infinity,s=0;for(let n of e){const r=n[0],a=n[1];let d,l=Infinity;r[0]===t[0]&&r[1]===t[1]?d=r:a[0]===t[0]&&a[1]===t[1]?d=a:[d]=vt(r,a,t),d&&(l=P(t,d),l<o&&(i=d,o=l,s=e.indexOf(n)))}return Infinity===o?void 0:{coordinate:i,distance:o,lineIndex:s}}(r,o)),!n)return;const a=this.pixelDistance.measure(t,n.coordinate);a<s.minDistance&&a<this.pointerDistance&&(s.featureId=e.id,s.coordinate=n.coordinate,s.featureCoordinateIndex=n.lineIndex,s.minDistance=a)}),s}}const It={cancel:"Escape",finish:"Enter"},xt={start:"crosshair",close:"pointer",dragStart:"grabbing",dragEnd:"crosshair"};class Mt extends v{constructor(t){super(t,!0),this.mode="linestring",this.currentCoordinate=0,this.currentId=void 0,this.closingPointId=void 0,this.keyEvents=It,this.snapping=void 0,this.cursors=xt,this.mouseMove=!1,this.insertCoordinates=void 0,this.lastCommitedCoordinates=void 0,this.snappedPointId=void 0,this.editable=!1,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedSnapType=void 0,this.editedInsertIndex=void 0,this.editedPointId=void 0,this.coordinateSnapping=void 0,this.insertPoint=void 0,this.lineSnapping=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null!=e&&e.snapping&&(this.snapping=e.snapping),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.insertCoordinates&&(this.insertCoordinates=e.insertCoordinates),e&&e.editable&&(this.editable=e.editable)}close(){if(void 0===this.currentId)return;const t=this.store.getGeometryCopy(this.currentId);t.coordinates.pop(),this.updateGeometries([...t.coordinates],void 0,e.Commit),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}]);const i=this.currentId;this.closingPointId&&this.store.delete([this.closingPointId]),this.snappedPointId&&this.store.delete([this.snappedPointId]),this.currentCoordinate=0,this.currentId=void 0,this.closingPointId=void 0,this.snappedPointId=void 0,this.lastCommitedCoordinates=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(i,{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}).valid)return;const s=[{id:this.currentId,geometry:o}];this.closingPointId&&e&&s.push({id:this.closingPointId,geometry:{type:"Point",coordinates:e}}),"commit"===i&&(this.lastCommitedCoordinates=o.coordinates),this.store.updateGeometry(s)}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=P(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,[o]:!0}}]);this.lastCommitedCoordinates=[t,t],this.currentId=e,this.currentCoordinate++,this.setDrawing()}firstUpdateToLine(t){if(!this.currentId)return;const i=this.store.getGeometryCopy(this.currentId).coordinates,[o]=this.store.create([{geometry:{type:"Point",coordinates:[...t]},properties:{mode:this.mode}}]);this.closingPointId=o,this.setCursor(this.cursors.close);const s=[...i,t];this.updateGeometries(s,void 0,e.Commit),this.currentCoordinate++}updateToLine(t,i){if(!this.currentId)return;const o=this.store.getGeometryCopy(this.currentId).coordinates,[s,n]=this.lastCommitedCoordinates?this.lastCommitedCoordinates[this.lastCommitedCoordinates.length-1]:o[o.length-2],{x:r,y:a}=this.project(s,n);if(Y({x:r,y:a},{x:i.x,y:i.y})<this.pointerDistance)return void this.close();this.setCursor(this.cursors.close);const d=[...o,t];this.updateGeometries(d,o[o.length-1],e.Commit),this.currentCoordinate++}registerBehaviors(t){this.coordinateSnapping=new it(t,new et(t),new tt(t)),this.insertPoint=new ct(t),this.clickBoundingBox=new tt(t),this.pixelDistance=new et(t),this.lineSnapping=new Pt(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new it(t,this.pixelDistance,this.clickBoundingBox)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){this.mouseMove=!0,this.setCursor(this.cursors.start);const i=this.snapCoordinate(t);if(i){if(this.snappedPointId)this.store.updateGeometry([{id:this.snappedPointId,geometry:{type:"Point",coordinates:i}}]);else{const[t]=this.store.create([{geometry:{type:"Point",coordinates:i},properties:{mode:this.mode,[r]:!0}}]);this.snappedPointId=t}t.lng=i[0],t.lat=i[1]}else this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0);const o=i||[t.lng,t.lat];if(void 0===this.currentId||0===this.currentCoordinate)return;const s=this.store.getGeometryCopy(this.currentId).coordinates;if(s.pop(),this.closingPointId){const[e,i]=s[s.length-1],{x:o,y:n}=this.project(e,i);Y({x:o,y:n},{x:t.containerX,y:t.containerY})<this.pointerDistance&&this.setCursor(this.cursors.close)}let n=[...s,o];if(this.insertCoordinates&&this.currentId&&this.lastCommitedCoordinates){const t=this.lastCommitedCoordinates[this.lastCommitedCoordinates.length-1],e=o;if(!ut(t,e)){const i=this.generateInsertCoordinates(t,e);n=[...this.lastCommitedCoordinates.slice(0,-1),...i,o]}}this.updateGeometries(n,void 0,e.Provisional)}onRightClick(t){if(!this.editable||"started"!==this.state)return;const{featureId:i,featureCoordinateIndex:o}=this.coordinateSnapping.getSnappable(t,t=>this.lineStringFilter(t));if(!i||void 0===o)return;const s=this.store.getGeometryCopy(i);let n;if("LineString"===s.type&&(n=s.coordinates,!(n.length<=2))){if(n.splice(o,1),this.validate&&!this.validate({id:i,type:"Feature",geometry:s,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Commit}).valid)return;this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.store.updateGeometry([{id:i,geometry:s}]),this.onFinish(i,{mode:this.mode,action:"edit"})}}onLeftClick(t){this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0);const e=this.snapCoordinate(t)||[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})}onClick(t){("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t))&&(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,"right"===t.button?this.onRightClick(t):"left"===t.button&&this.onLeftClick(t))}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp(),t.key===this.keyEvents.finish&&this.close()}onDragStart(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDragStart,t))return;if(!this.editable)return;let i;if("started"===this.state){const e=this.lineSnapping.getSnappable(t,t=>this.lineStringFilter(t));e.coordinate&&(this.editedSnapType="line",this.editedFeatureCoordinateIndex=e.featureCoordinateIndex,this.editedFeatureId=e.featureId,i=e.coordinate);const o=this.coordinateSnapping.getSnappable(t,t=>this.lineStringFilter(t));o.coordinate&&(this.editedSnapType="coordinate",this.editedFeatureCoordinateIndex=o.featureCoordinateIndex,this.editedFeatureId=o.featureId,i=o.coordinate)}if(this.editedFeatureId&&i){if(!this.editedPointId){const[t]=this.store.create([{geometry:{type:"Point",coordinates:i},properties:{mode:this.mode,[s]:!0}}]);this.editedPointId=t}this.setCursor(this.cursors.dragStart),e(!1)}}onDrag(t,i){if(!this.allowPointerEvent(this.pointerEvents.onDrag,t))return;if(void 0===this.editedFeatureId||void 0===this.editedFeatureCoordinateIndex)return;const o=this.store.getGeometryCopy(this.editedFeatureId);"coordinate"===this.editedSnapType||"line"===this.editedSnapType&&void 0!==this.editedInsertIndex?o.coordinates[this.editedFeatureCoordinateIndex]=[t.lng,t.lat]:"line"===this.editedSnapType&&void 0===this.editedInsertIndex&&(this.editedInsertIndex=this.editedFeatureCoordinateIndex+1,o.coordinates.splice(this.editedInsertIndex,0,[t.lng,t.lat]),this.editedFeatureCoordinateIndex++);const n={type:"LineString",coordinates:o.coordinates};this.validate&&!this.validate({type:"Feature",geometry:n,properties:this.store.getPropertiesCopy(this.editedFeatureId)},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Provisional}).valid||(this.snapping&&this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.store.updateGeometry([{id:this.editedFeatureId,geometry:n}]),this.editedPointId&&this.store.updateGeometry([{id:this.editedPointId,geometry:{type:"Point",coordinates:[t.lng,t.lat]}}]),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!0}]))}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&void 0!==this.editedFeatureId&&(this.setCursor(this.cursors.dragEnd),this.editedPointId&&(this.store.delete([this.editedPointId]),this.editedPointId=void 0),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!1}]),this.onFinish(this.editedFeatureId,{mode:this.mode,action:"edit"}),this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0,e(!0))}cleanUp(){const t=this.currentId,e=this.closingPointId,i=this.snappedPointId;this.closingPointId=void 0,this.snappedPointId=void 0,this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t]),void 0!==i&&this.store.delete([i]),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});if("Feature"===e.type&&"LineString"===e.geometry.type&&e.properties.mode===this.mode)return i.lineStringColor=this.getHexColorStylingValue(this.styles.lineStringColor,i.lineStringColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.lineStringWidth,i.lineStringWidth,e),i.zIndex=h,i;if("Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode){const t=e.properties[n];return i.pointColor=this.getHexColorStylingValue(t?this.styles.closingPointColor:this.styles.snappingPointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(t?this.styles.closingPointWidth:this.styles.snappingPointWidth,i.pointWidth,e),i.pointOutlineColor=this.getHexColorStylingValue(t?this.styles.closingPointOutlineColor:this.styles.snappingPointOutlineColor,"#ffffff",e),i.pointOutlineWidth=this.getNumericStylingValue(t?this.styles.closingPointOutlineWidth:this.styles.snappingPointOutlineWidth,2,e),i.zIndex=50,i}return i}validateFeature(t){return this.validateModeFeature(t,t=>pt(t,this.coordinatePrecision))}lineStringFilter(t){return Boolean("LineString"===t.geometry.type&&t.properties&&t.properties.mode===this.mode)}snapCoordinate(t){var e,i,o;let s;if(null!=(e=this.snapping)&&e.toLine){let e;e=this.currentId?this.lineSnapping.getSnappableCoordinate(t,this.currentId):this.lineSnapping.getSnappableCoordinateFirstClick(t),e&&(s=e)}return null!=(i=this.snapping)&&i.toCoordinate&&(s=this.currentId?this.coordinateSnapping.getSnappableCoordinate(t,this.currentId):this.coordinateSnapping.getSnappableCoordinateFirstClick(t)),null!=(o=this.snapping)&&o.toCustom&&(s=this.snapping.toCustom(t,{currentCoordinate:this.currentCoordinate,currentId:this.currentId,getCurrentGeometrySnapshot:this.currentId?()=>this.store.getGeometryCopy(this.currentId):()=>null,project:this.project,unproject:this.unproject})),s}}const St="Feature is not a Point",Et="Feature has invalid coordinates",Ft="Feature has coordinates with excessive precision";function wt(t,e){return"Point"!==t.geometry.type?{valid:!1,reason:St}:G(t.geometry.coordinates)?W(t.geometry.coordinates,e)?{valid:!0}:{valid:!1,reason:Ft}:{valid:!1,reason:Et}}const Dt={create:"crosshair",dragStart:"grabbing",dragEnd:"crosshair"};class bt extends v{constructor(t){super(t,!0),this.mode="point",this.cursors=Dt,this.editable=!1,this.editedFeatureId=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null!=e&&e.editable&&(this.editable=e.editable)}start(){this.setStarted(),this.setCursor(this.cursors.create)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){"right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t)?this.onRightClick(t):"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)&&this.onLeftClick(t)}onMouseMove(){}onKeyDown(){}onKeyUp(){}cleanUp(){this.editedFeatureId=void 0}onDragStart(t,e){if(this.allowPointerEvent(this.pointerEvents.onDragStart,t)){if(this.editable){const e=this.getNearestPointFeature(t);this.editedFeatureId=null==e?void 0:e.id}this.editedFeatureId&&(this.setCursor(this.cursors.dragStart),e(!1))}}onDrag(t,i){this.allowPointerEvent(this.pointerEvents.onDrag,t)&&void 0!==this.editedFeatureId&&(this.validate&&!this.validate({type:"Feature",geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:this.store.getPropertiesCopy(this.editedFeatureId)},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Finish}).valid||(this.store.updateGeometry([{id:this.editedFeatureId,geometry:{type:"Point",coordinates:[t.lng,t.lat]}}]),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!0}])))}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&void 0!==this.editedFeatureId&&(this.onFinish(this.editedFeatureId,{mode:this.mode,action:"edit"}),this.setCursor(this.cursors.dragEnd),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!1}]),this.editedFeatureId=void 0,e(!0))}registerBehaviors(t){this.pixelDistance=new et(t),this.clickBoundingBox=new tt(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("Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode){const t=Boolean(e.id&&this.editedFeatureId===e.id);i.pointWidth=this.getNumericStylingValue(t?this.styles.editedPointWidth:this.styles.pointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(t?this.styles.editedPointColor:this.styles.pointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(t?this.styles.editedPointOutlineColor:this.styles.pointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(t?this.styles.editedPointOutlineWidth:this.styles.pointOutlineWidth,2,e),i.zIndex=30}return i}validateFeature(t){return this.validateModeFeature(t,t=>wt(t,this.coordinatePrecision))}onLeftClick(t){const i={type:"Point",coordinates:[t.lng,t.lat]},o={mode:this.mode};if(this.validate&&!this.validate({type:"Feature",geometry:i,properties:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Finish}).valid)return;const[s]=this.store.create([{geometry:i,properties:o}]);this.onFinish(s,{mode:this.mode,action:"draw"})}onRightClick(t){if(!this.editable)return;const e=this.getNearestPointFeature(t);e&&this.store.delete([e.id])}getNearestPointFeature(t){const e=this.clickBoundingBox.create(t),i=this.store.search(e);let o,s=Infinity;for(let e=0;e<i.length;e++){const n=i[e];if("Point"!==n.geometry.type||n.properties.mode!==this.mode)continue;const r=this.pixelDistance.measure(t,n.geometry.coordinates);r>s||r>this.pointerDistance||(s=r,o=n)}return o}}class kt extends J{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,[n]:!0}},{geometry:{type:"Point",coordinates:t[t.length-2]},properties:{mode:e,[n]:!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),s=this.pixelDistance.measure(t,i.coordinates);return{isClosing:o<this.pointerDistance,isPreviousClosing:s<this.pointerDistance}}}class Ot extends J{constructor(t){super(t)}createOrUpdate(t){const e=this.store.getGeometryCopy(t),i=this.store.getPropertiesCopy(t);let o;if("Polygon"===e.type)o=e.coordinates[0].slice(0,-1);else{if("LineString"!==e.type)return;o=e.coordinates}const s=this.store.getPropertiesCopy(t),n=s.coordinatePointIds;if(n)if(n&&n.every(t=>this.store.has(t))){const e=s.coordinatePointIds,n=e.map(t=>this.store.getGeometryCopy(t).coordinates);if(e.length!==o.length){this.deleteCoordinatePoints(e);const s=this.createPoints(o,i.mode,t);this.setFeatureCoordinatePoints(t,s)}else o.forEach((t,i)=>{t[0]===n[i][0]&&t[1]===n[i][1]||this.store.updateGeometry([{id:e[i],geometry:{type:"Point",coordinates:t}}])})}else{const e=n.filter(t=>this.store.has(t));e.length&&this.deleteCoordinatePoints(e);const s=this.createPoints(o,i.mode,t);this.setFeatureCoordinatePoints(t,s)}else{const e=this.createPoints(o,i.mode,t);this.setFeatureCoordinatePoints(t,e)}}deletePointsByFeatureIds(t){for(const e of t)this.deleteIfPresent(e)}getUpdated(e,i){const o=this.store.getPropertiesCopy(e);if(o.coordinatePointIds)return o.coordinatePointIds.map((e,o)=>({id:e,geometry:t({},this.store.getGeometryCopy(e),{coordinates:i[o]})}))}createPoints(t,e,i){return this.store.create(t.map((t,o)=>({geometry:{type:"Point",coordinates:t},properties:{mode:e,[a]:!0,[d]:i,index:o}})))}setFeatureCoordinatePoints(t,e){this.store.updateProperty([{id:t,property:l,value:e}])}deleteCoordinatePoints(t){const e=t.filter(t=>this.store.has(t));this.store.delete(e)}deleteIfPresent(t){const e=this.store.getPropertiesCopy(t).coordinatePointIds;e&&(this.deleteCoordinatePoints(e),this.setFeatureCoordinatePoints(t,null))}}const _t={cancel:"Escape",finish:"Enter"},jt={start:"crosshair",close:"pointer",dragStart:"grabbing",dragEnd:"crosshair"};class Bt extends v{constructor(t){super(t,!0),this.mode="polygon",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=_t,this.cursors=jt,this.mouseMove=!1,this.showCoordinatePoints=!1,this.snapping=void 0,this.snappedPointId=void 0,this.editable=!1,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedSnapType=void 0,this.editedInsertIndex=void 0,this.editedPointId=void 0,this.coordinatePoints=void 0,this.lineSnapping=void 0,this.coordinateSnapping=void 0,this.pixelDistance=void 0,this.closingPoints=void 0,this.clickBoundingBox=void 0,this.updateOptions(t)}updateOptions(e){if(super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.snapping&&(this.snapping=e.snapping),void 0!==(null==e?void 0:e.editable)&&(this.editable=e.editable),void 0!==(null==e?void 0:e.pointerEvents)&&(this.pointerEvents=e.pointerEvents),void 0!==(null==e?void 0:e.showCoordinatePoints))if(this.showCoordinatePoints=e.showCoordinatePoints,this.coordinatePoints&&!0===e.showCoordinatePoints)this.store.copyAllWhere(t=>t.mode===this.mode).map(t=>t.id).forEach(t=>{this.coordinatePoints.createOrUpdate(t)});else if(this.coordinatePoints&&!1===this.showCoordinatePoints){const t=this.store.copyAllWhere(t=>t.mode===this.mode&&Boolean(t[l]));this.coordinatePoints.deletePointsByFeatureIds(t.map(t=>t.id))}}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]],e.Finish))return;const i=this.currentId;if(this.currentId){const t=H(this.store.getGeometryCopy(this.currentId));t&&this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}])}this.snappedPointId&&this.store.delete([this.snappedPointId]),this.currentCoordinate=0,this.currentId=void 0,this.snappedPointId=void 0,this.closingPoints.delete(),"drawing"===this.state&&this.setStarted(),this.onFinish(i,{mode:this.mode,action:"draw"})}registerBehaviors(t){this.clickBoundingBox=new tt(t),this.pixelDistance=new et(t),this.lineSnapping=new Pt(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new it(t,this.pixelDistance,this.clickBoundingBox),this.closingPoints=new kt(t,this.pixelDistance),this.coordinatePoints=new Ot(t)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){this.mouseMove=!0,this.setCursor(this.cursors.start);const i=this.snapCoordinate(t);if(i){if(this.snappedPointId)this.store.updateGeometry([{id:this.snappedPointId,geometry:{type:"Point",coordinates:i}}]);else{const[t]=this.store.create([{geometry:{type:"Point",coordinates:i},properties:{mode:this.mode,[r]:!0}}]);this.snappedPointId=t}t.lng=i[0],t.lat=i[1]}else this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0);if(void 0===this.currentId||0===this.currentCoordinate)return;const o=this.store.getGeometryCopy(this.currentId).coordinates[0];let s;if(1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),i=Math.max(1e-6,e);s=[o[0],[t.lng,t.lat],[t.lng,t.lat-i],o[0]]}else if(2===this.currentCoordinate)s=[o[0],o[1],[t.lng,t.lat],o[0]];else{const{isClosing:e,isPreviousClosing:i}=this.closingPoints.isClosingPoint(t);i||e?(this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.setCursor(this.cursors.close),s=[...o.slice(0,-2),o[0],o[0]]):s=[...o.slice(0,-2),[t.lng,t.lat],o[0]]}this.updatePolygonGeometry(s,e.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}).valid||(this.store.updateGeometry([{id:this.currentId,geometry:i}]),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(this.currentId),0))}snapCoordinate(t){var e,i,o;let s;if(null!=(e=this.snapping)&&e.toLine){let e;e=this.currentId?this.lineSnapping.getSnappableCoordinate(t,this.currentId):this.lineSnapping.getSnappableCoordinateFirstClick(t),e&&(s=e)}if(null!=(i=this.snapping)&&i.toCoordinate){let e;e=this.currentId?this.coordinateSnapping.getSnappableCoordinate(t,this.currentId):this.coordinateSnapping.getSnappableCoordinateFirstClick(t),e&&(s=e)}return null!=(o=this.snapping)&&o.toCustom&&(s=this.snapping.toCustom(t,{currentCoordinate:this.currentCoordinate,currentId:this.currentId,getCurrentGeometrySnapshot:this.currentId?()=>this.store.getGeometryCopy(this.currentId):()=>null,project:this.project,unproject:this.unproject})),s}polygonFilter(t){return Boolean("Polygon"===t.geometry.type&&t.properties&&t.properties.mode===this.mode)}onRightClick(t){if(!this.editable)return;const{featureId:i,featureCoordinateIndex:o}=this.coordinateSnapping.getSnappable(t,t=>this.polygonFilter(t));if(!i||void 0===o)return;const s=this.store.getGeometryCopy(i);let n;"Polygon"===s.type&&(n=s.coordinates[0],n.length<=4||("Polygon"!==s.type||0!==o&&o!==n.length-1?n.splice(o,1):(n.shift(),n.pop(),n.push([n[0][0],n[0][1]])),(!this.validate||this.validate({id:i,type:"Feature",geometry:s,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Commit}).valid)&&(this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.store.updateGeometry([{id:i,geometry:s}]),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(i),this.onFinish(i,{mode:this.mode,action:"edit"}))))}onLeftClick(t){if(this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),0===this.currentCoordinate){const e=this.snapCoordinate(t);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,[o]:!0}}]);this.currentId=i,this.currentCoordinate++,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(i),this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const i=this.snapCoordinate(t);i&&(t.lng=i[0],t.lat=i[1]);const o=this.store.getGeometryCopy(this.currentId);if(ut([t.lng,t.lat],o.coordinates[0][0]))return;if(!this.updatePolygonGeometry([o.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],o.coordinates[0][0]],e.Commit))return;this.currentCoordinate++}else if(2===this.currentCoordinate&&this.currentId){const i=this.snapCoordinate(t);i&&(t.lng=i[0],t.lat=i[1]);const o=this.store.getGeometryCopy(this.currentId).coordinates[0];if(ut([t.lng,t.lat],o[1]))return;if(!this.updatePolygonGeometry([o[0],o[1],[t.lng,t.lat],[t.lng,t.lat],o[0]],e.Commit))return;2===this.currentCoordinate&&this.closingPoints.create(o,"polygon"),this.currentCoordinate++}else if(this.currentId){const i=this.store.getGeometryCopy(this.currentId).coordinates[0],{isClosing:o,isPreviousClosing:s}=this.closingPoints.isClosingPoint(t);if(s||o)this.close();else{const o=this.snapCoordinate(t);if(o&&(t.lng=o[0],t.lat=o[1]),ut([t.lng,t.lat],i[this.currentCoordinate-1]))return;const s=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(s.geometry.coordinates[0],e.Commit))return;this.currentCoordinate++,this.closingPoints.ids.length&&this.closingPoints.update(s.geometry.coordinates[0])}}}onClick(t){this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,"right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t)?this.onRightClick(t):"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)&&this.onLeftClick(t)}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDragStart,t))return;if(!this.editable)return;let i;if("started"===this.state){const e=this.lineSnapping.getSnappable(t,t=>this.polygonFilter(t));e.coordinate&&(this.editedSnapType="line",this.editedFeatureCoordinateIndex=e.featureCoordinateIndex,this.editedFeatureId=e.featureId,i=e.coordinate);const o=this.coordinateSnapping.getSnappable(t,t=>this.polygonFilter(t));o.coordinate&&(this.editedSnapType="coordinate",this.editedFeatureCoordinateIndex=o.featureCoordinateIndex,this.editedFeatureId=o.featureId,i=o.coordinate)}if(this.editedFeatureId&&i){if(!this.editedPointId){const[t]=this.store.create([{geometry:{type:"Point",coordinates:i},properties:{mode:this.mode,[s]:!0}}]);this.editedPointId=t}this.setCursor(this.cursors.dragStart),e(!1)}}onDrag(t,i){if(!this.allowPointerEvent(this.pointerEvents.onDrag,t))return;if(void 0===this.editedFeatureId||void 0===this.editedFeatureCoordinateIndex)return;const o=this.store.getGeometryCopy(this.editedFeatureId),n=o.coordinates[0];"coordinate"===this.editedSnapType||"line"===this.editedSnapType&&void 0!==this.editedInsertIndex?0===this.editedFeatureCoordinateIndex||this.editedFeatureCoordinateIndex===o.coordinates[0].length-1?(n[0]=[t.lng,t.lat],n[n.length-1]=[t.lng,t.lat]):n[this.editedFeatureCoordinateIndex]=[t.lng,t.lat]:"line"===this.editedSnapType&&void 0===this.editedInsertIndex&&(this.editedInsertIndex=this.editedFeatureCoordinateIndex+1,o.coordinates[0].splice(this.editedInsertIndex,0,[t.lng,t.lat]),this.editedFeatureCoordinateIndex++);const r={type:"Polygon",coordinates:o.coordinates};this.validate&&!this.validate({type:"Feature",geometry:r,properties:this.store.getPropertiesCopy(this.editedFeatureId)},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Provisional}).valid||(this.snapping&&this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.store.updateGeometry([{id:this.editedFeatureId,geometry:r}]),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(this.editedFeatureId),this.editedPointId&&this.store.updateGeometry([{id:this.editedPointId,geometry:{type:"Point",coordinates:[t.lng,t.lat]}}]),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!0}]))}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&void 0!==this.editedFeatureId&&(this.setCursor(this.cursors.dragEnd),this.editedPointId&&(this.store.delete([this.editedPointId]),this.editedPointId=void 0),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!1}]),this.onFinish(this.editedFeatureId,{mode:this.mode,action:"edit"}),this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0,e(!0))}cleanUp(){const t=this.currentId,e=this.snappedPointId,i=this.editedPointId;this.currentId=void 0,this.snappedPointId=void 0,this.editedPointId=void 0,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted();try{t&&this.coordinatePoints.deletePointsByFeatureIds([t]),void 0!==t&&this.store.delete([t]),void 0!==i&&this.store.delete([i]),void 0!==e&&this.store.delete([e]),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=h,i;if("Point"===e.geometry.type){const t=e.properties[s],o=e.properties[a],d=t?"editedPoint":e.properties[n]?"closingPoint":e.properties[r]?"snappingPoint":o?"coordinatePoint":void 0;if(!d)return i;const l={editedPoint:{width:this.styles.editedPointOutlineWidth,color:this.styles.editedPointColor,outlineColor:this.styles.editedPointOutlineColor,outlineWidth:this.styles.editedPointOutlineWidth},closingPoint:{width:this.styles.closingPointWidth,color:this.styles.closingPointColor,outlineColor:this.styles.closingPointOutlineColor,outlineWidth:this.styles.closingPointOutlineWidth},snappingPoint:{width:this.styles.snappingPointWidth,color:this.styles.snappingPointColor,outlineColor:this.styles.snappingPointOutlineColor,outlineWidth:this.styles.snappingPointOutlineWidth},coordinatePoint:{width:this.styles.coordinatePointWidth,color:this.styles.coordinatePointColor,outlineColor:this.styles.coordinatePointOutlineColor,outlineWidth:this.styles.coordinatePointOutlineWidth}};return i.pointWidth=this.getNumericStylingValue(l[d].width,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(l[d].color,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(l[d].outlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(l[d].outlineWidth,2,e),i.zIndex=t?40:o?20:30,i}}return i}afterFeatureAdded(t){this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(t.id)}validateFeature(t){return this.validateModeFeature(t,t=>A(t,this.coordinatePrecision))}}const Wt={cancel:"Escape",finish:"Enter"},Gt={start:"crosshair"};class Nt extends v{constructor(t){super(t,!0),this.mode="rectangle",this.center=void 0,this.clickCount=0,this.currentRectangleId=void 0,this.keyEvents=Wt,this.cursors=Gt,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents))}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}).valid)return;this.store.updateGeometry([{id:this.currentRectangleId,geometry:o}])}}close(){const t=this.currentRectangleId;if(t){const e=H(this.store.getGeometryCopy(t));e&&this.store.updateGeometry([{id:t,geometry:e}]),this.store.updateProperty([{id:t,property:o,value:void 0}])}this.center=void 0,this.currentRectangleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted(),void 0!==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("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,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,[o]:!0}}]);this.currentRectangleId=e,this.clickCount++,this.setDrawing()}else this.updateRectangle(t,e.Finish),this.close()}onMouseMove(t){this.updateRectangle(t,e.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=h,i):i}validateFeature(t){return this.validateModeFeature(t,t=>R(t,this.coordinatePrecision))}}class Lt extends v{constructor(t){if(!t.modeName)throw new Error("Mode name is required for TerraDrawRenderMode");super(t,!0),this.type=f.Render,this.mode="render",this.updateOptions(t)}updateOptions(t){super.updateOptions(t),null!=t&&t.modeName&&(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){const e=super.validateFeature(t);if(e.valid){const e=t,i=wt(e,this.coordinatePrecision).valid||A(e,this.coordinatePrecision).valid||pt(e,this.coordinatePrecision).valid;return i?{valid:!0}:{valid:i,reason:"Feature is not a valid Point, Polygon or LineString feature"}}return e}}function Tt(t,e){const i=t,o=e,s=x(i[1]),n=x(o[1]);let r=x(o[0]-i[0]);r>Math.PI&&(r-=2*Math.PI),r<-Math.PI&&(r+=2*Math.PI);const a=Math.log(Math.tan(n/2+Math.PI/4)/Math.tan(s/2+Math.PI/4)),d=(S(Math.atan2(r,a))+360)%360;return d>180?-(360-d):d}function Vt(t,e,i){let o=e;e<0&&(o=-Math.abs(o));const s=o/I,n=t[0]*Math.PI/180,r=x(t[1]),a=x(i),d=s*Math.cos(a);let l=r+d;Math.abs(l)>Math.PI/2&&(l=l>0?Math.PI-l:-Math.PI-l);const h=Math.log(Math.tan(l/2+Math.PI/4)/Math.tan(r/2+Math.PI/4)),c=Math.abs(h)>1e-11?d/h:Math.cos(r),u=[(180*(n+s*Math.sin(a)/c)/Math.PI+540)%360-180,180*l/Math.PI];return u[0]+=u[0]-t[0]>180?-360:t[0]-u[0]>180?360:0,u}function Ut(t,e,i,o,s){const n=o(t[0],t[1]),r=o(e[0],e[1]),{lng:a,lat:d}=s((n.x+r.x)/2,(n.y+r.y)/2);return[E(a,i),E(d,i)]}function At(t,e,i){const o=Vt(t,1e3*P(t,e)/2,Tt(t,e));return[E(o[0],i),E(o[1],i)]}function Rt({featureCoords:t,precision:e,unproject:i,project:o,projection:s}){const n=[];for(let r=0;r<t.length-1;r++){let a;if("web-mercator"===s)a=Ut(t[r],t[r+1],e,o,i);else{if("globe"!==s)throw new Error("Invalid projection");a=At(t[r],t[r+1],e)}n.push(a)}return n}class Kt extends J{constructor(t,e,i){super(t),this.config=void 0,this.selectionPointBehavior=void 0,this.coordinatePointBehavior=void 0,this._midPoints=[],this.config=t,this.selectionPointBehavior=e,this.coordinatePointBehavior=i}get ids(){return this._midPoints.concat()}set ids(t){}insert(t,e,i){const o=this.store.getGeometryCopy(e),{midPointFeatureId:s,midPointSegment:n}=this.store.getPropertiesCopy(e),r=this.store.getGeometryCopy(s),a="Polygon"===r.type?r.coordinates[0]:r.coordinates;a.splice(n+1,0,o.coordinates),r.coordinates="Polygon"===r.type?[a]:a,this.store.updateGeometry([{id:s,geometry:r}]),this.store.getPropertiesCopy(t)[l]&&this.coordinatePointBehavior.createOrUpdate(t),this.store.delete([...this._midPoints,...this.selectionPointBehavior.ids]),this.create(a,s,i),this.selectionPointBehavior.create(a,r.type,s)}create(t,e,o){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,s,n){return Rt({featureCoords:t,precision:i,project:o,unproject:s,projection:n}).map((t,i)=>({geometry:{type:"Point",coordinates:t},properties:e(i)}))}(t,t=>({mode:this.mode,[i.MID_POINT]:!0,midPointSegment:t,midPointFeatureId:e}),o,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 Rt({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 zt extends J{constructor(t){super(t),this._selectionPoints=[]}get ids(){return this._selectionPoints.concat()}set ids(t){}create(t,e,o){this._selectionPoints=this.store.create(function(t,e,i){const o=[],s="Polygon"===e?t.length-1:t.length;for(let e=0;e<s;e++)o.push({geometry:{type:"Point",coordinates:t[e]},properties:i(e)});return o}(t,e,t=>({mode:this.mode,index:t,[i.SELECTION_POINT]:!0,[i.SELECTION_POINT_FEATURE_ID]:o})))}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 Xt(t,e){let i=!1;for(let r=0,a=e.length;r<a;r++){const a=e[r];for(let e=0,r=a.length,d=r-1;e<r;d=e++)(s=a[e])[1]>(o=t)[1]!=(n=a[d])[1]>o[1]&&o[0]<(n[0]-s[0])*(o[1]-s[1])/(n[1]-s[1])+s[0]&&(i=!i)}var o,s,n;return i}const Yt=(t,e,i)=>{const o=t=>t*t,s=(t,e)=>o(t.x-e.x)+o(t.y-e.y);return Math.sqrt(((t,e,i)=>{const o=s(e,i);if(0===o)return s(t,e);let n=((t.x-e.x)*(i.x-e.x)+(t.y-e.y)*(i.y-e.y))/o;return n=Math.max(0,Math.min(1,n)),s(t,{x:e.x+n*(i.x-e.x),y:e.y+n*(i.y-e.y)})})(t,e,i))};class Ht extends J{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 o,s,n,r,a=Infinity,d=Infinity,l=Infinity;const h=this.createClickBoundingBox.create(t),c=this.store.search(h);for(let h=0;h<c.length;h++){const u=c[h],p=u.geometry;if("Point"===p.type){if(u.properties.selectionPoint||u.properties.coordinatePoint||!e&&u.properties[i.MID_POINT])continue;const s=this.pixelDistance.measure(t,p.coordinates);u.properties[i.MID_POINT]&&s<this.pointerDistance&&s<l?(l=s,n=u):!u.properties[i.MID_POINT]&&s<this.pointerDistance&&s<a&&(a=s,o=u)}else if("LineString"===p.type){if(o)continue;for(let e=0;e<p.coordinates.length-1;e++){const i=p.coordinates[e],o=p.coordinates[e+1],n=Yt({x:t.containerX,y:t.containerY},this.project(i[0],i[1]),this.project(o[0],o[1]));n<this.pointerDistance&&n<d&&(d=n,s=u)}}else if("Polygon"===p.type){if(o||s)continue;Xt([t.lng,t.lat],p.coordinates)&&(r=u)}}return{clickedFeature:o||s||r,clickedMidPoint:n}}}class qt extends J{constructor(t,e,i,o,s){super(t),this.config=void 0,this.featuresAtCursorEvent=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.draggedFeatureId=null,this.dragPosition=void 0,this.config=t,this.featuresAtCursorEvent=e,this.selectionPoints=i,this.midPoints=o,this.coordinatePoints=s}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.featuresAtCursorEvent.find(t,!0);return!(!i||i.id!==e)}drag(t,i){if(!this.draggedFeatureId)return;const o=this.store.getGeometryCopy(this.draggedFeatureId),s=[t.lng,t.lat];if("Polygon"===o.type||"LineString"===o.type){let n,r;if("Polygon"===o.type?(n=o.coordinates[0],r=n.length-1):(n=o.coordinates,r=n.length),!this.dragPosition)return!1;for(let t=0;t<r;t++){const e=n[t];let i,o;if("web-mercator"===this.config.projection){const t=b(this.dragPosition[0],this.dragPosition[1]),n=b(s[0],s[1]),r=b(e[0],e[1]),a={x:t.x-n.x,y:t.y-n.y},d=r.x-a.x,l=r.y-a.y,{lng:h,lat:c}=k(d,l);i=h,o=c}else{const t=[this.dragPosition[0]-s[0],this.dragPosition[1]-s[1]];i=e[0]-t[0],o=e[1]-t[1]}if(i=E(i,this.config.coordinatePrecision),o=E(o,this.config.coordinatePrecision),i>180||i<-180||o>90||o<-90)return!1;n[t]=[i,o]}"Polygon"===o.type&&(n[n.length-1]=[n[0][0],n[0][1]]);const a=this.selectionPoints.getUpdated(n)||[],d=this.midPoints.getUpdated(n)||[],l=this.coordinatePoints.getUpdated(this.draggedFeatureId,n)||[];if(i&&!i({type:"Feature",id:this.draggedFeatureId,geometry:o,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:e.Provisional}).valid)return!1;this.store.updateGeometry([{id:this.draggedFeatureId,geometry:o},...a,...d,...l]),this.dragPosition=[t.lng,t.lat]}else"Point"===o.type&&(this.store.updateGeometry([{id:this.draggedFeatureId,geometry:{type:"Point",coordinates:s}}]),this.dragPosition=[t.lng,t.lat])}}class $t extends J{constructor(t,e,i,o,s,n,r){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.coordinateSnapping=void 0,this.lineSnapping=void 0,this.draggedCoordinate={id:null,index:-1},this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o,this.coordinatePoints=s,this.coordinateSnapping=n,this.lineSnapping=r}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 s=0;s<o.length;s++){const n=this.pixelDistance.measure(t,o[s]);if(n<this.pointerDistance&&n<i.dist){const t="Polygon"===e.type&&(s===o.length-1||0===s);i.dist=n,i.index=t?0:s,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}snapCoordinate(t,e,i){let o=[t.lng,t.lat];const s=t=>Boolean(t.properties&&t.properties.mode===i.properties.mode&&t.id!==this.draggedCoordinate.id);if(null!=e&&e.toLine){let e;e=this.lineSnapping.getSnappable(t,s).coordinate,e&&(o=e)}if(e.toCoordinate){let e;e=this.coordinateSnapping.getSnappable(t,s).coordinate,e&&(o=e)}if(null!=e&&e.toCustom){let s;s=e.toCustom(t,{currentCoordinate:this.draggedCoordinate.index,currentId:i.id,getCurrentGeometrySnapshot:i.id?()=>this.store.getGeometryCopy(i.id):()=>null,project:this.project,unproject:this.unproject}),s&&(o=s)}return o}drag(t,i,o,s){const n=this.draggedCoordinate.id;if(null===n)return!1;const r=this.draggedCoordinate.index,a=this.store.getGeometryCopy(n),d=this.store.getPropertiesCopy(n),l="LineString"===a.type?a.coordinates:a.coordinates[0],h="Polygon"===a.type&&(r===l.length-1||0===r),c={type:"Feature",id:n,geometry:a,properties:d},u=this.snapCoordinate(t,s,c);if(t.lng>180||t.lng<-180||t.lat>90||t.lat<-90)return!1;if(h){const t=l.length-1;l[0]=u,l[t]=u}else l[r]=u;const p=this.selectionPoints.getOneUpdated(r,u),g=p?[p]:[],y=this.midPoints.getUpdated(l)||[],f=this.coordinatePoints.getUpdated(n,l)||[];return!("Point"!==a.type&&!i&&j({type:"Feature",geometry:a,properties:{}})||o&&!o(c,{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:e.Provisional}).valid||(this.store.updateGeometry([{id:n,geometry:a},...g,...y,...f]),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 Jt(t){const e=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(t=>{const{x:e,y:i}=b(t[0],t[1]);return[e,i]});return"Polygon"===t.geometry.type?function(t){let e=0,i=0,o=0;const s=t.length;for(let n=0;n<s-1;n++){const[s,r]=t[n],[a,d]=t[n+1],l=s*d-a*r;e+=l,i+=(s+a)*l,o+=(r+d)*l}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 s=0;s<e;s++){const[e,n]=t[s];i+=e,o+=n}return{x:i/e,y:o/e}}(e)}class Qt extends J{constructor(t,e,i,o){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.lastBearing=void 0,this.selectedGeometry=void 0,this.selectedGeometryCentroid=void 0,this.selectedGeometryWebMercatorCentroid=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i,this.coordinatePoints=o}reset(){this.lastBearing=void 0,this.selectedGeometry=void 0,this.selectedGeometryWebMercatorCentroid=void 0,this.selectedGeometryCentroid=void 0}rotate(t,i,o){this.selectedGeometry||(this.selectedGeometry=this.store.getGeometryCopy(i));const s=this.selectedGeometry;if("Polygon"!==s.type&&"LineString"!==s.type)return;const n=[t.lng,t.lat];let r;const a={type:"Feature",geometry:s,properties:{}};if("web-mercator"===this.config.projection){this.selectedGeometryWebMercatorCentroid||(this.selectedGeometryWebMercatorCentroid=Jt(a));const e=b(t.lng,t.lat);if(r=rt(this.selectedGeometryWebMercatorCentroid,e),0===r)return;if(!this.lastBearing)return void(this.lastBearing=r);((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])=>b(t,e)),s=o.reduce((t,e)=>({x:t.x+e.x,y:t.y+e.y}),{x:0,y:0});s.x/=o.length,s.y/=o.length;const n=o.map(t=>({x:s.x+(t.x-s.x)*Math.cos(i)-(t.y-s.y)*Math.sin(i),y:s.y+(t.x-s.x)*Math.sin(i)+(t.y-s.y)*Math.cos(i)})).map(({x:t,y:e})=>[k(t,e).lng,k(t,e).lat]);"Polygon"===t.geometry.type?t.geometry.coordinates[0]=n:t.geometry.coordinates=n})(a,-(this.lastBearing-r))}else{if("globe"!==this.config.projection)throw new Error("Unsupported projection");if(this.selectedGeometryCentroid||(this.selectedGeometryCentroid=Zt({type:"Feature",geometry:s,properties:{}})),r=Tt(this.selectedGeometryCentroid,n),!this.lastBearing)return void(this.lastBearing=r+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=Tt(i,t)+e,s=function(t,e){t[0]+=t[0]-e[0]>180?-360:e[0]-t[0]>180?360:0;const i=I,o=e[1]*Math.PI/180,s=t[1]*Math.PI/180,n=s-o;let r=Math.abs(t[0]-e[0])*Math.PI/180;r>Math.PI&&(r-=2*Math.PI);const a=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(o/2+Math.PI/4)),d=Math.abs(a)>1e-11?n/a:Math.cos(o);return Math.sqrt(n*n+d*d*r*r)*i}(i,t),n=Vt(i,s,o);t[0]=n[0],t[1]=n[1]})}(a,-(this.lastBearing-(r+180)))}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 l=this.midPoints.getUpdated(d)||[],h=this.selectionPoints.getUpdated(d)||[],c=this.coordinatePoints.getUpdated(i,d)||[];if(o&&!o({id:i,type:"Feature",geometry:s,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:e.Provisional}))return!1;this.store.updateGeometry([{id:i,geometry:s},...h,...l,...c]),"web-mercator"===this.projection?this.lastBearing=r:"globe"===this.projection&&(this.lastBearing=r+180)}}class te extends J{constructor(t,e){super(t),this.config=void 0,this.dragCoordinateResizeBehavior=void 0,this.config=t,this.dragCoordinateResizeBehavior=e}scale(t,e,i){if(!this.dragCoordinateResizeBehavior.isDragging()){const i=this.dragCoordinateResizeBehavior.getDraggableIndex(t,e);this.dragCoordinateResizeBehavior.startDragging(e,i)}this.dragCoordinateResizeBehavior.drag(t,"center-fixed",i)}reset(){this.dragCoordinateResizeBehavior.stopDragging()}}class ee extends J{constructor(t,e,i,o,s){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=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,this.coordinatePoints=s}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 s=0;s<o.length;s++){const n=this.pixelDistance.measure(t,o[s]);if(n<this.pointerDistance&&n<i.dist){const t="Polygon"===e.type&&(s===o.length-1||0===s);i.dist=n,i.index=t?0:s,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:s,selectedCoordinate:n}=e,r=Jt(i);if(!r)return null;const a=b(n[0],n[1]),{closestBBoxIndex:d}=this.getIndexesWebMercator(o,a),l=b(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:d,updatedCoords:s,webMercatorCursor:l,webMercatorSelected:a,webMercatorOrigin:r}),s}centerFixedWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{feature:i,boundingBox:o,updatedCoords:s,selectedCoordinate:n}=e,r=Jt(i);if(!r)return null;const a=b(n[0],n[1]),{closestBBoxIndex:d}=this.getIndexesWebMercator(o,a),l=b(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:d,updatedCoords:s,webMercatorCursor:l,webMercatorSelected:a,webMercatorOrigin:r}),s}scaleFixedWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:i,webMercatorCursor:o,updatedCoords:s}){if(!this.isValidDragWebMercator(t,e.x-o.x,e.y-o.y))return null;let n=Y(e,o)/Y(e,i);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:s}=e,n=b(s[0],s[1]),{oppositeBboxIndex:r,closestBBoxIndex:a}=this.getIndexesWebMercator(i,n),d={x:i[r][0],y:i[r][1]},l=b(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:a,updatedCoords:o,webMercatorCursor:l,webMercatorSelected:n,webMercatorOrigin:d}),o}oppositeWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{boundingBox:i,updatedCoords:o,selectedCoordinate:s}=e,n=b(s[0],s[1]),{oppositeBboxIndex:r,closestBBoxIndex:a}=this.getIndexesWebMercator(i,n),d={x:i[r][0],y:i[r][1]},l=b(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:a,updatedCoords:o,webMercatorCursor:l,webMercatorSelected:n,webMercatorOrigin:d}),o}scaleWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:i,webMercatorCursor:o,updatedCoords:s}){const n=e.x-o.x,r=e.y-o.y;if(!this.isValidDragWebMercator(t,n,r))return null;let a=1;0!==n&&1!==t&&5!==t&&(a=1-(e.x-i.x-n)/n);let d=1;return 0!==r&&3!==t&&7!==t&&(d=1-(e.y-i.y-r)/r),this.validateScale(a,d)?(a<0&&(a=this.minimumScale),d<0&&(d=this.minimumScale),this.performWebMercatorScale(s,e.x,e.y,a,d),s):null}getFeature(t){if(null===this.draggedCoordinate.id)return null;const e=this.store.getGeometryCopy(t);return"Polygon"!==e.type&&"LineString"!==e.type?null:{id:t,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,s){t.forEach(t=>{const{x:n,y:r}=b(t[0],t[1]),a=e+(n-e)*o,d=i+(r-i)*s,{lng:l,lat:h}=k(a,d);t[0]=l,t[1]=h})}getBBoxWebMercator(t){const e=[Infinity,Infinity,-Infinity,-Infinity];(t=t.map(t=>{const{x:e,y:i}=b(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,s,n]=e;return[[i,n],[(i+s)/2,n],[s,n],[s,n+(o-n)/2],[s,o],[(i+s)/2,o],[i,o],[i,n+(o-n)/2]]}getIndexesWebMercator(t,e){let i,o=Infinity;for(let s=0;s<t.length;s++){const n=Y({x:e.x,y:e.y},{x:t[s][0],y:t[s][1]});n<o&&(i=s,o=n)}if(void 0===i)throw new Error("No closest coordinate found");return{oppositeBboxIndex:this.boundingBoxMaps.opposite[i],closestBBoxIndex:i}}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 s=this.getFeature(this.draggedCoordinate.id);if(!s)return!1;let n=null;if("center"===i?n=this.centerWebMercatorDrag(t):"opposite"===i?n=this.oppositeWebMercatorDrag(t):"center-fixed"===i?n=this.centerFixedWebMercatorDrag(t):"opposite-fixed"===i&&(n=this.oppositeFixedWebMercatorDrag(t)),!n)return!1;for(let t=0;t<n.length;t++){const e=n[t];if(e[0]=E(e[0],this.coordinatePrecision),e[1]=E(e[1],this.coordinatePrecision),!W(e,this.coordinatePrecision))return!1}const r=this.midPoints.getUpdated(n)||[],a=this.selectionPoints.getUpdated(n)||[],d=this.coordinatePoints.getUpdated(s.id,n)||[],l={type:s.geometry.type,coordinates:"Polygon"===s.geometry.type?[n]:n};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:e.Provisional}).valid||(this.store.updateGeometry([{id:this.draggedCoordinate.id,geometry:l},...a,...r,...d]),0))}}const ie={deselect:"Escape",delete:"Delete",rotate:["Control","r"],scale:["Control","s"]},oe={pointerOver:"move",dragStart:"move",dragEnd:"move",insertMidpoint:"crosshair"};class se extends C{constructor(t){super(t,!0),this.mode="select",this.allowManualDeselection=!0,this.dragEventThrottle=5,this.dragEventCount=0,this.selected=[],this.flags={},this.keyEvents=ie,this.cursors=oe,this.validations={},this.selectionPoints=void 0,this.midPoints=void 0,this.coordinateSnap=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.coordinatePoints=void 0,this.lineSnap=void 0,this.updateOptions(t)}updateOptions(e){if(super.updateOptions(e),this.cursors=e&&e.cursors?t({},this.cursors,e.cursors):oe,null===(null==e?void 0:e.keyEvents)?this.keyEvents={deselect:null,delete:null,rotate:null,scale:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),void 0!==(null==e?void 0:e.dragEventThrottle)&&(this.dragEventThrottle=e.dragEventThrottle),void 0!==(null==e?void 0:e.allowManualDeselection)&&(this.allowManualDeselection=e.allowManualDeselection),null!=e&&e.flags){this.flags=t({},this.flags,e.flags),this.validations={};for(const t in this.flags){const e=this.flags[t].feature;e&&e.validation&&(this.validations[t]=e.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 et(t),this.clickBoundingBox=new tt(t),this.featuresAtMouseEvent=new Ht(t,this.clickBoundingBox,this.pixelDistance),this.selectionPoints=new zt(t),this.coordinatePoints=new Ot(t),this.midPoints=new Kt(t,this.selectionPoints,this.coordinatePoints),this.coordinateSnap=new it(t,this.pixelDistance,this.clickBoundingBox),this.lineSnap=new Pt(t,this.pixelDistance,this.clickBoundingBox),this.rotateFeature=new Qt(t,this.selectionPoints,this.midPoints,this.coordinatePoints),this.dragFeature=new qt(t,this.featuresAtMouseEvent,this.selectionPoints,this.midPoints,this.coordinatePoints),this.dragCoordinate=new $t(t,this.pixelDistance,this.selectionPoints,this.midPoints,this.coordinatePoints,this.coordinateSnap,this.lineSnap),this.dragCoordinateResizeFeature=new ee(t,this.pixelDistance,this.selectionPoints,this.midPoints,this.coordinatePoints),this.scaleFeature=new te(t,this.dragCoordinateResizeFeature)}deselectFeature(){this.deselect()}deselect(){const t=this.selected.filter(t=>this.store.has(t)).map(t=>({id:t,property:i.SELECTED,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 i,o=Infinity;if(this.selectionPoints.ids.forEach(e=>{const s=this.store.getGeometryCopy(e),n=this.pixelDistance.measure(t,s.coordinates);n<this.pointerDistance&&n<o&&(o=n,i=this.store.getPropertiesCopy(e))}),!i)return;const s=i.selectionPointFeatureId,n=i.index,r=this.store.getPropertiesCopy(s),a=this.flags[r.mode],d=this.validations[r.mode];if(!(a&&a.feature&&a.feature.coordinates&&a.feature.coordinates.deletable))return;const l=this.store.getGeometryCopy(s);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<=2))return;if(!h)return;if("Polygon"!==l.type||0!==n&&n!==h.length-1?h.splice(n,1):(h.shift(),h.pop(),h.push([h[0][0],h[0][1]])),d&&!d({id:s,type:"Feature",geometry:l,properties:r},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Commit}).valid)return;const c=[...this.midPoints.ids,...this.selectionPoints.ids];this.store.delete(c),this.store.updateGeometry([{id:s,geometry:l}]),r.coordinatePointIds&&this.coordinatePoints.createOrUpdate(s),this.selectionPoints.create(h,l.type,s),a&&a.feature&&a.feature.coordinates&&a.feature.coordinates.midpoints&&this.midPoints.create(h,s,this.coordinatePrecision)}select(t,e=!0){if(this.selected[0]===t)return;const{mode:o}=this.store.getPropertiesCopy(t),s=this.flags[o];if(!s||!s.feature)return;const n=this.selected[0];if(n){if(n===t)return;this.deselect()}e&&this.setCursor(this.cursors.pointerOver),this.selected=[t],this.store.updateProperty([{id:t,property:i.SELECTED,value:!0}]),this.onSelect(t);const{type:r,coordinates:a}=this.store.getGeometryCopy(t);if("LineString"!==r&&"Polygon"!==r)return;const d="LineString"===r?a:a[0];d&&s&&s.feature.coordinates&&(this.selectionPoints.create(d,r,t),s.feature.coordinates.midpoints&&this.midPoints.create(d,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(this.selected[0],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&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t)?this.onRightClick(t):"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)&&this.onLeftClick(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;const t=this.selected[0];this.onDeselect(this.selected[0]),this.coordinatePoints.deletePointsByFeatureIds([t]),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.allowPointerEvent(this.pointerEvents.onDragStart,t))return;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||o.feature.coordinates&&"object"==typeof o.feature.coordinates.midpoints&&o.feature.coordinates.midpoints.draggable)))return;this.dragEventCount=0;const s=this.selected[0],n=this.dragCoordinate.getDraggableIndex(t,s);if(o&&o.feature&&o.feature.coordinates&&(o.feature.coordinates.draggable||o.feature.coordinates.resizable)&&-1!==n)return this.setCursor(this.cursors.dragStart),o.feature.coordinates.resizable?this.dragCoordinateResizeFeature.startDragging(s,n):this.dragCoordinate.startDragging(s,n),void e(!1);if(o&&o.feature&&o.feature.coordinates&&"object"==typeof o.feature.coordinates.midpoints&&o.feature.coordinates.midpoints.draggable){const{clickedMidPoint:i}=this.featuresAtMouseEvent.find(t,this.selected.length>0);if(this.selected.length&&i){this.midPoints.insert(s,i.id,this.coordinatePrecision);const o=this.dragCoordinate.getDraggableIndex(t,s);return this.dragCoordinate.startDragging(s,o),void e(!1)}}return o&&o.feature&&o.feature.draggable&&this.dragFeature.canDrag(t,s)?(this.setCursor(this.cursors.dragStart),this.dragFeature.startDragging(t,s),void e(!1)):void 0}onDrag(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDrag,t))return;const i=this.selected[0];if(!i)return;const o=this.store.getPropertiesCopy(i),s=this.flags[o.mode],n=!0===(s&&s.feature&&s.feature.selfIntersectable);if(this.dragEventCount++,this.dragEventCount%this.dragEventThrottle==0)return;const r=this.validations[o.mode];if(s&&s.feature&&s.feature.rotateable&&this.canRotate(t))return e(!1),void this.rotateFeature.rotate(t,i,r);if(s&&s.feature&&s.feature.scaleable&&this.canScale(t))return e(!1),void this.scaleFeature.scale(t,i,r);if(this.dragCoordinateResizeFeature.isDragging()&&s.feature&&s.feature.coordinates&&s.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,s.feature.coordinates.resizable,r)}if(this.dragCoordinate.isDragging()){var a;const e=null==(a=s.feature)||null==(a=a.coordinates)?void 0:a.snappable;let i={toCoordinate:!1};return!0===e?i={toCoordinate:!0}:"object"==typeof e&&(i=e),void this.dragCoordinate.drag(t,n,r,i)}this.dragFeature.isDragging()?this.dragFeature.drag(t,r):e(!0)}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&(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 s=this.store.getGeometryCopy(o);this.pixelDistance.measure(t,s.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 o=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 o.pointColor=this.getHexColorStylingValue(this.styles.selectionPointColor,o.pointColor,e),o.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectionPointOutlineColor,o.pointOutlineColor,e),o.pointWidth=this.getNumericStylingValue(this.styles.selectionPointWidth,o.pointWidth,e),o.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectionPointOutlineWidth,2,e),o.zIndex=30,o;if(e.properties.midPoint)return o.pointColor=this.getHexColorStylingValue(this.styles.midPointColor,o.pointColor,e),o.pointOutlineColor=this.getHexColorStylingValue(this.styles.midPointOutlineColor,o.pointOutlineColor,e),o.pointWidth=this.getNumericStylingValue(this.styles.midPointWidth,4,e),o.pointOutlineWidth=this.getNumericStylingValue(this.styles.midPointOutlineWidth,2,e),o.zIndex=50,o}else if(e.properties[i.SELECTED]){if("Polygon"===e.geometry.type)return o.polygonFillColor=this.getHexColorStylingValue(this.styles.selectedPolygonColor,o.polygonFillColor,e),o.polygonOutlineWidth=this.getNumericStylingValue(this.styles.selectedPolygonOutlineWidth,o.polygonOutlineWidth,e),o.polygonOutlineColor=this.getHexColorStylingValue(this.styles.selectedPolygonOutlineColor,o.polygonOutlineColor,e),o.polygonFillOpacity=this.getNumericStylingValue(this.styles.selectedPolygonFillOpacity,o.polygonFillOpacity,e),o.zIndex=h,o;if("LineString"===e.geometry.type)return o.lineStringColor=this.getHexColorStylingValue(this.styles.selectedLineStringColor,o.lineStringColor,e),o.lineStringWidth=this.getNumericStylingValue(this.styles.selectedLineStringWidth,o.lineStringWidth,e),o.zIndex=h,o;if("Point"===e.geometry.type)return o.pointWidth=this.getNumericStylingValue(this.styles.selectedPointWidth,o.pointWidth,e),o.pointColor=this.getHexColorStylingValue(this.styles.selectedPointColor,o.pointColor,e),o.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectedPointOutlineColor,o.pointOutlineColor,e),o.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectedPointOutlineWidth,o.pointOutlineWidth,e),o.zIndex=h,o}return o}}class ne extends v{constructor(...t){super(...t),this.type=f.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 re(t,e,i,o,s){for(;o>i;){if(o-i>600){const n=o-i+1,r=e-i+1,a=Math.log(n),d=.5*Math.exp(2*a/3),l=.5*Math.sqrt(a*d*(n-d)/n)*(r-n/2<0?-1:1);re(t,e,Math.max(i,Math.floor(e-r*d/n+l)),Math.min(o,Math.floor(e+(n-r)*d/n+l)),s)}const n=t[e];let r=i,a=o;for(ae(t,i,e),s(t[o],n)>0&&ae(t,i,o);r<a;){for(ae(t,r,a),r++,a--;s(t[r],n)<0;)r++;for(;s(t[a],n)>0;)a--}0===s(t[i],n)?ae(t,i,a):(a++,ae(t,a,o)),a<=e&&(i=a+1),e<=a&&(o=a-1)}}function ae(t,e,i){const o=t[e];t[e]=t[i],t[i]=o}function de(t,e){le(t,0,t.children.length,e,t)}function le(t,e,i,o,s){s||(s=ve([])),s.minX=Infinity,s.minY=Infinity,s.maxX=-Infinity,s.maxY=-Infinity;for(let n=e;n<i;n++){const e=t.children[n];he(s,t.leaf?o(e):e)}return s}function he(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 ce(t,e){return t.minX-e.minX}function ue(t,e){return t.minY-e.minY}function pe(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function ge(t){return t.maxX-t.minX+(t.maxY-t.minY)}function ye(t,e){const i=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),s=Math.min(t.maxX,e.maxX),n=Math.min(t.maxY,e.maxY);return Math.max(0,s-i)*Math.max(0,n-o)}function fe(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function me(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function ve(t){return{children:t,height:1,leaf:!0,minX:Infinity,minY:Infinity,maxX:-Infinity,maxY:-Infinity}}function Ce(t,e,i,o,s){const n=[e,i];for(;n.length;){if((i=n.pop())-(e=n.pop())<=o)continue;const r=e+Math.ceil((i-e)/o/2)*o;re(t,r,e,i,s),n.push(e,r,r,i)}}class Pe{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(!me(t,e))return i;const o=this.toBBox,s=[];for(;e;){for(let n=0;n<e.children.length;n++){const r=e.children[n],a=e.leaf?o(r):r;me(t,a)&&(e.leaf?i.push(r):fe(t,a)?this._all(r,i):s.push(r))}e=s.pop()}return i}collides(t){let e=this.data;if(me(t,e)){const i=[];for(;e;){for(let o=0;o<e.children.length;o++){const s=e.children[o],n=e.leaf?this.toBBox(s):s;if(me(t,n)){if(e.leaf||fe(t,n))return!0;i.push(s)}}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=ve([])}remove(t){let e=this.data;const i=this.toBBox(t),o=[],s=[];let n,r,a=!1;for(;e||o.length;){if(e||(e=o.pop(),r=o[o.length-1],n=s.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||!fe(e,i)?r?(n++,e=r.children[n],a=!1):e=null:(o.push(e),s.push(n),n=0,r=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 s=i-e+1;let n,r=this._maxEntries;if(s<=r)return n=ve(t.slice(e,i+1)),de(n,this.toBBox),n;o||(o=Math.ceil(Math.log(s)/Math.log(r)),r=Math.ceil(s/Math.pow(r,o-1))),n=ve([]),n.leaf=!1,n.height=o;const a=Math.ceil(s/r),d=a*Math.ceil(Math.sqrt(r));Ce(t,e,i,d,this.compareMinX);for(let s=e;s<=i;s+=d){const e=Math.min(s+d-1,i);Ce(t,s,e,a,this.compareMinY);for(let i=s;i<=e;i+=a){const s=Math.min(i+a-1,e);n.children.push(this._build(t,i,s,o-1))}}return de(n,this.toBBox),n}_chooseSubtree(t,e,i,o){for(;o.push(e),!e.leaf&&o.length-1!==i;){let i,o=Infinity,r=Infinity;for(let a=0;a<e.children.length;a++){const d=e.children[a],l=pe(d),h=(s=t,n=d,(Math.max(n.maxX,s.maxX)-Math.min(n.minX,s.minX))*(Math.max(n.maxY,s.maxY)-Math.min(n.minY,s.minY))-l);h<r?(r=h,o=l<o?l:o,i=d):h===r&&l<o&&(o=l,i=d)}e=i||e.children[0]}var s,n;return e}_insert(t,e,i){const o=i?t:this.toBBox(t),s=[],n=this._chooseSubtree(o,this.data,e,s);for(n.children.push(t),he(n,o);e>=0&&s[e].children.length>this._maxEntries;)this._split(s,e),e--;this._adjustParentBBoxes(o,s,e)}_split(t,e){const i=t[e],o=i.children.length,s=this._minEntries;this._chooseSplitAxis(i,s,o);const n=this._chooseSplitIndex(i,s,o),r=ve(i.children.splice(n,i.children.length-n));r.height=i.height,r.leaf=i.leaf,de(i,this.toBBox),de(r,this.toBBox),e?t[e-1].children.push(r):this._splitRoot(i,r)}_splitRoot(t,e){this.data=ve([t,e]),this.data.height=t.height+1,this.data.leaf=!1,de(this.data,this.toBBox)}_chooseSplitIndex(t,e,i){let o,s=Infinity,n=Infinity;for(let r=e;r<=i-e;r++){const e=le(t,0,r,this.toBBox),a=le(t,r,i,this.toBBox),d=ye(e,a),l=pe(e)+pe(a);d<s?(s=d,o=r,n=l<n?l:n):d===s&&l<n&&(n=l,o=r)}return o||i-e}_chooseSplitAxis(t,e,i){const o=t.leaf?this.compareMinX:ce,s=t.leaf?this.compareMinY:ue;this._allDistMargin(t,e,i,o)<this._allDistMargin(t,e,i,s)&&t.children.sort(o)}_allDistMargin(t,e,i,o){t.children.sort(o);const s=this.toBBox,n=le(t,0,e,s),r=le(t,i-e,i,s);let a=ge(n)+ge(r);for(let o=e;o<i-e;o++){const e=t.children[o];he(n,t.leaf?s(e):e),a+=ge(n)}for(let o=i-e-1;o>=e;o--){const e=t.children[o];he(r,t.leaf?s(e):e),a+=ge(r)}return a}_adjustParentBBoxes(t,e,i){for(let o=i;o>=0;o--)he(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():de(t[i],this.toBBox)}}class Ie{constructor(t){this.tree=void 0,this.idToNode=void 0,this.nodeToId=void 0,this.tree=new Pe(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 s=Math.min(...i),n=Math.max(...i);return{minX:Math.min(...e),minY:s,maxX:Math.max(...e),maxY:n}}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 xe={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 Ie,this.tracked=!t||!1!==t.tracked,this.idStrategy=t&&t.idStrategy?t.idStrategy:xe}clone(t){return JSON.parse(JSON.stringify(t))}getId(){return this.idStrategy.getId()}has(t){return Boolean(this.store[t])}load(t,e,i,o){if(0===t.length)return[];let s=this.clone(t);const n=[],r=[];return s=s.filter(t=>{null==t.id&&(t.id=this.idStrategy.getId());const o=t.id;if(e){const i=e(t);if(!i.valid)return r.push({id:o,valid:!1,reason:i.reason}),!1}if(this.tracked){if(t.properties.createdAt){if(!p(t.properties.createdAt))return r.push({id:t.id,valid:!1,reason:"createdAt is not a valid numeric timestamp"}),!1}else t.properties.createdAt=+new Date;if(t.properties.updatedAt){if(!p(t.properties.updatedAt))return r.push({id:t.id,valid:!1,reason:"updatedAt is not a valid numeric timestamp"}),!1}else t.properties.updatedAt=+new Date}return this.has(o)?(r.push({id:o,valid:!1,reason:`Feature already exists with this id: ${o}`}),!1):(this.store[o]=t,n.push(o),i&&i(t),r.push({id:o,valid:!0}),!0)}),this.spatialIndex.load(s),n.length>0&&this._onChange(n,"create",o),r}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,o)=>{t(e,i,o)}}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,e){const i=[];t.forEach(({id:t,property:e,value:o})=>{const s=this.store[t];if(!s)throw new Error(`No feature with this (${t}), can not update geometry`);i.push(t),void 0===o?delete s.properties[e]:s.properties[e]=o,this.tracked&&(s.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(i,"update",e)}updateGeometry(t,e){const i=[];t.forEach(({id:t,geometry:e})=>{i.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(e),this.spatialIndex.update(o),this.tracked&&(o.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(i,"update",e)}create(e,i){const o=[];return e.forEach(({geometry:e,properties:i})=>{let s,n=t({},i);this.tracked&&(s=+new Date,i?(n.createdAt="number"==typeof i.createdAt?i.createdAt:s,n.updatedAt="number"==typeof i.updatedAt?i.updatedAt:s):n={createdAt:s,updatedAt:s});const r=this.getId(),a={id:r,type:"Feature",geometry:e,properties:n};this.store[r]=a,this.spatialIndex.insert(a),o.push(r)}),this._onChange&&this._onChange([...o],"create",i),o}delete(t,e){t.forEach(t=>{if(!this.store[t])throw new Error(`No feature with id ${t}, can not delete`);delete this.store[t],this.spatialIndex.remove(t)}),this._onChange&&this._onChange([...t],"delete",e)}copy(t){return this.clone(this.store[t])}copyAll(){return this.clone(Object.keys(this.store).map(t=>this.store[t]))}copyAllWhere(t){return this.clone(Object.keys(this.store).map(t=>this.store[t]).filter(e=>e.properties&&t(e.properties)))}clear(){this.store={},this.spatialIndex.clear()}size(){return Object.keys(this.store).length}}function Se(t){const e=t.coordinates;let i=0;if(e&&e.length>0){i+=Math.abs(we(e[0]));for(let t=1;t<e.length;t++)i-=Math.abs(we(e[t]))}return i}const Ee=20294876564838.72,Fe=Math.PI/180;function we(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]*Fe-t[o][0]*Fe)*Math.sin(t[o+1===e?0:o+1][1]*Fe),o++;return i*Ee}const De="Feature is smaller than the minimum area",be=(t,e)=>"Polygon"!==t.geometry.type?{valid:!1,reason:g}:Se(t.geometry)<e?{valid:!1,reason:De}:{valid:!0},ke=(t,e)=>"Polygon"!==t.geometry.type?{valid:!1,reason:g}:Se(t.geometry)>e?{valid:!1,reason:"Feature is larger than the maximum area"}:{valid:!0},Oe="Feature is not a Polygon or LineString",_e="Feature intersects itself",je=t=>"Polygon"!==t.geometry.type&&"LineString"!==t.geometry.type?{valid:!1,reason:Oe}:j(t)?{valid:!1,reason:_e}:{valid:!0};function Be(t,e,i){const o=rt(t,e);let s=rt(e,i)-o;return s<0&&(s+=360),180-Math.abs(s-90-90)}const We={cancel:"Escape",finish:"Enter"},Ge={start:"crosshair",close:"pointer"};class Ne extends v{constructor(t){super(t,!0),this.mode="angled-rectangle",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=We,this.cursors=Ge,this.mouseMove=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents))}close(){if(void 0===this.currentId)return;this.store.updateProperty([{id:this.currentId,property:o,value:void 0}]);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 i=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),s=Math.max(1e-6,e);o=[i[0],[t.lng,t.lat],[t.lng,t.lat-s],i[0]]}else if(2===this.currentCoordinate){const e=i[0],s=i[1],n=Ut(e,s,this.coordinatePrecision,this.project,this.unproject),r=b(e[0],e[1]),a=b(n[0],n[1]),d=b(s[0],s[1]),l=b(t.lng,t.lat),h=Y(l,r)<Y(l,d),c=Be(r,a,l),u=h?90-c:Be(r,a,l)-90,p=Y(a,l),g=Math.cos(x(u))*p,y=rt(r,d)+("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"}(r,d,l)?-90:90),f=st(r,g,y),m=st(d,g,y),v=k(f.x,f.y),C=k(m.x,m.y);o=[i[0],i[1],[C.lng,C.lat],[v.lng,v.lat],i[0]]}o&&this.updatePolygonGeometry(this.currentId,o,e.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}).valid||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,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,[o]:!0}}]);this.currentId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const i=this.store.getGeometryCopy(this.currentId);if(ut([t.lng,t.lat],i.coordinates[0][0]))return;if(!this.updatePolygonGeometry(this.currentId,[i.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]],e.Commit))return;this.currentCoordinate++}else 2===this.currentCoordinate&&this.currentId&&this.close()}onKeyUp(t){if(t.key===this.keyEvents.cancel)this.cleanUp();else if(t.key===this.keyEvents.finish){if(this.currentCoordinate<2)return void this.cleanUp();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=h),i}validateFeature(t){return this.validateModeFeature(t,t=>R(t,this.coordinatePrecision))}}function Le(t,e,i){return(e.x-t.x)*(i.y-t.y)-(e.y-t.y)*(i.x-t.x)<=0}const Te={cancel:"Escape",finish:"Enter"},Ve={start:"crosshair",close:"pointer"};class Ue extends v{constructor(t){super(t,!0),this.mode="sector",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=Te,this.direction=void 0,this.arcPoints=64,this.cursors=Ve,this.mouseMove=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.arcPoints&&(this.arcPoints=e.arcPoints)}close(){if(void 0===this.currentId)return;const t=H(this.store.getGeometryCopy(this.currentId));t&&this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}]);const e=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.direction=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(e,{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 i=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),s=Math.max(1e-6,e);o=[i[0],[t.lng,t.lat],[t.lng,t.lat-s],i[0]]}else if(2===this.currentCoordinate){const e=i[0],s=i[1],n=[t.lng,t.lat],r=b(e[0],e[1]),a=b(s[0],s[1]),d=b(n[0],n[1]);if(void 0===this.direction){const t=Le(r,a,d);this.direction=t?"clockwise":"anticlockwise"}const l=Y(r,a),h=rt(r,a),c=rt(r,d),u=this.arcPoints,p=[e],g=at(h),y=at(c);let f;"anticlockwise"===this.direction?(f=y-g,f<0&&(f+=360)):(f=g-y,f<0&&(f+=360));const m=("anticlockwise"===this.direction?1:-1)*f/u;p.push(s);for(let t=0;t<=u;t++){const e=st(r,l,g+t*m),{lng:i,lat:o}=k(e.x,e.y),s=[E(i,this.coordinatePrecision),E(o,this.coordinatePrecision)];s[0]!==p[p.length-1][0]&&s[1]!==p[p.length-1][1]&&p.push(s)}p.push(e),o=[...p]}o&&this.updatePolygonGeometry(this.currentId,o,e.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}).valid||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,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,[o]:!0}}]);this.currentId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const i=this.store.getGeometryCopy(this.currentId);if(ut([t.lng,t.lat],i.coordinates[0][0]))return;if(!this.updatePolygonGeometry(this.currentId,[i.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]],e.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=h),i}validateFeature(t){return this.validateModeFeature(t,t=>R(t,this.coordinatePrecision))}}const Ae={cancel:"Escape",finish:"Enter"},Re={start:"crosshair",close:"pointer"};class Ke extends v{constructor(t){super(t,!0),this.mode="sensor",this.currentCoordinate=0,this.currentId=void 0,this.currentInitialArcId=void 0,this.currentStartingPointId=void 0,this.keyEvents=Ae,this.direction=void 0,this.arcPoints=64,this.cursors=Re,this.mouseMove=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.arcPoints&&(this.arcPoints=e.arcPoints)}close(){if(void 0===this.currentStartingPointId)return;const t=this.currentStartingPointId,e=this.currentInitialArcId,i=this.currentId;if(t&&this.store.delete([t]),e&&this.store.delete([e]),this.currentId){const t=H(this.store.getGeometryCopy(this.currentId));t&&this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}])}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 i=this.store.getGeometryCopy(this.currentInitialArcId).coordinates,o=this.store.getGeometryCopy(this.currentStartingPointId).coordinates,s=i[0],n=[t.lng,t.lat],r=b(s[0],s[1]),a=b(n[0],n[1]),d=b(o[0],o[1]),l=Y(d,r);if(void 0===this.direction){const t=Le(d,r,a);this.direction=t?"clockwise":"anticlockwise"}const h=rt(d,r),c=rt(d,a),u=this.arcPoints,p=[s],g=at(h),y=at(c);let f;"anticlockwise"===this.direction?(f=y-g,f<0&&(f+=360)):(f=g-y,f<0&&(f+=360));const m=("anticlockwise"===this.direction?1:-1)*f/u;for(let t=0;t<=u;t++){const e=st(d,l,g+t*m),{lng:i,lat:o}=k(e.x,e.y),s=[E(i,this.coordinatePrecision),E(o,this.coordinatePrecision)];s[0]!==p[p.length-1][0]&&s[1]!==p[p.length-1][1]&&p.push(s)}this.updateLineStringGeometry(this.currentInitialArcId,p,e.Provisional)}else if(3===this.currentCoordinate){const i=this.store.getGeometryCopy(this.currentInitialArcId).coordinates;if(i.length<2)return;if(!this.direction)return;const s=this.store.getGeometryCopy(this.currentStartingPointId).coordinates,n=i[0],r=i[i.length-1],a=b(t.lng,t.lat),d=b(n[0],n[1]),l=b(r[0],r[1]),h=b(s[0],s[1]),c=Y(h,d),u=Y(h,a)<c?d:a,p=rt(h,a),g=rt(h,d),y=rt(h,l),f=at(g),m=at(y),v=at(p);if(this.notInSector({normalizedCursor:v,normalizedStart:f,normalizedEnd:m,direction:this.direction}))return;const C=this.getDeltaBearing(this.direction,f,m),P=this.arcPoints,I=("anticlockwise"===this.direction?1:-1)*C/P,x=Y(h,u),M=[];for(let t=0;t<=P;t++){const e=st(h,x,f+t*I),{lng:o,lat:s}=k(e.x,e.y),n=[E(o,this.coordinatePrecision),E(s,this.coordinatePrecision)];n[0]!==i[i.length-1][0]&&n[1]!==i[i.length-1][1]&&M.unshift(n)}i.push(...M),i.push(i[0]),this.currentId?this.updatePolygonGeometry(this.currentId,i,e.Provisional):[this.currentId]=this.store.create([{geometry:{type:"Polygon",coordinates:[i]},properties:{mode:this.mode,[o]:!0}}])}}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}).valid||(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}).valid||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,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=h):"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=h):"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 this.validateModeFeature(t,t=>R(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}}class ze{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}}var Xe={__proto__:null,GeoJSONStore:Me,TerraDrawBaseDrawMode:v,TerraDrawBaseSelectMode:C,TerraDrawBaseAdapter:class{constructor(t){this._nextKeyUpIsContextMenu=!1,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,e=!1){const i=this.getLngLatFromEvent(t);if(!i)return null;const{lng:o,lat:s}=i,{containerX:n,containerY:r}=this.getMapElementXYPosition(t),a=this.getButton(t),d=Array.from(this._heldKeys);return{lng:E(o,this._coordinatePrecision),lat:E(s,this._coordinatePrecision),containerX:n,containerY:r,button:a,heldKeys:d,isContextMenu:e}}register(t){this._currentModeCallbacks=t,this._listeners=this.getAdapterListeners(),this._listeners.forEach(t=>{t.register()})}getCoordinatePrecision(){return this._coordinatePrecision}getAdapterListeners(){return[new ze({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 ze({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},i={x:e.containerX,y:e.containerY},o=this._currentModeCallbacks.getState(),s=Y(t,i);let n=!1;if(n="drawing"===o?s<this._minPixelDragDistanceDrawing:"selecting"===o?s<this._minPixelDragDistanceSelecting:s<this._minPixelDragDistance,n)return;this._nextKeyUpIsContextMenu=!1,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 ze({name:"contextmenu",callback:t=>{this._currentModeCallbacks&&(t.preventDefault(),this._nextKeyUpIsContextMenu=!0)},register:t=>{this.getMapEventElement().addEventListener("contextmenu",t)},unregister:t=>{this.getMapEventElement().removeEventListener("contextmenu",t)}}),new ze({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._nextKeyUpIsContextMenu&&(e.isContextMenu=!0,this._nextKeyUpIsContextMenu=!1),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 ze({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 ze({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(),this._currentModeCallbacks=void 0}},getDefaultStyling:()=>({polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0}),SELECT_PROPERTIES:i};const Ye={ValidationReasonFeatureNotPoint:St,ValidationReasonFeatureInvalidCoordinates:Et,ValidationReasonFeatureInvalidCoordinatePrecision:Ft,ValidationReasonFeatureNotPolygon:g,ValidationReasonFeatureHasHoles:L,ValidationReasonFeatureLessThanFourCoordinates:T,ValidationReasonFeatureHasInvalidCoordinates:V,ValidationReasonFeatureCoordinatesNotClosed:U,ValidationReasonFeatureNotPolygonOrLineString:Oe,ValidationReasonFeatureSelfIntersects:_e,ValidationReasonFeatureLessThanMinSize:De,ValidationReasonModeMismatch:y};class He{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 ne;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},{}),s=Object.keys(o);if(0===s.length)throw new Error("No modes provided");s.forEach(t=>{if(o[t].type===f.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 n=t=>{const e=[],i=this._store.copyAll().filter(i=>!t.includes(i.id)||(e.push(i),!1));return{changed:e,unchanged:i}},r=(t,e)=>{this._enabled&&this._eventListeners.finish.forEach(i=>{i(t,e)})},a=(t,e,i)=>{if(!this._enabled)return;this._eventListeners.change.forEach(o=>{o(t,e,i)});const{changed:o,unchanged:s}=n(t);"create"===e?this._adapter.render({created:o,deletedIds:[],unchanged:s,updated:[]},this.getModeStyles()):"update"===e?this._adapter.render({created:[],deletedIds:[],unchanged:s,updated:o},this.getModeStyles()):"delete"===e?this._adapter.render({created:[],deletedIds:t,unchanged:s,updated:[]},this.getModeStyles()):"styling"===e&&this._adapter.render({created:[],deletedIds:[],unchanged:s,updated:[]},this.getModeStyles())},d=t=>{if(!this._enabled)return;this._eventListeners.select.forEach(e=>{e(t)});const{changed:e,unchanged:i}=n([t]);this._adapter.render({created:[],deletedIds:[],unchanged:i,updated:e},this.getModeStyles())},l=t=>{if(!this._enabled)return;this._eventListeners.deselect.forEach(t=>{t()});const{changed:e,unchanged:i}=n([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:d,onDeselect:l,onFinish:r,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[i.SELECTED]?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},s){const r=s&&void 0!==s.pointerDistance?s.pointerDistance:30,d=!s||void 0===s.ignoreSelectFeatures||s.ignoreSelectFeatures,l=!(!s||void 0===s.ignoreCoordinatePoints)&&s.ignoreCoordinatePoints,h=!(!s||void 0===s.ignoreCurrentlyDrawing)&&s.ignoreCurrentlyDrawing,c=!(!s||void 0===s.ignoreClosingPoints)&&s.ignoreClosingPoints,u=this._adapter.unproject.bind(this._adapter),p=this._adapter.project.bind(this._adapter),g=p(t,e),y=Q({unproject:u,point:g,pointerDistance:r});return this._store.search(y).filter(s=>{if(d&&(s.properties[i.MID_POINT]||s.properties[i.SELECTION_POINT]))return!1;if(l&&s.properties[a])return!1;if(c&&s.properties[n])return!1;if(h&&s.properties[o])return!1;if("Point"===s.geometry.type){const t=s.geometry.coordinates,e=p(t[0],t[1]);return Y(g,e)<r}if("LineString"===s.geometry.type){const t=s.geometry.coordinates;for(let e=0;e<t.length-1;e++){const i=t[e],o=t[e+1];if(Yt(g,p(i[0],i[1]),p(o[0],o[1]))<r)return!0}return!1}{if(Xt([t,e],s.geometry.coordinates))return!0;const i=s.geometry.coordinates;for(const t of i)for(let e=0;e<t.length-1;e++){const i=t[e],o=t[e+1],s=p(i[0],i[1]),n=p(o[0],o[1]);if(Yt(g,s,n)<r)return!0}return!1}})}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}updateModeOptions(t,e){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._modes[t].updateOptions(e)}getSnapshot(){return this._store.copyAll()}getSnapshotFeature(t){if(this._store.has(t))return this._store.copy(t)}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}getModeState(){return this._mode.state}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();const e=[];t.forEach(t=>{if(!this._store.has(t))throw new Error(`No feature with id ${t}, can not delete`);const o=this._store.copy(t);o.properties[i.SELECTED]&&this.deselectFeature(t),o.properties[l]&&e.push(...o.properties[l])}),this._store.delete([...t,...e],{origin:"api"})}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){return this.checkEnabled(),0===t.length?[]:this._store.load(t,t=>{if(u(t)){const e=t.properties.mode,i=this._modes[e];if(!i)return{id:t.id,valid:!1,reason:`${e} mode is not in the list of instantiated modes`};const o=i.validateFeature.bind(i)(t);return{id:t.id,valid:o.valid,reason:o.reason?o.reason:o.valid?void 0:"Feature is invalid"}}return{id:t.id,valid:!1,reason:"Mode property does not exist"}},t=>{if(u(t)){const e=this._modes[t.properties.mode];e&&e.afterFeatureAdded&&e.afterFeatureAdded(t)}},{origin:"api"})}start(){this._enabled||(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&&(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{He as TerraDraw,Ne as TerraDrawAngledRectangleMode,X as TerraDrawCircleMode,Xe as TerraDrawExtend,Z as TerraDrawFreehandMode,Mt as TerraDrawLineStringMode,bt as TerraDrawPointMode,Bt as TerraDrawPolygonMode,Nt as TerraDrawRectangleMode,Lt as TerraDrawRenderMode,Ue as TerraDrawSectorMode,se as TerraDrawSelectMode,Ke as TerraDrawSensorMode,ke as ValidateMaxAreaSquareMeters,be as ValidateMinAreaSquareMeters,je as ValidateNotSelfIntersecting,Ye as ValidationReasons};
|
|
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)({}).hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t},t.apply(null,arguments)}var e;!function(t){t.Commit="commit",t.Provisional="provisional",t.Finish="finish"}(e||(e={}));const i={SELECTED:"selected",MID_POINT:"midPoint",SELECTION_POINT_FEATURE_ID:"selectionPointFeatureId",SELECTION_POINT:"selectionPoint"},o="currentlyDrawing",s="edited",n="closingPoint",r="snappingPoint",a="coordinatePoint",d="coordinatePointFeatureId",h="coordinatePointIds",l=10;function c(t){return Boolean(t&&"object"==typeof t&&null!==t&&!Array.isArray(t))}function u(t){return Boolean(t&&"object"==typeof t&&"properties"in t&&"object"==typeof t.properties&&null!==t.properties&&"mode"in t.properties)}function p(t){return!!function(t){return"number"==typeof t&&!isNaN(new Date(t).valueOf())}(t)}const g="Feature is not a Polygon",y="Feature mode property does not match the mode being added to";var f;!function(t){t.Drawing="drawing",t.Select="select",t.Static="static",t.Render="render"}(f||(f={}));const m={rightClick:!0,contextMenu:!1,leftClick:!0,onDragStart:!0,onDrag:!0,onDragEnd:!0};class v{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&&this.onStyleChange([],"styling"),this._styles=t}registerBehaviors(t){}constructor(t,e=!1){this._state="unregistered",this._styles={},this.pointerEvents=m,this.behaviors=[],this.validate=void 0,this.pointerDistance=40,this.coordinatePrecision=void 0,this.onStyleChange=void 0,this.store=void 0,this.projection="web-mercator",this.setDoubleClickToZoom=void 0,this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this.type=f.Drawing,this.mode="base",e||this.updateOptions(t)}updateOptions(e){null!=e&&e.styles&&(this.styles=t({},this._styles,e.styles)),null!=e&&e.pointerDistance&&(this.pointerDistance=e.pointerDistance),null!=e&&e.validation&&(this.validate=e&&e.validation),null!=e&&e.projection&&(this.projection=e.projection),void 0!==(null==e?void 0:e.pointerEvents)&&(this.pointerEvents=e.pointerEvents)}allowPointerEvent(t,e){return"boolean"==typeof t?t:"function"!=typeof t||t(e)}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){return this.performFeatureValidation(t)}afterFeatureAdded(t){}performFeatureValidation(t){if("unregistered"===this._state)throw new Error("Mode must be registered");const i=function(t,e){let i;if(c(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(c(t.geometry))if(c(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)return{valid:!1,reason:"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";return i?{valid:!1,reason:i}:{valid:!0}}(t,this.store.idStrategy.isValidId);if(this.validate){const o=this.validate(t,{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Provisional});return{valid:i.valid&&o.valid,reason:o.reason}}return{valid:i.valid,reason:i.reason}}validateModeFeature(t,e){const i=this.performFeatureValidation(t);return i.valid?t.properties.mode!==this.mode?{valid:!1,reason:y}:e(t):{valid:!1,reason:i.reason}}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 C extends v{constructor(...t){super(...t),this.type=f.Select}}function P(t,e){const i=t=>t*Math.PI/180,o=i(t[1]),s=i(t[0]),n=i(e[1]),r=n-o,a=i(e[0])-s,d=Math.sin(r/2)*Math.sin(r/2)+Math.cos(o)*Math.cos(n)*Math.sin(a/2)*Math.sin(a/2);return 2*Math.atan2(Math.sqrt(d),Math.sqrt(1-d))*6371e3/1e3}const I=6371008.8;function x(t){return t%360*Math.PI/180}function M(t){return t/6371.0088}function S(t){return t%(2*Math.PI)*180/Math.PI}function E(t,e=9){const i=Math.pow(10,e);return Math.round(t*i)/i}const F=57.29577951308232,w=.017453292519943295,D=6378137,b=(t,e)=>({x:0===t?0:t*w*D,y:0===e?0:Math.log(Math.tan(Math.PI/4+e*w/2))*D}),k=(t,e)=>({lng:0===t?0:F*(t/D),lat:0===e?0:(2*Math.atan(Math.exp(e/D))-Math.PI/2)*F});function O(t,e,i){const o=x(t[0]),s=x(t[1]),n=x(i),r=M(e),a=Math.asin(Math.sin(s)*Math.cos(r)+Math.cos(s)*Math.sin(r)*Math.cos(n));return[S(o+Math.atan2(Math.sin(n)*Math.sin(r)*Math.cos(s),Math.cos(r)-Math.sin(s)*Math.sin(a))),S(a)]}function _(t){const{center:e,radiusKilometers:i,coordinatePrecision:o}=t,s=t.steps?t.steps:64,n=[];for(let t=0;t<s;t++){const r=O(e,i,-360*t/s);n.push([E(r[0],o),E(r[1],o)])}return n.push(n[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[n]},properties:{}}}function j(t){let e;if("Polygon"===t.geometry.type)e=t.geometry.coordinates;else{if("LineString"!==t.geometry.type)throw new Error("Self intersects only accepts Polygons and LineStrings");e=[t.geometry.coordinates]}const i=[];for(let t=0;t<e.length;t++)for(let i=0;i<e[t].length-1;i++)for(let o=0;o<e.length;o++)for(let n=0;n<e[o].length-1;n++)s(t,i,o,n);return i.length>0;function o(t){return t<0||t>1}function s(t,s,n,r){const a=e[t][s],d=e[t][s+1],h=e[n][r],l=e[n][r+1],c=function(t,e,i,o){if(W(t,i)||W(t,o)||W(e,i)||W(o,i))return null;const s=t[0],n=t[1],r=e[0],a=e[1],d=i[0],h=i[1],l=o[0],c=o[1],u=(s-r)*(h-c)-(n-a)*(d-l);return 0===u?null:[((s*a-n*r)*(d-l)-(s-r)*(d*c-h*l))/u,((s*a-n*r)*(h-c)-(n-a)*(d*c-h*l))/u]}(a,d,h,l);if(null===c)return;let u,p;u=d[0]!==a[0]?(c[0]-a[0])/(d[0]-a[0]):(c[1]-a[1])/(d[1]-a[1]),p=l[0]!==h[0]?(c[0]-h[0])/(l[0]-h[0]):(c[1]-h[1])/(l[1]-h[1]),o(u)||o(p)||(c.toString(),i.push(c))}}function W(t,e){return t[0]===e[0]&&t[1]===e[1]}function B(t,e){return N(t[0])<=e&&N(t[1])<=e}function G(t){return 2===t.length&&"number"==typeof t[0]&&"number"==typeof t[1]&&Infinity!==t[0]&&Infinity!==t[1]&&(i=t[0])>=-180&&i<=180&&(e=t[1])>=-90&&e<=90;var e,i}function N(t){let e=1,i=0;for(;Math.round(t*e)/e!==t;)e*=10,i++;return i}const L="Feature has holes",T="Feature has less than 4 coordinates",V="Feature has invalid coordinates",U="Feature coordinates are not closed";function A(t,e){if("Polygon"!==t.geometry.type)return{valid:!1,reason:"Feature is not a Polygon"};if(1!==t.geometry.coordinates.length)return{valid:!1,reason:L};if(t.geometry.coordinates[0].length<4)return{valid:!1,reason:T};for(let i=0;i<t.geometry.coordinates[0].length;i++){if(!G(t.geometry.coordinates[0][i]))return{valid:!1,reason:V};if(!B(t.geometry.coordinates[0][i],e))return{valid:!1,reason:"Feature has coordinates with excessive precision"}}return(i=t.geometry.coordinates[0][0])[0]!==(o=t.geometry.coordinates[0][t.geometry.coordinates[0].length-1])[0]||i[1]!==o[1]?{valid:!1,reason:U}:{valid:!0};var i,o}function R(t,e){const i=A(t,e);return i.valid?j(t)?{valid:!1,reason:"Feature intersects itself"}:{valid:!0}:i}const K={cancel:"Escape",finish:"Enter"},z={start:"crosshair"};class X extends v{constructor(t){super(t,!0),this.mode="circle",this.center=void 0,this.clickCount=0,this.currentCircleId=void 0,this.keyEvents=K,this.cursors=z,this.startingRadiusKilometers=1e-5,this.cursorMovedAfterInitialCursorDown=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.startingRadiusKilometers&&(this.startingRadiusKilometers=e.startingRadiusKilometers)}close(){if(void 0===this.currentCircleId)return;this.store.updateProperty([{id:this.currentCircleId,property:o,value:void 0}]);const t=this.currentCircleId;if(this.validate&&t){const i=this.store.getGeometryCopy(t);if(!this.validate({type:"Feature",id:t,geometry:i,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Finish}).valid)return}this.cursorMovedAfterInitialCursorDown=!1,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("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t))if(0===this.clickCount){this.center=[t.lng,t.lat];const e=_({center:this.center,radiusKilometers:this.startingRadiusKilometers,coordinatePrecision:this.coordinatePrecision}),[i]=this.store.create([{geometry:e.geometry,properties:{mode:this.mode,radiusKilometers:this.startingRadiusKilometers,[o]:!0}}]);this.currentCircleId=i,this.clickCount++,this.cursorMovedAfterInitialCursorDown=!1,this.setDrawing()}else 1===this.clickCount&&this.center&&void 0!==this.currentCircleId&&this.cursorMovedAfterInitialCursorDown&&this.updateCircle(t),this.close()}onMouseMove(t){this.cursorMovedAfterInitialCursorDown=!0,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=l,i):i}validateFeature(t){return this.validateModeFeature(t,t=>R(t,this.coordinatePrecision))}updateCircle(t){if(1===this.clickCount&&this.center&&this.currentCircleId){const i=P(this.center,[t.lng,t.lat]);let o;if("web-mercator"===this.projection){const e=function(t,e){const i=1e3*P(t,e);if(0===i)return 1;const{x:o,y:s}=b(t[0],t[1]),{x:n,y:r}=b(e[0],e[1]);return Math.sqrt(Math.pow(n-o,2)+Math.pow(r-s,2))/i}(this.center,[t.lng,t.lat]);o=function(t){const{center:e,radiusKilometers:i,coordinatePrecision:o}=t,s=t.steps?t.steps:64,n=1e3*i,[r,a]=e,{x:d,y:h}=b(r,a),l=[];for(let t=0;t<s;t++){const e=360*t/s*Math.PI/180,i=n*Math.cos(e),r=n*Math.sin(e),[a,c]=[d+i,h+r],{lng:u,lat:p}=k(a,c);l.push([E(u,o),E(p,o)])}return l.push(l[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[l]},properties:{}}}({center:this.center,radiusKilometers:i*e,coordinatePrecision:this.coordinatePrecision})}else{if("globe"!==this.projection)throw new Error("Invalid projection");o=_({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:e.Provisional}).valid)return;this.store.updateGeometry([{id:this.currentCircleId,geometry:o.geometry}]),this.store.updateProperty([{id:this.currentCircleId,property:"radiusKilometers",value:i}])}}}const Y=(t,e)=>{const{x:i,y:o}=t,{x:s,y:n}=e,r=s-i,a=n-o;return Math.sqrt(a*a+r*r)};function H(t){const e=function(t){const e=t.coordinates[0];let i=0;for(let t=0;t<e.length-1;t++){const[o,s]=e[t],[n,r]=e[t+1];i+=(n-o)*(r+s)}return i<0}(t);if(!e)return{type:"Polygon",coordinates:[t.coordinates[0].reverse()]}}const q={cancel:"Escape",finish:"Enter"},$={start:"crosshair",close:"pointer"};class Z extends v{constructor(t){super(t,!0),this.mode="freehand",this.startingClick=!1,this.currentId=void 0,this.closingPointId=void 0,this.minDistance=20,this.keyEvents=q,this.cursors=$,this.preventPointsNearClose=!0,this.autoClose=!1,this.autoCloseTimeout=500,this.hasLeftStartingPoint=!1,this.preventNewFeature=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.minDistance&&(this.minDistance=e.minDistance),void 0!==(null==e?void 0:e.preventPointsNearClose)&&(this.preventPointsNearClose=e.preventPointsNearClose),void 0!==(null==e?void 0:e.autoClose)&&(this.autoClose=e.autoClose),null!=e&&e.autoCloseTimeout&&(this.autoCloseTimeout=e.autoCloseTimeout),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors))}close(){if(void 0===this.currentId)return;if(this.currentId){const t=H(this.store.getGeometryCopy(this.currentId));t&&this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}])}const t=this.currentId;if(this.validate&&t){const i=this.store.getGeometryCopy(t);if(!this.validate({type:"Feature",id:t,geometry:i,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Finish}).valid)return}this.closingPointId&&this.store.delete([this.closingPointId]),this.startingClick=!1,this.currentId=void 0,this.closingPointId=void 0,this.hasLeftStartingPoint=!1,"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 i=this.store.getGeometryCopy(this.currentId),o=i.coordinates[0].length-2,[s,n]=i.coordinates[0][o],{x:r,y:a}=this.project(s,n),d=Y({x:r,y:a},{x:t.containerX,y:t.containerY}),[h,l]=i.coordinates[0][0],{x:c,y:u}=this.project(h,l);if(Y({x:c,y:u},{x:t.containerX,y:t.containerY})<this.pointerDistance){if(this.autoClose&&this.hasLeftStartingPoint&&(this.preventNewFeature=!0,setTimeout(()=>{this.preventNewFeature=!1},this.autoCloseTimeout),this.close()),this.setCursor(this.cursors.close),this.preventPointsNearClose)return}else this.hasLeftStartingPoint=!0,this.setCursor(this.cursors.start);if(d<this.minDistance)return;i.coordinates[0].pop();const p={type:"Polygon",coordinates:[[...i.coordinates[0],[t.lng,t.lat],i.coordinates[0][0]]]};this.validate&&!this.validate({type:"Feature",id:this.currentId,geometry:p,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Provisional}).valid||this.store.updateGeometry([{id:this.currentId,geometry:p}])}onClick(t){if("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t)){if(this.preventNewFeature)return;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,[o]:!0}},{geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:{mode:this.mode,[n]:!0}}]);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&&!0===this.startingClick&&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=l,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=50,i):i}validateFeature(t){return this.validateModeFeature(t,t=>A(t,this.coordinatePrecision))}}class J{constructor({store:t,mode:e,project:i,unproject:o,pointerDistance:s,coordinatePrecision:n,projection:r}){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=s,this.coordinatePrecision=n,this.projection=r}}function Q({unproject:t,point:e,pointerDistance:i}){const o=i/2,{x:s,y:n}=e;return{type:"Feature",properties:{},geometry:{type:"Polygon",coordinates:[[t(s-o,n-o),t(s+o,n-o),t(s+o,n+o),t(s-o,n+o),t(s-o,n-o)].map(t=>[t.lng,t.lat])]}}}class tt extends J{constructor(t){super(t)}create(t){const{containerX:e,containerY:i}=t;return Q({unproject:this.unproject,point:{x:e,y:i},pointerDistance:this.pointerDistance})}}class et extends J{constructor(t){super(t)}measure(t,e){const{x:i,y:o}=this.project(e[0],e[1]);return Y({x:i,y:o},{x:t.containerX,y:t.containerY})}}class it extends J{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)).coordinate,this.getSnappableCoordinate=(t,e)=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e)).coordinate,this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),s={featureId:void 0,featureCoordinateIndex:void 0,coordinate: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((i,o)=>{const n=this.pixelDistance.measure(t,i);n<s.minDist&&n<this.pointerDistance&&(s.coordinate=i,s.minDist=n,s.featureId=e.id,s.featureCoordinateIndex=o)})}),s}}function ot(t,e,i){const o=x(t[0]),s=x(t[1]),n=x(i),r=M(e),a=Math.asin(Math.sin(s)*Math.cos(r)+Math.cos(s)*Math.sin(r)*Math.cos(n));return[S(o+Math.atan2(Math.sin(n)*Math.sin(r)*Math.cos(s),Math.cos(r)-Math.sin(s)*Math.sin(a))),S(a)]}function st({x:t,y:e},i,o){const s=x(o);return{x:t+i*Math.cos(s),y:e+i*Math.sin(s)}}function nt(t,e){const i=x(t[0]),o=x(e[0]),s=x(t[1]),n=x(e[1]),r=Math.sin(o-i)*Math.cos(n),a=Math.cos(s)*Math.sin(n)-Math.sin(s)*Math.cos(n)*Math.cos(o-i);return S(Math.atan2(r,a))}function rt({x:t,y:e},{x:i,y:o}){const s=i-t,n=o-e;if(0===s&&0===n)return 0;let r=Math.atan2(n,s);return r*=180/Math.PI,r>180?r-=360:r<-180&&(r+=360),r}function at(t){return(t+360)%360}function dt(t,e,i){const o=[],s=t.length;let n,r,a,d=0;for(let s=0;s<t.length&&!(e>=d&&s===t.length-1);s++){if(d>e&&0===o.length){if(n=e-d,!n)return o.push(t[s]),o;r=nt(t[s],t[s-1])-180,a=ot(t[s],n,r),o.push(a)}if(d>=i)return n=i-d,n?(r=nt(t[s],t[s-1])-180,a=ot(t[s],n,r),o.push(a),o):(o.push(t[s]),o);if(d>=e&&o.push(t[s]),s===t.length-1)return o;d+=P(t[s],t[s+1])}if(d<e&&t.length===s)throw new Error("Start position is beyond line");const h=t[t.length-1];return[h,h]}function ht(t){return t*(Math.PI/180)}function lt(t){return t*(180/Math.PI)}class ct extends J{constructor(t){super(t),this.config=void 0,this.config=t}generateInsertionCoordinates(t,e,i){const o=[t,e];let s=0;for(let t=0;t<o.length-1;t++)s+=P(o[0],o[1]);if(s<=i)return o;let n=s/i-1;Number.isInteger(n)||(n=Math.floor(n)+1);const r=[];for(let t=0;t<n;t++){const e=dt(o,i*t,i*(t+1));r.push(e)}const a=[];for(let t=0;t<r.length;t++)a.push(r[t][1]);return this.limitCoordinates(a)}generateInsertionGeodesicCoordinates(t,e,i){const o=P(t,e),s=function(t,e,i){const o=[],s=ht(t[1]),n=ht(t[0]),r=ht(e[1]),a=ht(e[0]);i+=1;const d=2*Math.asin(Math.sqrt(Math.sin((r-s)/2)**2+Math.cos(s)*Math.cos(r)*Math.sin((a-n)/2)**2));if(0===d||isNaN(d))return o;for(let t=0;t<=i;t++){const e=t/i,h=Math.sin((1-e)*d)/Math.sin(d),l=Math.sin(e*d)/Math.sin(d),c=h*Math.cos(s)*Math.cos(n)+l*Math.cos(r)*Math.cos(a),u=h*Math.cos(s)*Math.sin(n)+l*Math.cos(r)*Math.sin(a),p=h*Math.sin(s)+l*Math.sin(r);if(isNaN(c)||isNaN(u)||isNaN(p))continue;const g=Math.atan2(p,Math.sqrt(c**2+u**2)),y=Math.atan2(u,c);isNaN(g)||isNaN(y)||o.push([lt(y),lt(g)])}return o.slice(1,-1)}(t,e,Math.floor(o/i));return this.limitCoordinates(s)}limitCoordinates(t){return t.map(t=>[E(t[0],this.config.coordinatePrecision),E(t[1],this.config.coordinatePrecision)])}}function ut(t,e){return t[0]===e[0]&&t[1]===e[1]}function pt(t,e){if("LineString"!==t.geometry.type)return{valid:!1,reason:"Feature is not a LineString"};if(t.geometry.coordinates.length<2)return{valid:!1,reason:"Feature has less than 2 coordinates"};for(let i=0;i<t.geometry.coordinates.length;i++){if(!G(t.geometry.coordinates[i]))return{valid:!1,reason:"Feature has invalid coordinates"};if(!B(t.geometry.coordinates[i],e))return{valid:!1,reason:"Feature has coordinates with excessive precision"}}return{valid:!0}}function gt(t){return Math.sqrt(Math.pow(t[0],2)+Math.pow(t[1],2)+Math.pow(t[2],2))}function yt(t,e){const i=function(t,e){const[i,o,s]=t,[n,r,a]=e;return i*n+o*r+s*a}(t,e)/(gt(t)*gt(e));return Math.acos(Math.min(Math.max(i,-1),1))}function ft(t){const e=x(t[1]),i=x(t[0]);return[Math.cos(e)*Math.cos(i),Math.cos(e)*Math.sin(i),Math.sin(e)]}function mt(t){const[e,i,o]=t,s=S(Math.asin(o));return[S(Math.atan2(i,e)),s]}function vt(t,e,i){const o=ft(t),s=ft(e),n=ft(i),[r,a,d]=n,[h,l,c]=function(t,e){const[i,o,s]=t,[n,r,a]=e;return[o*a-s*r,s*n-i*a,i*r-o*n]}(o,s),u=l*d-c*a,p=c*r-h*d,g=h*a-l*r,y=g*l-p*c,f=u*c-g*h,m=p*h-u*l,v=1/Math.sqrt(Math.pow(y,2)+Math.pow(f,2)+Math.pow(m,2)),C=[y*v,f*v,m*v],I=[-1*y*v,-1*f*v,-1*m*v],x=yt(o,s),M=yt(o,C),S=yt(s,C),E=yt(o,I),F=yt(s,I);let w;return w=M<E&&M<F||S<E&&S<F?C:I,yt(o,w)>x||yt(s,w)>x?P(mt(w),mt(o))<=P(mt(w),mt(s))?[mt(o),!0,!1]:[mt(s),!1,!0]:[mt(w),!1,!1]}function Ct(t,e,i){const o=e.x-t.x,s=e.y-t.y,n=Math.max(0,Math.min(1,((i.x-t.x)*o+(i.y-t.y)*s)/(o*o+s*s)));return{x:t.x+n*o,y:t.y+n*s}}class Pt extends J{constructor(t,e,i){super(t),this.config=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.getSnappableCoordinateFirstClick=t=>{const e=this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode));return e.coordinate?[E(e.coordinate[0],this.config.coordinatePrecision),E(e.coordinate[1],this.config.coordinatePrecision)]:void 0},this.getSnappableCoordinate=(t,e)=>{const i=this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e));return i.coordinate?[E(i.coordinate[0],this.config.coordinatePrecision),E(i.coordinate[1],this.config.coordinatePrecision)]:void 0},this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),s={featureId:void 0,featureCoordinateIndex:void 0,coordinate:void 0,minDistance: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}const o=[];for(let t=0;t<i.length-1;t++)o.push([i[t],i[t+1]]);let n;const r=[t.lng,t.lat];if("web-mercator"===this.config.projection?n=function(t,e){let i=[Infinity,Infinity],o=Infinity,s=0;for(let n of e){const r=n[0],a=n[1];let d,h=Infinity;const l=b(r[0],r[1]),c=b(a[0],a[1]),u=b(t[0],t[1]);if(r[0]===t[0]&&r[1]===t[1])d=r;else if(a[0]===t[0]&&a[1]===t[1])d=a;else{const{x:t,y:e}=Ct(l,c,u),{lng:i,lat:o}=k(t,e);d=[i,o]}d&&(h=Y(u,b(d[0],d[1])),h<o&&(i=d,o=h,s=e.indexOf(n)))}return Infinity===o?void 0:{coordinate:i,lineIndex:s,distance:o}}(r,o):"globe"===this.config.projection&&(n=function(t,e){let i=[Infinity,Infinity],o=Infinity,s=0;for(let n of e){const r=n[0],a=n[1];let d,h=Infinity;r[0]===t[0]&&r[1]===t[1]?d=r:a[0]===t[0]&&a[1]===t[1]?d=a:[d]=vt(r,a,t),d&&(h=P(t,d),h<o&&(i=d,o=h,s=e.indexOf(n)))}return Infinity===o?void 0:{coordinate:i,distance:o,lineIndex:s}}(r,o)),!n)return;const a=this.pixelDistance.measure(t,n.coordinate);a<s.minDistance&&a<this.pointerDistance&&(s.featureId=e.id,s.coordinate=[E(n.coordinate[0],this.config.coordinatePrecision),E(n.coordinate[1],this.config.coordinatePrecision)],s.featureCoordinateIndex=n.lineIndex,s.minDistance=a)}),s}}const It={cancel:"Escape",finish:"Enter"},xt={start:"crosshair",close:"pointer",dragStart:"grabbing",dragEnd:"crosshair"};class Mt extends v{constructor(t){super(t,!0),this.mode="linestring",this.currentCoordinate=0,this.currentId=void 0,this.closingPointId=void 0,this.keyEvents=It,this.snapping=void 0,this.cursors=xt,this.mouseMove=!1,this.insertCoordinates=void 0,this.lastCommitedCoordinates=void 0,this.snappedPointId=void 0,this.editable=!1,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedSnapType=void 0,this.editedInsertIndex=void 0,this.editedPointId=void 0,this.coordinateSnapping=void 0,this.insertPoint=void 0,this.lineSnapping=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null!=e&&e.snapping&&(this.snapping=e.snapping),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.insertCoordinates&&(this.insertCoordinates=e.insertCoordinates),e&&e.editable&&(this.editable=e.editable)}close(){if(void 0===this.currentId)return;const t=this.store.getGeometryCopy(this.currentId);t.coordinates.pop(),this.updateGeometries([...t.coordinates],void 0,e.Commit),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}]);const i=this.currentId;this.closingPointId&&this.store.delete([this.closingPointId]),this.snappedPointId&&this.store.delete([this.snappedPointId]),this.currentCoordinate=0,this.currentId=void 0,this.closingPointId=void 0,this.snappedPointId=void 0,this.lastCommitedCoordinates=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(i,{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}).valid)return;const s=[{id:this.currentId,geometry:o}];this.closingPointId&&e&&s.push({id:this.closingPointId,geometry:{type:"Point",coordinates:e}}),"commit"===i&&(this.lastCommitedCoordinates=o.coordinates),this.store.updateGeometry(s)}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=P(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,[o]:!0}}]);this.lastCommitedCoordinates=[t,t],this.currentId=e,this.currentCoordinate++,this.setDrawing()}firstUpdateToLine(t){if(!this.currentId)return;const i=this.store.getGeometryCopy(this.currentId).coordinates,[o]=this.store.create([{geometry:{type:"Point",coordinates:[...t]},properties:{mode:this.mode}}]);this.closingPointId=o,this.setCursor(this.cursors.close);const s=[...i,t];this.updateGeometries(s,void 0,e.Commit),this.currentCoordinate++}updateToLine(t,i){if(!this.currentId)return;const o=this.store.getGeometryCopy(this.currentId).coordinates,[s,n]=this.lastCommitedCoordinates?this.lastCommitedCoordinates[this.lastCommitedCoordinates.length-1]:o[o.length-2],{x:r,y:a}=this.project(s,n);if(Y({x:r,y:a},{x:i.x,y:i.y})<this.pointerDistance)return void this.close();this.setCursor(this.cursors.close);const d=[...o,t];this.updateGeometries(d,o[o.length-1],e.Commit),this.currentCoordinate++}registerBehaviors(t){this.coordinateSnapping=new it(t,new et(t),new tt(t)),this.insertPoint=new ct(t),this.clickBoundingBox=new tt(t),this.pixelDistance=new et(t),this.lineSnapping=new Pt(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new it(t,this.pixelDistance,this.clickBoundingBox)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){this.mouseMove=!0,this.setCursor(this.cursors.start);const i=this.snapCoordinate(t);if(i){if(this.snappedPointId)this.store.updateGeometry([{id:this.snappedPointId,geometry:{type:"Point",coordinates:i}}]);else{const[t]=this.store.create([{geometry:{type:"Point",coordinates:i},properties:{mode:this.mode,[r]:!0}}]);this.snappedPointId=t}t.lng=i[0],t.lat=i[1]}else this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0);const o=i||[t.lng,t.lat];if(void 0===this.currentId||0===this.currentCoordinate)return;const s=this.store.getGeometryCopy(this.currentId).coordinates;if(s.pop(),this.closingPointId){const[e,i]=s[s.length-1],{x:o,y:n}=this.project(e,i);Y({x:o,y:n},{x:t.containerX,y:t.containerY})<this.pointerDistance&&this.setCursor(this.cursors.close)}let n=[...s,o];if(this.insertCoordinates&&this.currentId&&this.lastCommitedCoordinates){const t=this.lastCommitedCoordinates[this.lastCommitedCoordinates.length-1],e=o;if(!ut(t,e)){const i=this.generateInsertCoordinates(t,e);n=[...this.lastCommitedCoordinates.slice(0,-1),...i,o]}}this.updateGeometries(n,void 0,e.Provisional)}onRightClick(t){if(!this.editable||"started"!==this.state)return;const{featureId:i,featureCoordinateIndex:o}=this.coordinateSnapping.getSnappable(t,t=>this.lineStringFilter(t));if(!i||void 0===o)return;const s=this.store.getGeometryCopy(i);let n;if("LineString"===s.type&&(n=s.coordinates,!(n.length<=2))){if(n.splice(o,1),this.validate&&!this.validate({id:i,type:"Feature",geometry:s,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Commit}).valid)return;this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.store.updateGeometry([{id:i,geometry:s}]),this.onFinish(i,{mode:this.mode,action:"edit"})}}onLeftClick(t){this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0);const e=this.snapCoordinate(t)||[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})}onClick(t){("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t))&&(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,"right"===t.button?this.onRightClick(t):"left"===t.button&&this.onLeftClick(t))}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp(),t.key===this.keyEvents.finish&&this.close()}onDragStart(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDragStart,t))return;if(!this.editable)return;let i;if("started"===this.state){const e=this.lineSnapping.getSnappable(t,t=>this.lineStringFilter(t));e.coordinate&&(this.editedSnapType="line",this.editedFeatureCoordinateIndex=e.featureCoordinateIndex,this.editedFeatureId=e.featureId,i=e.coordinate);const o=this.coordinateSnapping.getSnappable(t,t=>this.lineStringFilter(t));o.coordinate&&(this.editedSnapType="coordinate",this.editedFeatureCoordinateIndex=o.featureCoordinateIndex,this.editedFeatureId=o.featureId,i=o.coordinate)}if(this.editedFeatureId&&i){if(!this.editedPointId){const[t]=this.store.create([{geometry:{type:"Point",coordinates:i},properties:{mode:this.mode,[s]:!0}}]);this.editedPointId=t}this.setCursor(this.cursors.dragStart),e(!1)}}onDrag(t,i){if(!this.allowPointerEvent(this.pointerEvents.onDrag,t))return;if(void 0===this.editedFeatureId||void 0===this.editedFeatureCoordinateIndex)return;const o=this.store.getGeometryCopy(this.editedFeatureId);"coordinate"===this.editedSnapType||"line"===this.editedSnapType&&void 0!==this.editedInsertIndex?o.coordinates[this.editedFeatureCoordinateIndex]=[t.lng,t.lat]:"line"===this.editedSnapType&&void 0===this.editedInsertIndex&&(this.editedInsertIndex=this.editedFeatureCoordinateIndex+1,o.coordinates.splice(this.editedInsertIndex,0,[t.lng,t.lat]),this.editedFeatureCoordinateIndex++);const n={type:"LineString",coordinates:o.coordinates};this.validate&&!this.validate({type:"Feature",geometry:n,properties:this.store.getPropertiesCopy(this.editedFeatureId)},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Provisional}).valid||(this.snapping&&this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.store.updateGeometry([{id:this.editedFeatureId,geometry:n}]),this.editedPointId&&this.store.updateGeometry([{id:this.editedPointId,geometry:{type:"Point",coordinates:[t.lng,t.lat]}}]),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!0}]))}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&void 0!==this.editedFeatureId&&(this.setCursor(this.cursors.dragEnd),this.editedPointId&&(this.store.delete([this.editedPointId]),this.editedPointId=void 0),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!1}]),this.onFinish(this.editedFeatureId,{mode:this.mode,action:"edit"}),this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0,e(!0))}cleanUp(){const t=this.currentId,e=this.closingPointId,i=this.snappedPointId;this.closingPointId=void 0,this.snappedPointId=void 0,this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted();try{void 0!==t&&this.store.delete([t]),void 0!==i&&this.store.delete([i]),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});if("Feature"===e.type&&"LineString"===e.geometry.type&&e.properties.mode===this.mode)return i.lineStringColor=this.getHexColorStylingValue(this.styles.lineStringColor,i.lineStringColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.lineStringWidth,i.lineStringWidth,e),i.zIndex=l,i;if("Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode){const t=e.properties[n];return i.pointColor=this.getHexColorStylingValue(t?this.styles.closingPointColor:this.styles.snappingPointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(t?this.styles.closingPointWidth:this.styles.snappingPointWidth,i.pointWidth,e),i.pointOutlineColor=this.getHexColorStylingValue(t?this.styles.closingPointOutlineColor:this.styles.snappingPointOutlineColor,"#ffffff",e),i.pointOutlineWidth=this.getNumericStylingValue(t?this.styles.closingPointOutlineWidth:this.styles.snappingPointOutlineWidth,2,e),i.zIndex=50,i}return i}validateFeature(t){return this.validateModeFeature(t,t=>pt(t,this.coordinatePrecision))}lineStringFilter(t){return Boolean("LineString"===t.geometry.type&&t.properties&&t.properties.mode===this.mode)}snapCoordinate(t){var e,i,o;let s;if(null!=(e=this.snapping)&&e.toLine){let e;e=this.currentId?this.lineSnapping.getSnappableCoordinate(t,this.currentId):this.lineSnapping.getSnappableCoordinateFirstClick(t),e&&(s=e)}return null!=(i=this.snapping)&&i.toCoordinate&&(s=this.currentId?this.coordinateSnapping.getSnappableCoordinate(t,this.currentId):this.coordinateSnapping.getSnappableCoordinateFirstClick(t)),null!=(o=this.snapping)&&o.toCustom&&(s=this.snapping.toCustom(t,{currentCoordinate:this.currentCoordinate,currentId:this.currentId,getCurrentGeometrySnapshot:this.currentId?()=>this.store.getGeometryCopy(this.currentId):()=>null,project:this.project,unproject:this.unproject})),s}}const St="Feature is not a Point",Et="Feature has invalid coordinates",Ft="Feature has coordinates with excessive precision";function wt(t,e){return"Point"!==t.geometry.type?{valid:!1,reason:St}:G(t.geometry.coordinates)?B(t.geometry.coordinates,e)?{valid:!0}:{valid:!1,reason:Ft}:{valid:!1,reason:Et}}const Dt={create:"crosshair",dragStart:"grabbing",dragEnd:"crosshair"};class bt extends v{constructor(t){super(t,!0),this.mode="point",this.cursors=Dt,this.editable=!1,this.editedFeatureId=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null!=e&&e.editable&&(this.editable=e.editable)}start(){this.setStarted(),this.setCursor(this.cursors.create)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){"right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t)?this.onRightClick(t):"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)&&this.onLeftClick(t)}onMouseMove(){}onKeyDown(){}onKeyUp(){}cleanUp(){this.editedFeatureId=void 0}onDragStart(t,e){if(this.allowPointerEvent(this.pointerEvents.onDragStart,t)){if(this.editable){const e=this.getNearestPointFeature(t);this.editedFeatureId=null==e?void 0:e.id}this.editedFeatureId&&(this.setCursor(this.cursors.dragStart),e(!1))}}onDrag(t,i){this.allowPointerEvent(this.pointerEvents.onDrag,t)&&void 0!==this.editedFeatureId&&(this.validate&&!this.validate({type:"Feature",geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:this.store.getPropertiesCopy(this.editedFeatureId)},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Finish}).valid||(this.store.updateGeometry([{id:this.editedFeatureId,geometry:{type:"Point",coordinates:[t.lng,t.lat]}}]),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!0}])))}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&void 0!==this.editedFeatureId&&(this.onFinish(this.editedFeatureId,{mode:this.mode,action:"edit"}),this.setCursor(this.cursors.dragEnd),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!1}]),this.editedFeatureId=void 0,e(!0))}registerBehaviors(t){this.pixelDistance=new et(t),this.clickBoundingBox=new tt(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("Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode){const t=Boolean(e.id&&this.editedFeatureId===e.id);i.pointWidth=this.getNumericStylingValue(t?this.styles.editedPointWidth:this.styles.pointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(t?this.styles.editedPointColor:this.styles.pointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(t?this.styles.editedPointOutlineColor:this.styles.pointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(t?this.styles.editedPointOutlineWidth:this.styles.pointOutlineWidth,2,e),i.zIndex=30}return i}validateFeature(t){return this.validateModeFeature(t,t=>wt(t,this.coordinatePrecision))}onLeftClick(t){const i={type:"Point",coordinates:[t.lng,t.lat]},o={mode:this.mode};if(this.validate&&!this.validate({type:"Feature",geometry:i,properties:o},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Finish}).valid)return;const[s]=this.store.create([{geometry:i,properties:o}]);this.onFinish(s,{mode:this.mode,action:"draw"})}onRightClick(t){if(!this.editable)return;const e=this.getNearestPointFeature(t);e&&this.store.delete([e.id])}getNearestPointFeature(t){const e=this.clickBoundingBox.create(t),i=this.store.search(e);let o,s=Infinity;for(let e=0;e<i.length;e++){const n=i[e];if("Point"!==n.geometry.type||n.properties.mode!==this.mode)continue;const r=this.pixelDistance.measure(t,n.geometry.coordinates);r>s||r>this.pointerDistance||(s=r,o=n)}return o}}class kt extends J{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,[n]:!0}},{geometry:{type:"Point",coordinates:t[t.length-2]},properties:{mode:e,[n]:!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),s=this.pixelDistance.measure(t,i.coordinates);return{isClosing:o<this.pointerDistance,isPreviousClosing:s<this.pointerDistance}}}class Ot extends J{constructor(t){super(t)}createOrUpdate(t){const e=this.store.getGeometryCopy(t),i=this.store.getPropertiesCopy(t);let o;if("Polygon"===e.type)o=e.coordinates[0].slice(0,-1);else{if("LineString"!==e.type)return;o=e.coordinates}const s=this.store.getPropertiesCopy(t),n=s.coordinatePointIds;if(n)if(n&&n.every(t=>this.store.has(t))){const e=s.coordinatePointIds,n=e.map(t=>this.store.getGeometryCopy(t).coordinates);if(e.length!==o.length){this.deleteCoordinatePoints(e);const s=this.createPoints(o,i.mode,t);this.setFeatureCoordinatePoints(t,s)}else o.forEach((t,i)=>{t[0]===n[i][0]&&t[1]===n[i][1]||this.store.updateGeometry([{id:e[i],geometry:{type:"Point",coordinates:t}}])})}else{const e=n.filter(t=>this.store.has(t));e.length&&this.deleteCoordinatePoints(e);const s=this.createPoints(o,i.mode,t);this.setFeatureCoordinatePoints(t,s)}else{const e=this.createPoints(o,i.mode,t);this.setFeatureCoordinatePoints(t,e)}}deletePointsByFeatureIds(t){for(const e of t)this.deleteIfPresent(e)}getUpdated(e,i){const o=this.store.getPropertiesCopy(e);if(o.coordinatePointIds)return o.coordinatePointIds.map((e,o)=>({id:e,geometry:t({},this.store.getGeometryCopy(e),{coordinates:i[o]})}))}createPoints(t,e,i){return this.store.create(t.map((t,o)=>({geometry:{type:"Point",coordinates:t},properties:{mode:e,[a]:!0,[d]:i,index:o}})))}setFeatureCoordinatePoints(t,e){this.store.updateProperty([{id:t,property:h,value:e}])}deleteCoordinatePoints(t){const e=t.filter(t=>this.store.has(t));this.store.delete(e)}deleteIfPresent(t){const e=this.store.getPropertiesCopy(t).coordinatePointIds;e&&(this.deleteCoordinatePoints(e),this.setFeatureCoordinatePoints(t,null))}}const _t={cancel:"Escape",finish:"Enter"},jt={start:"crosshair",close:"pointer",dragStart:"grabbing",dragEnd:"crosshair"};class Wt extends v{constructor(t){super(t,!0),this.mode="polygon",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=_t,this.cursors=jt,this.mouseMove=!1,this.showCoordinatePoints=!1,this.snapping=void 0,this.snappedPointId=void 0,this.editable=!1,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedSnapType=void 0,this.editedInsertIndex=void 0,this.editedPointId=void 0,this.coordinatePoints=void 0,this.lineSnapping=void 0,this.coordinateSnapping=void 0,this.pixelDistance=void 0,this.closingPoints=void 0,this.clickBoundingBox=void 0,this.updateOptions(t)}updateOptions(e){if(super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.snapping&&(this.snapping=e.snapping),void 0!==(null==e?void 0:e.editable)&&(this.editable=e.editable),void 0!==(null==e?void 0:e.pointerEvents)&&(this.pointerEvents=e.pointerEvents),void 0!==(null==e?void 0:e.showCoordinatePoints))if(this.showCoordinatePoints=e.showCoordinatePoints,this.coordinatePoints&&!0===e.showCoordinatePoints)this.store.copyAllWhere(t=>t.mode===this.mode).map(t=>t.id).forEach(t=>{this.coordinatePoints.createOrUpdate(t)});else if(this.coordinatePoints&&!1===this.showCoordinatePoints){const t=this.store.copyAllWhere(t=>t.mode===this.mode&&Boolean(t[h]));this.coordinatePoints.deletePointsByFeatureIds(t.map(t=>t.id))}}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]],e.Finish))return;const i=this.currentId;if(this.currentId){const t=H(this.store.getGeometryCopy(this.currentId));t&&this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}])}this.snappedPointId&&this.store.delete([this.snappedPointId]),this.currentCoordinate=0,this.currentId=void 0,this.snappedPointId=void 0,this.closingPoints.delete(),"drawing"===this.state&&this.setStarted(),this.onFinish(i,{mode:this.mode,action:"draw"})}registerBehaviors(t){this.clickBoundingBox=new tt(t),this.pixelDistance=new et(t),this.lineSnapping=new Pt(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new it(t,this.pixelDistance,this.clickBoundingBox),this.closingPoints=new kt(t,this.pixelDistance),this.coordinatePoints=new Ot(t)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onMouseMove(t){this.mouseMove=!0,this.setCursor(this.cursors.start);const i=this.snapCoordinate(t);if(i){if(this.snappedPointId)this.store.updateGeometry([{id:this.snappedPointId,geometry:{type:"Point",coordinates:i}}]);else{const[t]=this.store.create([{geometry:{type:"Point",coordinates:i},properties:{mode:this.mode,[r]:!0}}]);this.snappedPointId=t}t.lng=i[0],t.lat=i[1]}else this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0);if(void 0===this.currentId||0===this.currentCoordinate)return;const o=this.store.getGeometryCopy(this.currentId).coordinates[0];let s;if(1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),i=Math.max(1e-6,e);s=[o[0],[t.lng,t.lat],[t.lng,t.lat-i],o[0]]}else if(2===this.currentCoordinate)s=[o[0],o[1],[t.lng,t.lat],o[0]];else{const{isClosing:e,isPreviousClosing:i}=this.closingPoints.isClosingPoint(t);i||e?(this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.setCursor(this.cursors.close),s=[...o.slice(0,-2),o[0],o[0]]):s=[...o.slice(0,-2),[t.lng,t.lat],o[0]]}this.updatePolygonGeometry(s,e.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}).valid||(this.store.updateGeometry([{id:this.currentId,geometry:i}]),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(this.currentId),0))}snapCoordinate(t){var e,i,o;let s;if(null!=(e=this.snapping)&&e.toLine){let e;e=this.currentId?this.lineSnapping.getSnappableCoordinate(t,this.currentId):this.lineSnapping.getSnappableCoordinateFirstClick(t),e&&(s=e)}if(null!=(i=this.snapping)&&i.toCoordinate){let e;e=this.currentId?this.coordinateSnapping.getSnappableCoordinate(t,this.currentId):this.coordinateSnapping.getSnappableCoordinateFirstClick(t),e&&(s=e)}return null!=(o=this.snapping)&&o.toCustom&&(s=this.snapping.toCustom(t,{currentCoordinate:this.currentCoordinate,currentId:this.currentId,getCurrentGeometrySnapshot:this.currentId?()=>this.store.getGeometryCopy(this.currentId):()=>null,project:this.project,unproject:this.unproject})),s}polygonFilter(t){return Boolean("Polygon"===t.geometry.type&&t.properties&&t.properties.mode===this.mode)}onRightClick(t){if(!this.editable||"started"!==this.state)return;const{featureId:i,featureCoordinateIndex:o}=this.coordinateSnapping.getSnappable(t,t=>this.polygonFilter(t));if(!i||void 0===o)return;const s=this.store.getGeometryCopy(i);let n;"Polygon"===s.type&&(n=s.coordinates[0],n.length<=4||("Polygon"!==s.type||0!==o&&o!==n.length-1?n.splice(o,1):(n.shift(),n.pop(),n.push([n[0][0],n[0][1]])),(!this.validate||this.validate({id:i,type:"Feature",geometry:s,properties:{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Commit}).valid)&&(this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.store.updateGeometry([{id:i,geometry:s}]),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(i),this.onFinish(i,{mode:this.mode,action:"edit"}))))}onLeftClick(t){if(this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),0===this.currentCoordinate){const e=this.snapCoordinate(t);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,[o]:!0}}]);this.currentId=i,this.currentCoordinate++,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(i),this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const i=this.snapCoordinate(t);i&&(t.lng=i[0],t.lat=i[1]);const o=this.store.getGeometryCopy(this.currentId);if(ut([t.lng,t.lat],o.coordinates[0][0]))return;if(!this.updatePolygonGeometry([o.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],o.coordinates[0][0]],e.Commit))return;this.currentCoordinate++}else if(2===this.currentCoordinate&&this.currentId){const i=this.snapCoordinate(t);i&&(t.lng=i[0],t.lat=i[1]);const o=this.store.getGeometryCopy(this.currentId).coordinates[0];if(ut([t.lng,t.lat],o[1]))return;if(!this.updatePolygonGeometry([o[0],o[1],[t.lng,t.lat],[t.lng,t.lat],o[0]],e.Commit))return;2===this.currentCoordinate&&this.closingPoints.create(o,"polygon"),this.currentCoordinate++}else if(this.currentId){const i=this.store.getGeometryCopy(this.currentId).coordinates[0],{isClosing:o,isPreviousClosing:s}=this.closingPoints.isClosingPoint(t);if(s||o)this.close();else{const o=this.snapCoordinate(t);if(o&&(t.lng=o[0],t.lat=o[1]),ut([t.lng,t.lat],i[this.currentCoordinate-1]))return;const s=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(s.geometry.coordinates[0],e.Commit))return;this.currentCoordinate++,this.closingPoints.ids.length&&this.closingPoints.update(s.geometry.coordinates[0])}}}onClick(t){this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,"right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t)?this.onRightClick(t):"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)&&this.onLeftClick(t)}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onKeyDown(){}onDragStart(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDragStart,t))return;if(!this.editable)return;let i;if("started"===this.state){const e=this.lineSnapping.getSnappable(t,t=>this.polygonFilter(t));e.coordinate&&(this.editedSnapType="line",this.editedFeatureCoordinateIndex=e.featureCoordinateIndex,this.editedFeatureId=e.featureId,i=e.coordinate);const o=this.coordinateSnapping.getSnappable(t,t=>this.polygonFilter(t));o.coordinate&&(this.editedSnapType="coordinate",this.editedFeatureCoordinateIndex=o.featureCoordinateIndex,this.editedFeatureId=o.featureId,i=o.coordinate)}if(this.editedFeatureId&&i){if(!this.editedPointId){const[t]=this.store.create([{geometry:{type:"Point",coordinates:i},properties:{mode:this.mode,[s]:!0}}]);this.editedPointId=t}this.setCursor(this.cursors.dragStart),e(!1)}}onDrag(t,i){if(!this.allowPointerEvent(this.pointerEvents.onDrag,t))return;if(void 0===this.editedFeatureId||void 0===this.editedFeatureCoordinateIndex)return;const o=this.store.getGeometryCopy(this.editedFeatureId),n=o.coordinates[0];"coordinate"===this.editedSnapType||"line"===this.editedSnapType&&void 0!==this.editedInsertIndex?0===this.editedFeatureCoordinateIndex||this.editedFeatureCoordinateIndex===o.coordinates[0].length-1?(n[0]=[t.lng,t.lat],n[n.length-1]=[t.lng,t.lat]):n[this.editedFeatureCoordinateIndex]=[t.lng,t.lat]:"line"===this.editedSnapType&&void 0===this.editedInsertIndex&&(this.editedInsertIndex=this.editedFeatureCoordinateIndex+1,o.coordinates[0].splice(this.editedInsertIndex,0,[t.lng,t.lat]),this.editedFeatureCoordinateIndex++);const r={type:"Polygon",coordinates:o.coordinates};this.validate&&!this.validate({type:"Feature",geometry:r,properties:this.store.getPropertiesCopy(this.editedFeatureId)},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Provisional}).valid||(this.snapping&&this.snappedPointId&&(this.store.delete([this.snappedPointId]),this.snappedPointId=void 0),this.store.updateGeometry([{id:this.editedFeatureId,geometry:r}]),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(this.editedFeatureId),this.editedPointId&&this.store.updateGeometry([{id:this.editedPointId,geometry:{type:"Point",coordinates:[t.lng,t.lat]}}]),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!0}]))}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&void 0!==this.editedFeatureId&&(this.setCursor(this.cursors.dragEnd),this.editedPointId&&(this.store.delete([this.editedPointId]),this.editedPointId=void 0),this.store.updateProperty([{id:this.editedFeatureId,property:s,value:!1}]),this.onFinish(this.editedFeatureId,{mode:this.mode,action:"edit"}),this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0,e(!0))}cleanUp(){const t=this.currentId,e=this.snappedPointId,i=this.editedPointId;this.currentId=void 0,this.snappedPointId=void 0,this.editedPointId=void 0,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted();try{t&&this.coordinatePoints.deletePointsByFeatureIds([t]),void 0!==t&&this.store.delete([t]),void 0!==i&&this.store.delete([i]),void 0!==e&&this.store.delete([e]),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=l,i;if("Point"===e.geometry.type){const t=e.properties[s],o=e.properties[a],d=t?"editedPoint":e.properties[n]?"closingPoint":e.properties[r]?"snappingPoint":o?"coordinatePoint":void 0;if(!d)return i;const h={editedPoint:{width:this.styles.editedPointOutlineWidth,color:this.styles.editedPointColor,outlineColor:this.styles.editedPointOutlineColor,outlineWidth:this.styles.editedPointOutlineWidth},closingPoint:{width:this.styles.closingPointWidth,color:this.styles.closingPointColor,outlineColor:this.styles.closingPointOutlineColor,outlineWidth:this.styles.closingPointOutlineWidth},snappingPoint:{width:this.styles.snappingPointWidth,color:this.styles.snappingPointColor,outlineColor:this.styles.snappingPointOutlineColor,outlineWidth:this.styles.snappingPointOutlineWidth},coordinatePoint:{width:this.styles.coordinatePointWidth,color:this.styles.coordinatePointColor,outlineColor:this.styles.coordinatePointOutlineColor,outlineWidth:this.styles.coordinatePointOutlineWidth}};return i.pointWidth=this.getNumericStylingValue(h[d].width,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(h[d].color,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(h[d].outlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(h[d].outlineWidth,2,e),i.zIndex=t?40:o?20:30,i}}return i}afterFeatureAdded(t){this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate(t.id)}validateFeature(t){return this.validateModeFeature(t,t=>A(t,this.coordinatePrecision))}}const Bt={cancel:"Escape",finish:"Enter"},Gt={start:"crosshair"};class Nt extends v{constructor(t){super(t,!0),this.mode="rectangle",this.center=void 0,this.clickCount=0,this.currentRectangleId=void 0,this.keyEvents=Bt,this.cursors=Gt,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents))}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}).valid)return;this.store.updateGeometry([{id:this.currentRectangleId,geometry:o}])}}close(){const t=this.currentRectangleId;if(t){const e=H(this.store.getGeometryCopy(t));e&&this.store.updateGeometry([{id:t,geometry:e}]),this.store.updateProperty([{id:t,property:o,value:void 0}])}this.center=void 0,this.currentRectangleId=void 0,this.clickCount=0,"drawing"===this.state&&this.setStarted(),void 0!==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("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,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,[o]:!0}}]);this.currentRectangleId=e,this.clickCount++,this.setDrawing()}else this.updateRectangle(t,e.Finish),this.close()}onMouseMove(t){this.updateRectangle(t,e.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=l,i):i}validateFeature(t){return this.validateModeFeature(t,t=>R(t,this.coordinatePrecision))}}class Lt extends v{constructor(t){if(!t.modeName)throw new Error("Mode name is required for TerraDrawRenderMode");super(t,!0),this.type=f.Render,this.mode="render",this.updateOptions(t)}updateOptions(t){super.updateOptions(t),null!=t&&t.modeName&&(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){const e=super.validateFeature(t);if(e.valid){const e=t,i=wt(e,this.coordinatePrecision).valid||A(e,this.coordinatePrecision).valid||pt(e,this.coordinatePrecision).valid;return i?{valid:!0}:{valid:i,reason:"Feature is not a valid Point, Polygon or LineString feature"}}return e}}function Tt(t,e){const i=t,o=e,s=x(i[1]),n=x(o[1]);let r=x(o[0]-i[0]);r>Math.PI&&(r-=2*Math.PI),r<-Math.PI&&(r+=2*Math.PI);const a=Math.log(Math.tan(n/2+Math.PI/4)/Math.tan(s/2+Math.PI/4)),d=(S(Math.atan2(r,a))+360)%360;return d>180?-(360-d):d}function Vt(t,e,i){let o=e;e<0&&(o=-Math.abs(o));const s=o/I,n=t[0]*Math.PI/180,r=x(t[1]),a=x(i),d=s*Math.cos(a);let h=r+d;Math.abs(h)>Math.PI/2&&(h=h>0?Math.PI-h:-Math.PI-h);const l=Math.log(Math.tan(h/2+Math.PI/4)/Math.tan(r/2+Math.PI/4)),c=Math.abs(l)>1e-11?d/l:Math.cos(r),u=[(180*(n+s*Math.sin(a)/c)/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 Ut(t,e,i,o,s){const n=o(t[0],t[1]),r=o(e[0],e[1]),{lng:a,lat:d}=s((n.x+r.x)/2,(n.y+r.y)/2);return[E(a,i),E(d,i)]}function At(t,e,i){const o=Vt(t,1e3*P(t,e)/2,Tt(t,e));return[E(o[0],i),E(o[1],i)]}function Rt({featureCoords:t,precision:e,unproject:i,project:o,projection:s}){const n=[];for(let r=0;r<t.length-1;r++){let a;if("web-mercator"===s)a=Ut(t[r],t[r+1],e,o,i);else{if("globe"!==s)throw new Error("Invalid projection");a=At(t[r],t[r+1],e)}n.push(a)}return n}class Kt extends J{constructor(t,e,i){super(t),this.config=void 0,this.selectionPointBehavior=void 0,this.coordinatePointBehavior=void 0,this._midPoints=[],this.config=t,this.selectionPointBehavior=e,this.coordinatePointBehavior=i}get ids(){return this._midPoints.concat()}set ids(t){}insert(t,e,i){const o=this.store.getGeometryCopy(e),{midPointFeatureId:s,midPointSegment:n}=this.store.getPropertiesCopy(e),r=this.store.getGeometryCopy(s),a="Polygon"===r.type?r.coordinates[0]:r.coordinates;a.splice(n+1,0,o.coordinates),r.coordinates="Polygon"===r.type?[a]:a,this.store.updateGeometry([{id:s,geometry:r}]),this.store.getPropertiesCopy(t)[h]&&this.coordinatePointBehavior.createOrUpdate(t),this.store.delete([...this._midPoints,...this.selectionPointBehavior.ids]),this.create(a,s,i),this.selectionPointBehavior.create(a,r.type,s)}create(t,e,o){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,s,n){return Rt({featureCoords:t,precision:i,project:o,unproject:s,projection:n}).map((t,i)=>({geometry:{type:"Point",coordinates:t},properties:e(i)}))}(t,t=>({mode:this.mode,[i.MID_POINT]:!0,midPointSegment:t,midPointFeatureId:e}),o,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 Rt({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 zt extends J{constructor(t){super(t),this._selectionPoints=[]}get ids(){return this._selectionPoints.concat()}set ids(t){}create(t,e,o){this._selectionPoints=this.store.create(function(t,e,i){const o=[],s="Polygon"===e?t.length-1:t.length;for(let e=0;e<s;e++)o.push({geometry:{type:"Point",coordinates:t[e]},properties:i(e)});return o}(t,e,t=>({mode:this.mode,index:t,[i.SELECTION_POINT]:!0,[i.SELECTION_POINT_FEATURE_ID]:o})))}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 Xt(t,e){let i=!1;for(let r=0,a=e.length;r<a;r++){const a=e[r];for(let e=0,r=a.length,d=r-1;e<r;d=e++)(s=a[e])[1]>(o=t)[1]!=(n=a[d])[1]>o[1]&&o[0]<(n[0]-s[0])*(o[1]-s[1])/(n[1]-s[1])+s[0]&&(i=!i)}var o,s,n;return i}const Yt=(t,e,i)=>{const o=t=>t*t,s=(t,e)=>o(t.x-e.x)+o(t.y-e.y);return Math.sqrt(((t,e,i)=>{const o=s(e,i);if(0===o)return s(t,e);let n=((t.x-e.x)*(i.x-e.x)+(t.y-e.y)*(i.y-e.y))/o;return n=Math.max(0,Math.min(1,n)),s(t,{x:e.x+n*(i.x-e.x),y:e.y+n*(i.y-e.y)})})(t,e,i))};class Ht extends J{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 o,s,n,r,a=Infinity,d=Infinity,h=Infinity;const l=this.createClickBoundingBox.create(t),c=this.store.search(l);for(let l=0;l<c.length;l++){const u=c[l],p=u.geometry;if("Point"===p.type){if(u.properties.selectionPoint||u.properties.coordinatePoint||!e&&u.properties[i.MID_POINT])continue;const s=this.pixelDistance.measure(t,p.coordinates);u.properties[i.MID_POINT]&&s<this.pointerDistance&&s<h?(h=s,n=u):!u.properties[i.MID_POINT]&&s<this.pointerDistance&&s<a&&(a=s,o=u)}else if("LineString"===p.type){if(o)continue;for(let e=0;e<p.coordinates.length-1;e++){const i=p.coordinates[e],o=p.coordinates[e+1],n=Yt({x:t.containerX,y:t.containerY},this.project(i[0],i[1]),this.project(o[0],o[1]));n<this.pointerDistance&&n<d&&(d=n,s=u)}}else if("Polygon"===p.type){if(o||s)continue;Xt([t.lng,t.lat],p.coordinates)&&(r=u)}}return{clickedFeature:o||s||r,clickedMidPoint:n}}}class qt extends J{constructor(t,e,i,o,s){super(t),this.config=void 0,this.featuresAtCursorEvent=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.draggedFeatureId=null,this.dragPosition=void 0,this.config=t,this.featuresAtCursorEvent=e,this.selectionPoints=i,this.midPoints=o,this.coordinatePoints=s}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.featuresAtCursorEvent.find(t,!0);return!(!i||i.id!==e)}drag(t,i){if(!this.draggedFeatureId)return;const o=this.store.getGeometryCopy(this.draggedFeatureId),s=[t.lng,t.lat];if("Polygon"===o.type||"LineString"===o.type){let n,r;if("Polygon"===o.type?(n=o.coordinates[0],r=n.length-1):(n=o.coordinates,r=n.length),!this.dragPosition)return!1;for(let t=0;t<r;t++){const e=n[t];let i,o;if("web-mercator"===this.config.projection){const t=b(this.dragPosition[0],this.dragPosition[1]),n=b(s[0],s[1]),r=b(e[0],e[1]),a={x:t.x-n.x,y:t.y-n.y},d=r.x-a.x,h=r.y-a.y,{lng:l,lat:c}=k(d,h);i=l,o=c}else{const t=[this.dragPosition[0]-s[0],this.dragPosition[1]-s[1]];i=e[0]-t[0],o=e[1]-t[1]}if(i=E(i,this.config.coordinatePrecision),o=E(o,this.config.coordinatePrecision),i>180||i<-180||o>90||o<-90)return!1;n[t]=[i,o]}"Polygon"===o.type&&(n[n.length-1]=[n[0][0],n[0][1]]);const a=this.selectionPoints.getUpdated(n)||[],d=this.midPoints.getUpdated(n)||[],h=this.coordinatePoints.getUpdated(this.draggedFeatureId,n)||[];if(i&&!i({type:"Feature",id:this.draggedFeatureId,geometry:o,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:e.Provisional}).valid)return!1;this.store.updateGeometry([{id:this.draggedFeatureId,geometry:o},...a,...d,...h]),this.dragPosition=[t.lng,t.lat]}else"Point"===o.type&&(this.store.updateGeometry([{id:this.draggedFeatureId,geometry:{type:"Point",coordinates:s}}]),this.dragPosition=[t.lng,t.lat])}}class $t extends J{constructor(t,e,i,o,s,n,r){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.coordinateSnapping=void 0,this.lineSnapping=void 0,this.draggedCoordinate={id:null,index:-1},this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o,this.coordinatePoints=s,this.coordinateSnapping=n,this.lineSnapping=r}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 s=0;s<o.length;s++){const n=this.pixelDistance.measure(t,o[s]);if(n<this.pointerDistance&&n<i.dist){const t="Polygon"===e.type&&(s===o.length-1||0===s);i.dist=n,i.index=t?0:s,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}snapCoordinate(t,e,i){let o=[t.lng,t.lat];const s=t=>Boolean(t.properties&&t.properties.mode===i.properties.mode&&t.id!==this.draggedCoordinate.id);if(null!=e&&e.toLine){let e;e=this.lineSnapping.getSnappable(t,s).coordinate,e&&(o=e)}if(e.toCoordinate){let e;e=this.coordinateSnapping.getSnappable(t,s).coordinate,e&&(o=e)}if(null!=e&&e.toCustom){let s;s=e.toCustom(t,{currentCoordinate:this.draggedCoordinate.index,currentId:i.id,getCurrentGeometrySnapshot:i.id?()=>this.store.getGeometryCopy(i.id):()=>null,project:this.project,unproject:this.unproject}),s&&(o=s)}return o}drag(t,i,o,s){const n=this.draggedCoordinate.id;if(null===n)return!1;const r=this.draggedCoordinate.index,a=this.store.getGeometryCopy(n),d=this.store.getPropertiesCopy(n),h="LineString"===a.type?a.coordinates:a.coordinates[0],l="Polygon"===a.type&&(r===h.length-1||0===r),c={type:"Feature",id:n,geometry:a,properties:d},u=this.snapCoordinate(t,s,c);if(t.lng>180||t.lng<-180||t.lat>90||t.lat<-90)return!1;if(l){const t=h.length-1;h[0]=u,h[t]=u}else h[r]=u;const p=this.selectionPoints.getOneUpdated(r,u),g=p?[p]:[],y=this.midPoints.getUpdated(h)||[],f=this.coordinatePoints.getUpdated(n,h)||[];return!("Point"!==a.type&&!i&&j({type:"Feature",geometry:a,properties:{}})||o&&!o(c,{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:e.Provisional}).valid||(this.store.updateGeometry([{id:n,geometry:a},...g,...y,...f]),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 Jt(t){const e=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(t=>{const{x:e,y:i}=b(t[0],t[1]);return[e,i]});return"Polygon"===t.geometry.type?function(t){let e=0,i=0,o=0;const s=t.length;for(let n=0;n<s-1;n++){const[s,r]=t[n],[a,d]=t[n+1],h=s*d-a*r;e+=h,i+=(s+a)*h,o+=(r+d)*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 s=0;s<e;s++){const[e,n]=t[s];i+=e,o+=n}return{x:i/e,y:o/e}}(e)}class Qt extends J{constructor(t,e,i,o){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.lastBearing=void 0,this.selectedGeometry=void 0,this.selectedGeometryCentroid=void 0,this.selectedGeometryWebMercatorCentroid=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i,this.coordinatePoints=o}reset(){this.lastBearing=void 0,this.selectedGeometry=void 0,this.selectedGeometryWebMercatorCentroid=void 0,this.selectedGeometryCentroid=void 0}rotate(t,i,o){this.selectedGeometry||(this.selectedGeometry=this.store.getGeometryCopy(i));const s=this.selectedGeometry;if("Polygon"!==s.type&&"LineString"!==s.type)return;const n=[t.lng,t.lat];let r;const a={type:"Feature",geometry:s,properties:{}};if("web-mercator"===this.config.projection){this.selectedGeometryWebMercatorCentroid||(this.selectedGeometryWebMercatorCentroid=Jt(a));const e=b(t.lng,t.lat);if(r=rt(this.selectedGeometryWebMercatorCentroid,e),0===r)return;if(!this.lastBearing)return void(this.lastBearing=r);((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])=>b(t,e)),s=o.reduce((t,e)=>({x:t.x+e.x,y:t.y+e.y}),{x:0,y:0});s.x/=o.length,s.y/=o.length;const n=o.map(t=>({x:s.x+(t.x-s.x)*Math.cos(i)-(t.y-s.y)*Math.sin(i),y:s.y+(t.x-s.x)*Math.sin(i)+(t.y-s.y)*Math.cos(i)})).map(({x:t,y:e})=>[k(t,e).lng,k(t,e).lat]);"Polygon"===t.geometry.type?t.geometry.coordinates[0]=n:t.geometry.coordinates=n})(a,-(this.lastBearing-r))}else{if("globe"!==this.config.projection)throw new Error("Unsupported projection");if(this.selectedGeometryCentroid||(this.selectedGeometryCentroid=Zt({type:"Feature",geometry:s,properties:{}})),r=Tt(this.selectedGeometryCentroid,n),!this.lastBearing)return void(this.lastBearing=r+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=Tt(i,t)+e,s=function(t,e){t[0]+=t[0]-e[0]>180?-360:e[0]-t[0]>180?360:0;const i=I,o=e[1]*Math.PI/180,s=t[1]*Math.PI/180,n=s-o;let r=Math.abs(t[0]-e[0])*Math.PI/180;r>Math.PI&&(r-=2*Math.PI);const a=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(o/2+Math.PI/4)),d=Math.abs(a)>1e-11?n/a:Math.cos(o);return Math.sqrt(n*n+d*d*r*r)*i}(i,t),n=Vt(i,s,o);t[0]=n[0],t[1]=n[1]})}(a,-(this.lastBearing-(r+180)))}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 h=this.midPoints.getUpdated(d)||[],l=this.selectionPoints.getUpdated(d)||[],c=this.coordinatePoints.getUpdated(i,d)||[];if(o&&!o({id:i,type:"Feature",geometry:s,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:e.Provisional}))return!1;this.store.updateGeometry([{id:i,geometry:s},...l,...h,...c]),"web-mercator"===this.projection?this.lastBearing=r:"globe"===this.projection&&(this.lastBearing=r+180)}}class te extends J{constructor(t,e){super(t),this.config=void 0,this.dragCoordinateResizeBehavior=void 0,this.config=t,this.dragCoordinateResizeBehavior=e}scale(t,e,i){if(!this.dragCoordinateResizeBehavior.isDragging()){const i=this.dragCoordinateResizeBehavior.getDraggableIndex(t,e);this.dragCoordinateResizeBehavior.startDragging(e,i)}this.dragCoordinateResizeBehavior.drag(t,"center-fixed",i)}reset(){this.dragCoordinateResizeBehavior.stopDragging()}}class ee extends J{constructor(t,e,i,o,s){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=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,this.coordinatePoints=s}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 s=0;s<o.length;s++){const n=this.pixelDistance.measure(t,o[s]);if(n<this.pointerDistance&&n<i.dist){const t="Polygon"===e.type&&(s===o.length-1||0===s);i.dist=n,i.index=t?0:s,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:s,selectedCoordinate:n}=e,r=Jt(i);if(!r)return null;const a=b(n[0],n[1]),{closestBBoxIndex:d}=this.getIndexesWebMercator(o,a),h=b(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:d,updatedCoords:s,webMercatorCursor:h,webMercatorSelected:a,webMercatorOrigin:r}),s}centerFixedWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{feature:i,boundingBox:o,updatedCoords:s,selectedCoordinate:n}=e,r=Jt(i);if(!r)return null;const a=b(n[0],n[1]),{closestBBoxIndex:d}=this.getIndexesWebMercator(o,a),h=b(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:d,updatedCoords:s,webMercatorCursor:h,webMercatorSelected:a,webMercatorOrigin:r}),s}scaleFixedWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:i,webMercatorCursor:o,updatedCoords:s}){if(!this.isValidDragWebMercator(t,e.x-o.x,e.y-o.y))return null;let n=Y(e,o)/Y(e,i);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:s}=e,n=b(s[0],s[1]),{oppositeBboxIndex:r,closestBBoxIndex:a}=this.getIndexesWebMercator(i,n),d={x:i[r][0],y:i[r][1]},h=b(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:a,updatedCoords:o,webMercatorCursor:h,webMercatorSelected:n,webMercatorOrigin:d}),o}oppositeWebMercatorDrag(t){const e=this.getSelectedFeatureDataWebMercator();if(!e)return null;const{boundingBox:i,updatedCoords:o,selectedCoordinate:s}=e,n=b(s[0],s[1]),{oppositeBboxIndex:r,closestBBoxIndex:a}=this.getIndexesWebMercator(i,n),d={x:i[r][0],y:i[r][1]},h=b(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:a,updatedCoords:o,webMercatorCursor:h,webMercatorSelected:n,webMercatorOrigin:d}),o}scaleWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:i,webMercatorCursor:o,updatedCoords:s}){const n=e.x-o.x,r=e.y-o.y;if(!this.isValidDragWebMercator(t,n,r))return null;let a=1;0!==n&&1!==t&&5!==t&&(a=1-(e.x-i.x-n)/n);let d=1;return 0!==r&&3!==t&&7!==t&&(d=1-(e.y-i.y-r)/r),this.validateScale(a,d)?(a<0&&(a=this.minimumScale),d<0&&(d=this.minimumScale),this.performWebMercatorScale(s,e.x,e.y,a,d),s):null}getFeature(t){if(null===this.draggedCoordinate.id)return null;const e=this.store.getGeometryCopy(t);return"Polygon"!==e.type&&"LineString"!==e.type?null:{id:t,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,s){t.forEach(t=>{const{x:n,y:r}=b(t[0],t[1]),a=e+(n-e)*o,d=i+(r-i)*s,{lng:h,lat:l}=k(a,d);t[0]=h,t[1]=l})}getBBoxWebMercator(t){const e=[Infinity,Infinity,-Infinity,-Infinity];(t=t.map(t=>{const{x:e,y:i}=b(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,s,n]=e;return[[i,n],[(i+s)/2,n],[s,n],[s,n+(o-n)/2],[s,o],[(i+s)/2,o],[i,o],[i,n+(o-n)/2]]}getIndexesWebMercator(t,e){let i,o=Infinity;for(let s=0;s<t.length;s++){const n=Y({x:e.x,y:e.y},{x:t[s][0],y:t[s][1]});n<o&&(i=s,o=n)}if(void 0===i)throw new Error("No closest coordinate found");return{oppositeBboxIndex:this.boundingBoxMaps.opposite[i],closestBBoxIndex:i}}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 s=this.getFeature(this.draggedCoordinate.id);if(!s)return!1;let n=null;if("center"===i?n=this.centerWebMercatorDrag(t):"opposite"===i?n=this.oppositeWebMercatorDrag(t):"center-fixed"===i?n=this.centerFixedWebMercatorDrag(t):"opposite-fixed"===i&&(n=this.oppositeFixedWebMercatorDrag(t)),!n)return!1;for(let t=0;t<n.length;t++){const e=n[t];if(e[0]=E(e[0],this.coordinatePrecision),e[1]=E(e[1],this.coordinatePrecision),!B(e,this.coordinatePrecision))return!1}const r=this.midPoints.getUpdated(n)||[],a=this.selectionPoints.getUpdated(n)||[],d=this.coordinatePoints.getUpdated(s.id,n)||[],h={type:s.geometry.type,coordinates:"Polygon"===s.geometry.type?[n]:n};return!(o&&!o({id:this.draggedCoordinate.id,type:"Feature",geometry:h,properties:{}},{project:this.config.project,unproject:this.config.unproject,coordinatePrecision:this.config.coordinatePrecision,updateType:e.Provisional}).valid||(this.store.updateGeometry([{id:this.draggedCoordinate.id,geometry:h},...a,...r,...d]),0))}}const ie={deselect:"Escape",delete:"Delete",rotate:["Control","r"],scale:["Control","s"]},oe={pointerOver:"move",dragStart:"move",dragEnd:"move",insertMidpoint:"crosshair"};class se extends C{constructor(t){super(t,!0),this.mode="select",this.allowManualDeselection=!0,this.dragEventThrottle=5,this.dragEventCount=0,this.selected=[],this.flags={},this.keyEvents=ie,this.cursors=oe,this.validations={},this.selectionPoints=void 0,this.midPoints=void 0,this.coordinateSnap=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.coordinatePoints=void 0,this.lineSnap=void 0,this.updateOptions(t)}updateOptions(e){if(super.updateOptions(e),this.cursors=e&&e.cursors?t({},this.cursors,e.cursors):oe,null===(null==e?void 0:e.keyEvents)?this.keyEvents={deselect:null,delete:null,rotate:null,scale:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),void 0!==(null==e?void 0:e.dragEventThrottle)&&(this.dragEventThrottle=e.dragEventThrottle),void 0!==(null==e?void 0:e.allowManualDeselection)&&(this.allowManualDeselection=e.allowManualDeselection),null!=e&&e.flags){this.flags=t({},this.flags,e.flags),this.validations={};for(const t in this.flags){const e=this.flags[t].feature;e&&e.validation&&(this.validations[t]=e.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 et(t),this.clickBoundingBox=new tt(t),this.featuresAtMouseEvent=new Ht(t,this.clickBoundingBox,this.pixelDistance),this.selectionPoints=new zt(t),this.coordinatePoints=new Ot(t),this.midPoints=new Kt(t,this.selectionPoints,this.coordinatePoints),this.coordinateSnap=new it(t,this.pixelDistance,this.clickBoundingBox),this.lineSnap=new Pt(t,this.pixelDistance,this.clickBoundingBox),this.rotateFeature=new Qt(t,this.selectionPoints,this.midPoints,this.coordinatePoints),this.dragFeature=new qt(t,this.featuresAtMouseEvent,this.selectionPoints,this.midPoints,this.coordinatePoints),this.dragCoordinate=new $t(t,this.pixelDistance,this.selectionPoints,this.midPoints,this.coordinatePoints,this.coordinateSnap,this.lineSnap),this.dragCoordinateResizeFeature=new ee(t,this.pixelDistance,this.selectionPoints,this.midPoints,this.coordinatePoints),this.scaleFeature=new te(t,this.dragCoordinateResizeFeature)}deselectFeature(){this.deselect()}deselect(){const t=this.selected.filter(t=>this.store.has(t)).map(t=>({id:t,property:i.SELECTED,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 i,o=Infinity;if(this.selectionPoints.ids.forEach(e=>{const s=this.store.getGeometryCopy(e),n=this.pixelDistance.measure(t,s.coordinates);n<this.pointerDistance&&n<o&&(o=n,i=this.store.getPropertiesCopy(e))}),!i)return;const s=i.selectionPointFeatureId,n=i.index,r=this.store.getPropertiesCopy(s),a=this.flags[r.mode],d=this.validations[r.mode];if(!(a&&a.feature&&a.feature.coordinates&&a.feature.coordinates.deletable))return;const h=this.store.getGeometryCopy(s);let l;if("Polygon"===h.type){if(l=h.coordinates[0],l.length<=4)return}else if("LineString"===h.type&&(l=h.coordinates,l.length<=2))return;if(!l)return;if("Polygon"!==h.type||0!==n&&n!==l.length-1?l.splice(n,1):(l.shift(),l.pop(),l.push([l[0][0],l[0][1]])),d&&!d({id:s,type:"Feature",geometry:h,properties:r},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:e.Commit}).valid)return;const c=[...this.midPoints.ids,...this.selectionPoints.ids];this.store.delete(c),this.store.updateGeometry([{id:s,geometry:h}]),r.coordinatePointIds&&this.coordinatePoints.createOrUpdate(s),this.selectionPoints.create(l,h.type,s),a&&a.feature&&a.feature.coordinates&&a.feature.coordinates.midpoints&&this.midPoints.create(l,s,this.coordinatePrecision)}select(t,e=!0){if(this.selected[0]===t)return;const{mode:o}=this.store.getPropertiesCopy(t),s=this.flags[o];if(!s||!s.feature)return;const n=this.selected[0];if(n){if(n===t)return;this.deselect()}e&&this.setCursor(this.cursors.pointerOver),this.selected=[t],this.store.updateProperty([{id:t,property:i.SELECTED,value:!0}]),this.onSelect(t);const{type:r,coordinates:a}=this.store.getGeometryCopy(t);if("LineString"!==r&&"Polygon"!==r)return;const d="LineString"===r?a:a[0];d&&s&&s.feature.coordinates&&(this.selectionPoints.create(d,r,t),s.feature.coordinates.midpoints&&this.midPoints.create(d,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(this.selected[0],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&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t)?this.onRightClick(t):"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)&&this.onLeftClick(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;const t=this.selected[0];this.onDeselect(this.selected[0]),this.coordinatePoints.deletePointsByFeatureIds([t]),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.allowPointerEvent(this.pointerEvents.onDragStart,t))return;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||o.feature.coordinates&&"object"==typeof o.feature.coordinates.midpoints&&o.feature.coordinates.midpoints.draggable)))return;this.dragEventCount=0;const s=this.selected[0],n=this.dragCoordinate.getDraggableIndex(t,s);if(o&&o.feature&&o.feature.coordinates&&(o.feature.coordinates.draggable||o.feature.coordinates.resizable)&&-1!==n)return this.setCursor(this.cursors.dragStart),o.feature.coordinates.resizable?this.dragCoordinateResizeFeature.startDragging(s,n):this.dragCoordinate.startDragging(s,n),void e(!1);if(o&&o.feature&&o.feature.coordinates&&"object"==typeof o.feature.coordinates.midpoints&&o.feature.coordinates.midpoints.draggable){const{clickedMidPoint:i}=this.featuresAtMouseEvent.find(t,this.selected.length>0);if(this.selected.length&&i){this.midPoints.insert(s,i.id,this.coordinatePrecision);const o=this.dragCoordinate.getDraggableIndex(t,s);return this.dragCoordinate.startDragging(s,o),void e(!1)}}return o&&o.feature&&o.feature.draggable&&this.dragFeature.canDrag(t,s)?(this.setCursor(this.cursors.dragStart),this.dragFeature.startDragging(t,s),void e(!1)):void 0}onDrag(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDrag,t))return;const i=this.selected[0];if(!i)return;const o=this.store.getPropertiesCopy(i),s=this.flags[o.mode],n=!0===(s&&s.feature&&s.feature.selfIntersectable);if(this.dragEventCount++,this.dragEventCount%this.dragEventThrottle==0)return;const r=this.validations[o.mode];if(s&&s.feature&&s.feature.rotateable&&this.canRotate(t))return e(!1),void this.rotateFeature.rotate(t,i,r);if(s&&s.feature&&s.feature.scaleable&&this.canScale(t))return e(!1),void this.scaleFeature.scale(t,i,r);if(this.dragCoordinateResizeFeature.isDragging()&&s.feature&&s.feature.coordinates&&s.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,s.feature.coordinates.resizable,r)}if(this.dragCoordinate.isDragging()){var a;const e=null==(a=s.feature)||null==(a=a.coordinates)?void 0:a.snappable;let i={toCoordinate:!1};return!0===e?i={toCoordinate:!0}:"object"==typeof e&&(i=e),void this.dragCoordinate.drag(t,n,r,i)}this.dragFeature.isDragging()?this.dragFeature.drag(t,r):e(!0)}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&(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 s=this.store.getGeometryCopy(o);this.pixelDistance.measure(t,s.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 o=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 o.pointColor=this.getHexColorStylingValue(this.styles.selectionPointColor,o.pointColor,e),o.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectionPointOutlineColor,o.pointOutlineColor,e),o.pointWidth=this.getNumericStylingValue(this.styles.selectionPointWidth,o.pointWidth,e),o.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectionPointOutlineWidth,2,e),o.zIndex=30,o;if(e.properties.midPoint)return o.pointColor=this.getHexColorStylingValue(this.styles.midPointColor,o.pointColor,e),o.pointOutlineColor=this.getHexColorStylingValue(this.styles.midPointOutlineColor,o.pointOutlineColor,e),o.pointWidth=this.getNumericStylingValue(this.styles.midPointWidth,4,e),o.pointOutlineWidth=this.getNumericStylingValue(this.styles.midPointOutlineWidth,2,e),o.zIndex=50,o}else if(e.properties[i.SELECTED]){if("Polygon"===e.geometry.type)return o.polygonFillColor=this.getHexColorStylingValue(this.styles.selectedPolygonColor,o.polygonFillColor,e),o.polygonOutlineWidth=this.getNumericStylingValue(this.styles.selectedPolygonOutlineWidth,o.polygonOutlineWidth,e),o.polygonOutlineColor=this.getHexColorStylingValue(this.styles.selectedPolygonOutlineColor,o.polygonOutlineColor,e),o.polygonFillOpacity=this.getNumericStylingValue(this.styles.selectedPolygonFillOpacity,o.polygonFillOpacity,e),o.zIndex=l,o;if("LineString"===e.geometry.type)return o.lineStringColor=this.getHexColorStylingValue(this.styles.selectedLineStringColor,o.lineStringColor,e),o.lineStringWidth=this.getNumericStylingValue(this.styles.selectedLineStringWidth,o.lineStringWidth,e),o.zIndex=l,o;if("Point"===e.geometry.type)return o.pointWidth=this.getNumericStylingValue(this.styles.selectedPointWidth,o.pointWidth,e),o.pointColor=this.getHexColorStylingValue(this.styles.selectedPointColor,o.pointColor,e),o.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectedPointOutlineColor,o.pointOutlineColor,e),o.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectedPointOutlineWidth,o.pointOutlineWidth,e),o.zIndex=l,o}return o}}class ne extends v{constructor(...t){super(...t),this.type=f.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 re(t,e,i,o,s){for(;o>i;){if(o-i>600){const n=o-i+1,r=e-i+1,a=Math.log(n),d=.5*Math.exp(2*a/3),h=.5*Math.sqrt(a*d*(n-d)/n)*(r-n/2<0?-1:1);re(t,e,Math.max(i,Math.floor(e-r*d/n+h)),Math.min(o,Math.floor(e+(n-r)*d/n+h)),s)}const n=t[e];let r=i,a=o;for(ae(t,i,e),s(t[o],n)>0&&ae(t,i,o);r<a;){for(ae(t,r,a),r++,a--;s(t[r],n)<0;)r++;for(;s(t[a],n)>0;)a--}0===s(t[i],n)?ae(t,i,a):(a++,ae(t,a,o)),a<=e&&(i=a+1),e<=a&&(o=a-1)}}function ae(t,e,i){const o=t[e];t[e]=t[i],t[i]=o}function de(t,e){he(t,0,t.children.length,e,t)}function he(t,e,i,o,s){s||(s=ve([])),s.minX=Infinity,s.minY=Infinity,s.maxX=-Infinity,s.maxY=-Infinity;for(let n=e;n<i;n++){const e=t.children[n];le(s,t.leaf?o(e):e)}return s}function le(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 ce(t,e){return t.minX-e.minX}function ue(t,e){return t.minY-e.minY}function pe(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function ge(t){return t.maxX-t.minX+(t.maxY-t.minY)}function ye(t,e){const i=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),s=Math.min(t.maxX,e.maxX),n=Math.min(t.maxY,e.maxY);return Math.max(0,s-i)*Math.max(0,n-o)}function fe(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function me(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function ve(t){return{children:t,height:1,leaf:!0,minX:Infinity,minY:Infinity,maxX:-Infinity,maxY:-Infinity}}function Ce(t,e,i,o,s){const n=[e,i];for(;n.length;){if((i=n.pop())-(e=n.pop())<=o)continue;const r=e+Math.ceil((i-e)/o/2)*o;re(t,r,e,i,s),n.push(e,r,r,i)}}class Pe{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(!me(t,e))return i;const o=this.toBBox,s=[];for(;e;){for(let n=0;n<e.children.length;n++){const r=e.children[n],a=e.leaf?o(r):r;me(t,a)&&(e.leaf?i.push(r):fe(t,a)?this._all(r,i):s.push(r))}e=s.pop()}return i}collides(t){let e=this.data;if(me(t,e)){const i=[];for(;e;){for(let o=0;o<e.children.length;o++){const s=e.children[o],n=e.leaf?this.toBBox(s):s;if(me(t,n)){if(e.leaf||fe(t,n))return!0;i.push(s)}}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=ve([])}remove(t){let e=this.data;const i=this.toBBox(t),o=[],s=[];let n,r,a=!1;for(;e||o.length;){if(e||(e=o.pop(),r=o[o.length-1],n=s.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||!fe(e,i)?r?(n++,e=r.children[n],a=!1):e=null:(o.push(e),s.push(n),n=0,r=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 s=i-e+1;let n,r=this._maxEntries;if(s<=r)return n=ve(t.slice(e,i+1)),de(n,this.toBBox),n;o||(o=Math.ceil(Math.log(s)/Math.log(r)),r=Math.ceil(s/Math.pow(r,o-1))),n=ve([]),n.leaf=!1,n.height=o;const a=Math.ceil(s/r),d=a*Math.ceil(Math.sqrt(r));Ce(t,e,i,d,this.compareMinX);for(let s=e;s<=i;s+=d){const e=Math.min(s+d-1,i);Ce(t,s,e,a,this.compareMinY);for(let i=s;i<=e;i+=a){const s=Math.min(i+a-1,e);n.children.push(this._build(t,i,s,o-1))}}return de(n,this.toBBox),n}_chooseSubtree(t,e,i,o){for(;o.push(e),!e.leaf&&o.length-1!==i;){let i,o=Infinity,r=Infinity;for(let a=0;a<e.children.length;a++){const d=e.children[a],h=pe(d),l=(s=t,n=d,(Math.max(n.maxX,s.maxX)-Math.min(n.minX,s.minX))*(Math.max(n.maxY,s.maxY)-Math.min(n.minY,s.minY))-h);l<r?(r=l,o=h<o?h:o,i=d):l===r&&h<o&&(o=h,i=d)}e=i||e.children[0]}var s,n;return e}_insert(t,e,i){const o=i?t:this.toBBox(t),s=[],n=this._chooseSubtree(o,this.data,e,s);for(n.children.push(t),le(n,o);e>=0&&s[e].children.length>this._maxEntries;)this._split(s,e),e--;this._adjustParentBBoxes(o,s,e)}_split(t,e){const i=t[e],o=i.children.length,s=this._minEntries;this._chooseSplitAxis(i,s,o);const n=this._chooseSplitIndex(i,s,o),r=ve(i.children.splice(n,i.children.length-n));r.height=i.height,r.leaf=i.leaf,de(i,this.toBBox),de(r,this.toBBox),e?t[e-1].children.push(r):this._splitRoot(i,r)}_splitRoot(t,e){this.data=ve([t,e]),this.data.height=t.height+1,this.data.leaf=!1,de(this.data,this.toBBox)}_chooseSplitIndex(t,e,i){let o,s=Infinity,n=Infinity;for(let r=e;r<=i-e;r++){const e=he(t,0,r,this.toBBox),a=he(t,r,i,this.toBBox),d=ye(e,a),h=pe(e)+pe(a);d<s?(s=d,o=r,n=h<n?h:n):d===s&&h<n&&(n=h,o=r)}return o||i-e}_chooseSplitAxis(t,e,i){const o=t.leaf?this.compareMinX:ce,s=t.leaf?this.compareMinY:ue;this._allDistMargin(t,e,i,o)<this._allDistMargin(t,e,i,s)&&t.children.sort(o)}_allDistMargin(t,e,i,o){t.children.sort(o);const s=this.toBBox,n=he(t,0,e,s),r=he(t,i-e,i,s);let a=ge(n)+ge(r);for(let o=e;o<i-e;o++){const e=t.children[o];le(n,t.leaf?s(e):e),a+=ge(n)}for(let o=i-e-1;o>=e;o--){const e=t.children[o];le(r,t.leaf?s(e):e),a+=ge(r)}return a}_adjustParentBBoxes(t,e,i){for(let o=i;o>=0;o--)le(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():de(t[i],this.toBBox)}}class Ie{constructor(t){this.tree=void 0,this.idToNode=void 0,this.nodeToId=void 0,this.tree=new Pe(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 s=Math.min(...i),n=Math.max(...i);return{minX:Math.min(...e),minY:s,maxX:Math.max(...e),maxY:n}}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 xe={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 Ie,this.tracked=!t||!1!==t.tracked,this.idStrategy=t&&t.idStrategy?t.idStrategy:xe}clone(t){return JSON.parse(JSON.stringify(t))}getId(){return this.idStrategy.getId()}has(t){return Boolean(this.store[t])}load(t,e,i,o){if(0===t.length)return[];let s=this.clone(t);const n=[],r=[];return s=s.filter(t=>{null==t.id&&(t.id=this.idStrategy.getId());const o=t.id;if(e){const i=e(t);if(!i.valid)return r.push({id:o,valid:!1,reason:i.reason}),!1}if(this.tracked){if(t.properties.createdAt){if(!p(t.properties.createdAt))return r.push({id:t.id,valid:!1,reason:"createdAt is not a valid numeric timestamp"}),!1}else t.properties.createdAt=+new Date;if(t.properties.updatedAt){if(!p(t.properties.updatedAt))return r.push({id:t.id,valid:!1,reason:"updatedAt is not a valid numeric timestamp"}),!1}else t.properties.updatedAt=+new Date}return this.has(o)?(r.push({id:o,valid:!1,reason:`Feature already exists with this id: ${o}`}),!1):(this.store[o]=t,n.push(o),i&&i(t),r.push({id:o,valid:!0}),!0)}),this.spatialIndex.load(s),n.length>0&&this._onChange(n,"create",o),r}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,o)=>{t(e,i,o)}}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,e){const i=[];t.forEach(({id:t,property:e,value:o})=>{const s=this.store[t];if(!s)throw new Error(`No feature with this (${t}), can not update geometry`);i.push(t),void 0===o?delete s.properties[e]:s.properties[e]=o,this.tracked&&(s.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(i,"update",e)}updateGeometry(t,e){const i=[];t.forEach(({id:t,geometry:e})=>{i.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(e),this.spatialIndex.update(o),this.tracked&&(o.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(i,"update",e)}create(e,i){const o=[];return e.forEach(({geometry:e,properties:i})=>{let s,n=t({},i);this.tracked&&(s=+new Date,i?(n.createdAt="number"==typeof i.createdAt?i.createdAt:s,n.updatedAt="number"==typeof i.updatedAt?i.updatedAt:s):n={createdAt:s,updatedAt:s});const r=this.getId(),a={id:r,type:"Feature",geometry:e,properties:n};this.store[r]=a,this.spatialIndex.insert(a),o.push(r)}),this._onChange&&this._onChange([...o],"create",i),o}delete(t,e){t.forEach(t=>{if(!this.store[t])throw new Error(`No feature with id ${t}, can not delete`);delete this.store[t],this.spatialIndex.remove(t)}),this._onChange&&this._onChange([...t],"delete",e)}copy(t){return this.clone(this.store[t])}copyAll(){return this.clone(Object.keys(this.store).map(t=>this.store[t]))}copyAllWhere(t){return this.clone(Object.keys(this.store).map(t=>this.store[t]).filter(e=>e.properties&&t(e.properties)))}clear(){this.store={},this.spatialIndex.clear()}size(){return Object.keys(this.store).length}}function Se(t){const e=t.coordinates;let i=0;if(e&&e.length>0){i+=Math.abs(we(e[0]));for(let t=1;t<e.length;t++)i-=Math.abs(we(e[t]))}return i}const Ee=20294876564838.72,Fe=Math.PI/180;function we(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]*Fe-t[o][0]*Fe)*Math.sin(t[o+1===e?0:o+1][1]*Fe),o++;return i*Ee}const De="Feature is smaller than the minimum area",be=(t,e)=>"Polygon"!==t.geometry.type?{valid:!1,reason:g}:Se(t.geometry)<e?{valid:!1,reason:De}:{valid:!0},ke=(t,e)=>"Polygon"!==t.geometry.type?{valid:!1,reason:g}:Se(t.geometry)>e?{valid:!1,reason:"Feature is larger than the maximum area"}:{valid:!0},Oe="Feature is not a Polygon or LineString",_e="Feature intersects itself",je=t=>"Polygon"!==t.geometry.type&&"LineString"!==t.geometry.type?{valid:!1,reason:Oe}:j(t)?{valid:!1,reason:_e}:{valid:!0};function We(t,e,i){const o=rt(t,e);let s=rt(e,i)-o;return s<0&&(s+=360),180-Math.abs(s-90-90)}const Be={cancel:"Escape",finish:"Enter"},Ge={start:"crosshair",close:"pointer"};class Ne extends v{constructor(t){super(t,!0),this.mode="angled-rectangle",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=Be,this.cursors=Ge,this.mouseMove=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents))}close(){if(void 0===this.currentId)return;this.store.updateProperty([{id:this.currentId,property:o,value:void 0}]);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 i=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),s=Math.max(1e-6,e);o=[i[0],[t.lng,t.lat],[t.lng,t.lat-s],i[0]]}else if(2===this.currentCoordinate){const e=i[0],s=i[1],n=Ut(e,s,this.coordinatePrecision,this.project,this.unproject),r=b(e[0],e[1]),a=b(n[0],n[1]),d=b(s[0],s[1]),h=b(t.lng,t.lat),l=Y(h,r)<Y(h,d),c=We(r,a,h),u=l?90-c:We(r,a,h)-90,p=Y(a,h),g=Math.cos(x(u))*p,y=rt(r,d)+("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"}(r,d,h)?-90:90),f=st(r,g,y),m=st(d,g,y),v=k(f.x,f.y),C=k(m.x,m.y);o=[i[0],i[1],[C.lng,C.lat],[v.lng,v.lat],i[0]]}o&&this.updatePolygonGeometry(this.currentId,o,e.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}).valid||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,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,[o]:!0}}]);this.currentId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const i=this.store.getGeometryCopy(this.currentId);if(ut([t.lng,t.lat],i.coordinates[0][0]))return;if(!this.updatePolygonGeometry(this.currentId,[i.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]],e.Commit))return;this.currentCoordinate++}else 2===this.currentCoordinate&&this.currentId&&this.close()}onKeyUp(t){if(t.key===this.keyEvents.cancel)this.cleanUp();else if(t.key===this.keyEvents.finish){if(this.currentCoordinate<2)return void this.cleanUp();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=l),i}validateFeature(t){return this.validateModeFeature(t,t=>R(t,this.coordinatePrecision))}}function Le(t,e,i){return(e.x-t.x)*(i.y-t.y)-(e.y-t.y)*(i.x-t.x)<=0}const Te={cancel:"Escape",finish:"Enter"},Ve={start:"crosshair",close:"pointer"};class Ue extends v{constructor(t){super(t,!0),this.mode="sector",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=Te,this.direction=void 0,this.arcPoints=64,this.cursors=Ve,this.mouseMove=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.arcPoints&&(this.arcPoints=e.arcPoints)}close(){if(void 0===this.currentId)return;const t=H(this.store.getGeometryCopy(this.currentId));t&&this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}]);const e=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.direction=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(e,{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 i=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),s=Math.max(1e-6,e);o=[i[0],[t.lng,t.lat],[t.lng,t.lat-s],i[0]]}else if(2===this.currentCoordinate){const e=i[0],s=i[1],n=[t.lng,t.lat],r=b(e[0],e[1]),a=b(s[0],s[1]),d=b(n[0],n[1]);if(void 0===this.direction){const t=Le(r,a,d);this.direction=t?"clockwise":"anticlockwise"}const h=Y(r,a),l=rt(r,a),c=rt(r,d),u=this.arcPoints,p=[e],g=at(l),y=at(c);let f;"anticlockwise"===this.direction?(f=y-g,f<0&&(f+=360)):(f=g-y,f<0&&(f+=360));const m=("anticlockwise"===this.direction?1:-1)*f/u;p.push(s);for(let t=0;t<=u;t++){const e=st(r,h,g+t*m),{lng:i,lat:o}=k(e.x,e.y),s=[E(i,this.coordinatePrecision),E(o,this.coordinatePrecision)];s[0]!==p[p.length-1][0]&&s[1]!==p[p.length-1][1]&&p.push(s)}p.push(e),o=[...p]}o&&this.updatePolygonGeometry(this.currentId,o,e.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}).valid||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,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,[o]:!0}}]);this.currentId=e,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const i=this.store.getGeometryCopy(this.currentId);if(ut([t.lng,t.lat],i.coordinates[0][0]))return;if(!this.updatePolygonGeometry(this.currentId,[i.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]],e.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=l),i}validateFeature(t){return this.validateModeFeature(t,t=>R(t,this.coordinatePrecision))}}const Ae={cancel:"Escape",finish:"Enter"},Re={start:"crosshair",close:"pointer"};class Ke extends v{constructor(t){super(t,!0),this.mode="sensor",this.currentCoordinate=0,this.currentId=void 0,this.currentInitialArcId=void 0,this.currentStartingPointId=void 0,this.keyEvents=Ae,this.direction=void 0,this.arcPoints=64,this.cursors=Re,this.mouseMove=!1,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),null!=e&&e.cursors&&(this.cursors=t({},this.cursors,e.cursors)),null===(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:null!=e&&e.keyEvents&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),null!=e&&e.arcPoints&&(this.arcPoints=e.arcPoints)}close(){if(void 0===this.currentStartingPointId)return;const t=this.currentStartingPointId,e=this.currentInitialArcId,i=this.currentId;if(t&&this.store.delete([t]),e&&this.store.delete([e]),this.currentId){const t=H(this.store.getGeometryCopy(this.currentId));t&&this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.store.updateProperty([{id:this.currentId,property:o,value:void 0}])}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 i=this.store.getGeometryCopy(this.currentInitialArcId).coordinates,o=this.store.getGeometryCopy(this.currentStartingPointId).coordinates,s=i[0],n=[t.lng,t.lat],r=b(s[0],s[1]),a=b(n[0],n[1]),d=b(o[0],o[1]),h=Y(d,r);if(void 0===this.direction){const t=Le(d,r,a);this.direction=t?"clockwise":"anticlockwise"}const l=rt(d,r),c=rt(d,a),u=this.arcPoints,p=[s],g=at(l),y=at(c);let f;"anticlockwise"===this.direction?(f=y-g,f<0&&(f+=360)):(f=g-y,f<0&&(f+=360));const m=("anticlockwise"===this.direction?1:-1)*f/u;for(let t=0;t<=u;t++){const e=st(d,h,g+t*m),{lng:i,lat:o}=k(e.x,e.y),s=[E(i,this.coordinatePrecision),E(o,this.coordinatePrecision)];s[0]!==p[p.length-1][0]&&s[1]!==p[p.length-1][1]&&p.push(s)}this.updateLineStringGeometry(this.currentInitialArcId,p,e.Provisional)}else if(3===this.currentCoordinate){const i=this.store.getGeometryCopy(this.currentInitialArcId).coordinates;if(i.length<2)return;if(!this.direction)return;const s=this.store.getGeometryCopy(this.currentStartingPointId).coordinates,n=i[0],r=i[i.length-1],a=b(t.lng,t.lat),d=b(n[0],n[1]),h=b(r[0],r[1]),l=b(s[0],s[1]),c=Y(l,d),u=Y(l,a)<c?d:a,p=rt(l,a),g=rt(l,d),y=rt(l,h),f=at(g),m=at(y),v=at(p);if(this.notInSector({normalizedCursor:v,normalizedStart:f,normalizedEnd:m,direction:this.direction}))return;const C=this.getDeltaBearing(this.direction,f,m),P=this.arcPoints,I=("anticlockwise"===this.direction?1:-1)*C/P,x=Y(l,u),M=[];for(let t=0;t<=P;t++){const e=st(l,x,f+t*I),{lng:o,lat:s}=k(e.x,e.y),n=[E(o,this.coordinatePrecision),E(s,this.coordinatePrecision)];n[0]!==i[i.length-1][0]&&n[1]!==i[i.length-1][1]&&M.unshift(n)}i.push(...M),i.push(i[0]),this.currentId?this.updatePolygonGeometry(this.currentId,i,e.Provisional):[this.currentId]=this.store.create([{geometry:{type:"Polygon",coordinates:[i]},properties:{mode:this.mode,[o]:!0}}])}}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}).valid||(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}).valid||(this.store.updateGeometry([{id:t,geometry:o}]),0))}onClick(t){if("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,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=l):"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=l):"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 this.validateModeFeature(t,t=>R(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}}class ze{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}}var Xe={__proto__:null,GeoJSONStore:Me,TerraDrawBaseDrawMode:v,TerraDrawBaseSelectMode:C,TerraDrawBaseAdapter:class{constructor(t){this._nextKeyUpIsContextMenu=!1,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,e=!1){const i=this.getLngLatFromEvent(t);if(!i)return null;const{lng:o,lat:s}=i,{containerX:n,containerY:r}=this.getMapElementXYPosition(t),a=this.getButton(t),d=Array.from(this._heldKeys);return{lng:E(o,this._coordinatePrecision),lat:E(s,this._coordinatePrecision),containerX:n,containerY:r,button:a,heldKeys:d,isContextMenu:e}}register(t){this._currentModeCallbacks=t,this._listeners=this.getAdapterListeners(),this._listeners.forEach(t=>{t.register()})}getCoordinatePrecision(){return this._coordinatePrecision}getAdapterListeners(){return[new ze({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 ze({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},i={x:e.containerX,y:e.containerY},o=this._currentModeCallbacks.getState(),s=Y(t,i);let n=!1;if(n="drawing"===o?s<this._minPixelDragDistanceDrawing:"selecting"===o?s<this._minPixelDragDistanceSelecting:s<this._minPixelDragDistance,n)return;this._nextKeyUpIsContextMenu=!1,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 ze({name:"contextmenu",callback:t=>{this._currentModeCallbacks&&(t.preventDefault(),this._nextKeyUpIsContextMenu=!0)},register:t=>{this.getMapEventElement().addEventListener("contextmenu",t)},unregister:t=>{this.getMapEventElement().removeEventListener("contextmenu",t)}}),new ze({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._nextKeyUpIsContextMenu&&(e.isContextMenu=!0,this._nextKeyUpIsContextMenu=!1),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 ze({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 ze({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(),this._currentModeCallbacks=void 0}},getDefaultStyling:()=>({polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0}),SELECT_PROPERTIES:i};const Ye={ValidationReasonFeatureNotPoint:St,ValidationReasonFeatureInvalidCoordinates:Et,ValidationReasonFeatureInvalidCoordinatePrecision:Ft,ValidationReasonFeatureNotPolygon:g,ValidationReasonFeatureHasHoles:L,ValidationReasonFeatureLessThanFourCoordinates:T,ValidationReasonFeatureHasInvalidCoordinates:V,ValidationReasonFeatureCoordinatesNotClosed:U,ValidationReasonFeatureNotPolygonOrLineString:Oe,ValidationReasonFeatureSelfIntersects:_e,ValidationReasonFeatureLessThanMinSize:De,ValidationReasonModeMismatch:y};class He{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 ne;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},{}),s=Object.keys(o);if(0===s.length)throw new Error("No modes provided");s.forEach(t=>{if(o[t].type===f.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 n=t=>{const e=[],i=this._store.copyAll().filter(i=>!t.includes(i.id)||(e.push(i),!1));return{changed:e,unchanged:i}},r=(t,e)=>{this._enabled&&this._eventListeners.finish.forEach(i=>{i(t,e)})},a=(t,e,i)=>{if(!this._enabled)return;this._eventListeners.change.forEach(o=>{o(t,e,i)});const{changed:o,unchanged:s}=n(t);"create"===e?this._adapter.render({created:o,deletedIds:[],unchanged:s,updated:[]},this.getModeStyles()):"update"===e?this._adapter.render({created:[],deletedIds:[],unchanged:s,updated:o},this.getModeStyles()):"delete"===e?this._adapter.render({created:[],deletedIds:t,unchanged:s,updated:[]},this.getModeStyles()):"styling"===e&&this._adapter.render({created:[],deletedIds:[],unchanged:s,updated:[]},this.getModeStyles())},d=t=>{if(!this._enabled)return;this._eventListeners.select.forEach(e=>{e(t)});const{changed:e,unchanged:i}=n([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}=n([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:d,onDeselect:h,onFinish:r,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[i.SELECTED]?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},s){const r=s&&void 0!==s.pointerDistance?s.pointerDistance:30,d=!s||void 0===s.ignoreSelectFeatures||s.ignoreSelectFeatures,h=!(!s||void 0===s.ignoreCoordinatePoints)&&s.ignoreCoordinatePoints,l=!(!s||void 0===s.ignoreCurrentlyDrawing)&&s.ignoreCurrentlyDrawing,c=!(!s||void 0===s.ignoreClosingPoints)&&s.ignoreClosingPoints,u=this._adapter.unproject.bind(this._adapter),p=this._adapter.project.bind(this._adapter),g=p(t,e),y=Q({unproject:u,point:g,pointerDistance:r});return this._store.search(y).filter(u=>{if(d&&(u.properties[i.MID_POINT]||u.properties[i.SELECTION_POINT]))return!1;if(h&&u.properties[a])return!1;if(c&&u.properties[n])return!1;if(l&&u.properties[o])return!1;if("Point"===u.geometry.type){const t=u.geometry.coordinates,e=p(t[0],t[1]);return Y(g,e)<r}if("LineString"===u.geometry.type){const t=u.geometry.coordinates;for(let e=0;e<t.length-1;e++){const i=t[e],o=t[e+1];if(Yt(g,p(i[0],i[1]),p(o[0],o[1]))<r)return!0}return!1}if(Xt([t,e],u.geometry.coordinates))return!0;if(null!=s&&s.includePolygonsWithinPointerDistance){const t=u.geometry.coordinates;for(const e of t)for(let t=0;t<e.length-1;t++){const i=e[t],o=e[t+1],s=p(i[0],i[1]),n=p(o[0],o[1]);if(Yt(g,s,n)<r)return!0}}return!1})}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}updateModeOptions(t,e){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._modes[t].updateOptions(e)}getSnapshot(){return this._store.copyAll()}getSnapshotFeature(t){if(this._store.has(t))return this._store.copy(t)}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}getModeState(){return this._mode.state}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();const e=[];t.forEach(t=>{if(!this._store.has(t))throw new Error(`No feature with id ${t}, can not delete`);const o=this._store.copy(t);o.properties[i.SELECTED]&&this.deselectFeature(t),o.properties[h]&&e.push(...o.properties[h])}),this._store.delete([...t,...e],{origin:"api"})}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){return this.checkEnabled(),0===t.length?[]:this._store.load(t,t=>{if(u(t)){const e=t.properties.mode,i=this._modes[e];if(!i)return{id:t.id,valid:!1,reason:`${e} mode is not in the list of instantiated modes`};const o=i.validateFeature.bind(i)(t);return{id:t.id,valid:o.valid,reason:o.reason?o.reason:o.valid?void 0:"Feature is invalid"}}return{id:t.id,valid:!1,reason:"Mode property does not exist"}},t=>{if(u(t)){const e=this._modes[t.properties.mode];e&&e.afterFeatureAdded&&e.afterFeatureAdded(t)}},{origin:"api"})}start(){this._enabled||(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&&(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{He as TerraDraw,Ne as TerraDrawAngledRectangleMode,X as TerraDrawCircleMode,Xe as TerraDrawExtend,Z as TerraDrawFreehandMode,Mt as TerraDrawLineStringMode,bt as TerraDrawPointMode,Wt as TerraDrawPolygonMode,Nt as TerraDrawRectangleMode,Lt as TerraDrawRenderMode,Ue as TerraDrawSectorMode,se as TerraDrawSelectMode,Ke as TerraDrawSensorMode,ke as ValidateMaxAreaSquareMeters,be as ValidateMinAreaSquareMeters,je as ValidateNotSelfIntersecting,Ye as ValidationReasons};
|
|
2
2
|
//# sourceMappingURL=terra-draw.modern.js.map
|