function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e"number"==typeof t&&t>=0&&Number.isFinite(t),C=t=>"boolean"==typeof t,P=t=>null===t,v=t=>"object"==typeof t&&null!==t,I=t=>"string"==typeof t&&t.length>0,S=t=>"click-move"===t||"click-drag"===t||"click-move-or-drag"===t;var F;!function(t){t.Drawing="drawing",t.Select="select",t.Static="static",t.Render="render"}(F||(F={}));const O={rightClick:!0,contextMenu:!1,leftClick:!0,onDragStart:!0,onDrag:!0,onDragEnd:!0};class x{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(e,i=!1){this._state="unregistered",this._styles={},this.pointerEvents=O,this.behaviors=[],this.validate=void 0,this.pointerDistance=40,this.coordinatePrecision=void 0,this.undoRedoMaxStackSize=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.isInitialUpdate=!1,this.type=F.Drawing,this.mode="base",i?this.isInitialUpdate=!0:this.updateOptions(t({},e))}updateOptions(e){v(null==e?void 0:e.styles)&&(this.styles=t({},this._styles,e.styles)),f(null==e?void 0:e.pointerDistance)&&(this.pointerDistance=e.pointerDistance),"function"==typeof(null==e?void 0:e.validation)&&(this.validate=e.validation),I(null==e?void 0:e.projection)&&(this.projection=e.projection),v(null==e?void 0:e.pointerEvents)&&(this.pointerEvents=e.pointerEvents),I(null==e?void 0:e.modeName)&&!0===this.isInitialUpdate&&(this.mode=e.modeName),this.isInitialUpdate=!1}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.undoRedoMaxStackSize=t.undoRedoMaxStackSize,this.registerBehaviors({mode:t.mode,store:this.store,project:this.project,unproject:this.unproject,pointerDistance:this.pointerDistance,coordinatePrecision:t.coordinatePrecision,projection:this.projection,undoRedoMaxStackSize:t.undoRedoMaxStackSize})}validateFeature(t){return this.performFeatureValidation(t)}afterFeatureAdded(t){}afterFeatureUpdated(t){}performFeatureValidation(t){if("unregistered"===this._state)throw new Error("Mode must be registered");const e=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(!e.valid)return e;if(this.validate){const i=this.validate(t,{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:s.Provisional});return{valid:e.valid&&i.valid,reason:i.reason}}return{valid:e.valid,reason:e.reason}}validateModeFeature(t,e){const i=this.performFeatureValidation(t);return i.valid?t.properties.mode!==this.mode?{valid:!1,reason:m}:e(t):{valid:!1,reason:i.reason}}onFinish(t,e){}onDeselect(t){}onSelect(t){}onKeyDown(t){}onKeyUp(t){}undo(){}clearHistory(){}undoSize(){return 0}redoSize(){return 0}redo(){}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)}getUrlStylingValue(t,e,i){return this.getStylingValue(t,e,i)}getMarkerAnchorStylingValue(t,e,i){return this.getStylingValue(t,e,i)}getDashArrayStylingValue(t,e,i){return this.getStylingValue(t,e,i)}getStylingValue(t,e,i){return void 0===t?e:"function"==typeof t?null!=(o=t(i))?o:e:t;var o}}class w extends x{constructor(...t){super(...t),this.type=F.Select}}const M="Enter",E="Escape",D="crosshair",k="pointer",N="grabbing",b="move",_="unset";function T(t,e){const i=t=>t*Math.PI/180,o=i(t[1]),n=i(t[0]),s=i(e[1]),r=s-o,a=i(e[0])-n,d=Math.sin(r/2)*Math.sin(r/2)+Math.cos(o)*Math.cos(s)*Math.sin(a/2)*Math.sin(a/2);return 2*Math.atan2(Math.sqrt(d),Math.sqrt(1-d))*6371e3/1e3}const R=6371008.8;function W(t){return t%360*Math.PI/180}function A(t){return t/6371.0088}function U(t){return t%(2*Math.PI)*180/Math.PI}function L(t,e=9){const i=Math.pow(10,e);return Math.round(t*i)/i}const B=57.29577951308232,V=.017453292519943295,z=6378137,H=(t,e)=>({x:0===t?0:t*V*z,y:0===e?0:Math.log(Math.tan(Math.PI/4+e*V/2))*z}),G=(t,e)=>({lng:0===t?0:B*(t/z),lat:0===e?0:(2*Math.atan(Math.exp(e/z))-Math.PI/2)*B});function j(t,e,i){const o=W(t[0]),n=W(t[1]),s=W(i),r=A(e),a=Math.asin(Math.sin(n)*Math.cos(r)+Math.cos(n)*Math.sin(r)*Math.cos(s));return[U(o+Math.atan2(Math.sin(s)*Math.sin(r)*Math.cos(n),Math.cos(r)-Math.sin(n)*Math.sin(a))),U(a)]}function K(t){const{center:e,radiusKilometers:i,coordinatePrecision:o}=t,n=t.steps?t.steps:64,s=[];for(let t=0;t0;function o(t){return t<0||t>1}function n(t,n,s,r){const a=e[t][n],d=e[t][n+1],h=e[s][r],l=e[s][r+1],u=function(t,e,i,o){if(X(t,i)||X(t,o)||X(e,i)||X(o,i))return null;const n=t[0],s=t[1],r=e[0],a=e[1],d=i[0],h=i[1],l=o[0],u=o[1],c=(n-r)*(h-u)-(s-a)*(d-l);return 0===c?null:[((n*a-s*r)*(d-l)-(n-r)*(d*u-h*l))/c,((n*a-s*r)*(h-u)-(s-a)*(d*u-h*l))/c]}(a,d,h,l);if(null===u)return;let c,p;c=d[0]!==a[0]?(u[0]-a[0])/(d[0]-a[0]):(u[1]-a[1])/(d[1]-a[1]),p=l[0]!==h[0]?(u[0]-h[0])/(l[0]-h[0]):(u[1]-h[1])/(l[1]-h[1]),o(c)||o(p)||(u.toString(),i.push(u))}}function X(t,e){return t[0]===e[0]&&t[1]===e[1]}function $(t,e){return Z(t[0])<=e&&Z(t[1])<=e}function q(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 Z(t){let e=1,i=0;for(;Math.round(t*e)/e!==t;)e*=10,i++;return i}const J="Feature has holes",Q="Feature has less than 4 coordinates",tt="Feature has invalid coordinates",et="Feature coordinates are not closed";function it(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:J};if(t.geometry.coordinates[0].length<4)return{valid:!1,reason:Q};for(let i=0;i({type:"Feature",geometry:{type:"Point",coordinates:e},properties:t({mode:this.mode,[i]:!0},o?o(n):{})}));return this.createFeatures(n)}updatePoint({featureId:t,coordinateMutations:e,propertyMutations:i,context:o}){return this.handleMutateFeature({type:"Point",featureId:t,coordinateMutations:e,propertyMutations:i,context:o})}updatePolygon({featureId:t,coordinateMutations:e,context:i,propertyMutations:o}){return this.handleMutateFeature({type:"Polygon",featureId:t,coordinateMutations:e,propertyMutations:o,context:i})}updateLineString({featureId:t,coordinateMutations:e,context:i,propertyMutations:o}){return this.handleMutateFeature({type:"LineString",featureId:t,coordinateMutations:e,propertyMutations:o,context:i})}deleteFeatureIfPresent(t){t&&this.store.has(t)&&this.store.delete([t])}deleteFeaturesIfPresent(t){if(0===t.length)return;const e=t.filter(t=>this.store.has(t));e.length&&this.store.delete(e)}setDeselected(t){const e=t.filter(t=>this.store.has(t)).map(t=>({featureId:t,properties:{[a.SELECTED]:!1}}));this.updateFeatureProperties(e)}setSelected(t){const{type:e}=this.store.getGeometryCopy(t),i={featureId:t,propertyMutations:{[a.SELECTED]:!0},context:{updateType:s.Commit}};"Polygon"===e?this.updatePolygon(i):"LineString"===e?this.updateLineString(i):"Point"===e&&this.updatePoint(i)}updateGuidancePoints(t){this.updateFeatureGeometries(t.map(({featureId:t,coordinate:e})=>({id:t,geometry:{type:"Point",coordinates:e}})))}handleCreateFeature({geometry:t,properties:e}){return this.createFeatureWithGeometry({geometry:t,properties:e})}handleMutateFeature({type:e,featureId:i,coordinateMutations:o,propertyMutations:n,context:r}){if(!this.mutateFeature({type:e,featureId:i,coordinateMutations:o,propertyMutations:n,context:r.updateType===s.Finish?t({},r,{correctRightHandRule:!0}):t({},r)}))return null;const a=this.buildFeatureWithGeometry(i);return r.updateType!==s.Finish||o||this.validateGeometryWithUpdateType({geometry:a.geometry,properties:a.properties,updateType:r.updateType})?a:null}mutateFeature({type:t,featureId:e,coordinateMutations:i,propertyMutations:o,context:n}){if(null==e)return!1;const s=this.store.getGeometryCopy(e),r=this.store.getPropertiesCopy(e);if(s.type!==t)throw new Error(`${t} geometries cannot be updated on features with ${s.type} geometries`);if(i){let t=this.applyCoordinateMutations(s,i);if(n.correctRightHandRule&&"Polygon"===t.type){const e=st(t);e&&(t=e)}if(!this.validateGeometryWithUpdateType({geometry:t,properties:r,updateType:n.updateType}))return!1;this.updateFeatureGeometries([{id:e,geometry:t}])}return o&&this.updateFeatureProperties([{featureId:e,properties:o}]),!0}applyCoordinateMutations(e,i){if(this.isReplaceMutation(i))return t({},e,{coordinates:i.coordinates});if("Point"===e.type)throw new Error("Coordinate mutations are not supported for Point geometries");const o="Polygon"===e.type,n=o?e.coordinates[0].slice():e.coordinates.slice(),s=n.length,r=t=>{const e=t<0?s+t:t;if(e<0||e>=s)throw new RangeError(`Index ${t} (normalized to ${e}) is out of bounds`);return e},a=new Array(s).fill(void 0),d=Array.from({length:s},()=>[]),h=Array.from({length:s},()=>[]),l=[];for(const e of i){if(e.type===rt||e.type===at){const t=e.index,i=t<0?s+t:t;if(i<0||i>s)throw new RangeError(`Index ${e.index} (normalized to ${i}) is out of bounds`);if(e.type===rt){if(i>=s)throw new RangeError(`INSERT_BEFORE index ${e.index} (normalized to ${i}) is out of bounds for length ${s}`);d[i].push(e)}else i===s?l.push(e):h[i].push(e);continue}const i=r(e.index);a[i]=t({},e,{index:i})}const u=[];for(let t=0;tObject.entries(e).map(([e,i])=>({id:t,property:e,value:i}))).flat();this.store.updateProperty(e)}}function ct(t,e){return t[0]===e[0]&&t[1]===e[1]}class pt extends nt{constructor(t){super(t)}getGeometryType(t){return this.store.getGeometryCopy(t).type}coordinateAtIndexIsIdentical({featureId:t,newCoordinate:e,index:i}){const o=this.store.getGeometryCopy(t);let n;if("Polygon"===o.type)n=o.coordinates[0][i];else if("LineString"===o.type)n=o.coordinates[i];else{if(0!==i)throw new Error("Point geometries only have one coordinate at index 0");n=o.coordinates}return ct(e,n)}getGeometry(t){return this.store.getGeometryCopy(t)}getCoordinates(t){const{type:e,coordinates:i}=this.store.getGeometryCopy(t);return"Polygon"===e?i[0]:i}getCoordinate(t,e){const i=this.getCoordinates(t),o=e<0?i.length+e:e;if(o<0||o>=i.length)throw new RangeError(`Index ${e} (normalized to ${o}) is out of bounds`);return i[o]}getProperties(t){return this.store.getPropertiesCopy(t)}hasFeature(t){return this.store.has(t)}getAllFeatureIdsWhere(t){return this.store.copyAllWhere(t).map(({id:t})=>t)}}function gt(t){return Array.isArray(t)&&t.length>0&&Array.isArray(t[0])&&Array.isArray(t[0][0])}const yt=t=>gt(t)?t[0].slice(0,-1):t,mt=t=>gt(t)?t[0]:t;class ft extends nt{constructor(t,e,i){super(t),this.readFeature=void 0,this.mutateFeature=void 0,this.readFeature=e,this.mutateFeature=i}setEnabled(t){const e=this.store.copyAllWhere(t=>t.mode===this.mode).filter(t=>"LineString"===t.geometry.type||"Polygon"===t.geometry.type);t?e.forEach(t=>{this.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}):this.deletePointsByFeatureIds(e.filter(t=>Boolean(t.properties[d.COORDINATE_POINT_IDS])).map(t=>t.id))}createOrUpdate({featureId:t,featureCoordinates:e}){if(!this.readFeature.hasFeature(t))return void this.deleteOrphanedPoints(t);const i=yt(e),o=this.readFeature.getProperties(t),n=o.coordinatePointIds;if(n)if(n&&n.every(t=>this.readFeature.hasFeature(t))){const e=o.coordinatePointIds,s=e.map(t=>this.readFeature.getGeometry(t).coordinates);if(e.length!==i.length){this.deleteCoordinatePoints(e);const n=this.createPoints(i,o.mode,t);this.setFeatureCoordinatePoints(t,n)}else{const t=[];i.forEach((i,o)=>{i[0]===s[o][0]&&i[1]===s[o][1]||t.push({featureId:e[o],coordinate:i})}),this.mutateFeature.updateGuidancePoints(t),this.syncCurrentlyDrawing(n,o)}}else{const e=n.filter(t=>this.readFeature.hasFeature(t));e.length&&this.deleteCoordinatePoints(e);const s=this.createPoints(i,o.mode,t);this.setFeatureCoordinatePoints(t,s)}else{const e=this.createPoints(i,o.mode,t);this.setFeatureCoordinatePoints(t,e)}}deletePointsByFeatureIds(t){for(const e of t)this.deleteIfPresent(e)}updateOneAtIndex(t,e,i){const o=this.readFeature.getProperties(t).coordinatePointIds;o&&0!==o.length&&void 0!==o[e]&&this.mutateFeature.updateGuidancePoints([{featureId:o[e],coordinate:i}])}updateAllInPlace({featureId:t,featureCoordinates:e}){const i=this.readFeature.getProperties(t);if(!i.coordinatePointIds)return;const o=yt(e);o.length===i.coordinatePointIds.length&&this.mutateFeature.updateGuidancePoints(i.coordinatePointIds.map((t,e)=>({featureId:t,coordinate:o[e]})))}createPoints(e,i,o){const n=!0===this.readFeature.getProperties(o)[d.CURRENTLY_DRAWING];return this.mutateFeature.createGuidancePoints({coordinates:e,type:d.COORDINATE_POINT,additionalProperties:e=>t({mode:i,[d.COORDINATE_POINT]:!0,[d.COORDINATE_POINT_FEATURE_ID]:o},n?{[d.CURRENTLY_DRAWING]:!0}:{},{index:e})})}syncCurrentlyDrawing(t,e){const i=!0===e[d.CURRENTLY_DRAWING];t.forEach(t=>{this.readFeature.hasFeature(t)&&this.mutateFeature.updatePoint({featureId:t,propertyMutations:{[d.CURRENTLY_DRAWING]:!!i||void 0},context:{updateType:s.Commit}})})}setFeatureCoordinatePoints(t,e,i=s.Commit){const o=this.readFeature.getGeometryType(t),n={featureId:t,propertyMutations:{[d.COORDINATE_POINT_IDS]:e},context:{updateType:i}};if("Polygon"===o)this.mutateFeature.updatePolygon(n);else{if("LineString"!==o)throw new Error("Unsupported geometry type for coordinate points");this.mutateFeature.updateLineString(n)}}deleteCoordinatePoints(t){this.mutateFeature.deleteFeaturesIfPresent(t)}deleteIfPresent(t){if(!this.readFeature.hasFeature(t))return;const e=this.readFeature.getProperties(t).coordinatePointIds;e&&(this.deleteCoordinatePoints(e),this.setFeatureCoordinatePoints(t,null))}deleteOrphanedPoints(t){const e=this.readFeature.getAllFeatureIdsWhere(e=>e[d.COORDINATE_POINT_FEATURE_ID]===t);this.mutateFeature.deleteFeaturesIfPresent(e)}}const Ct=(t,e)=>{const{x:i,y:o}=t,{x:n,y:s}=e,r=n-i,a=s-o;return Math.sqrt(a*a+r*r)};class Pt extends nt{constructor(t){super(t)}measure(t,e){const{x:i,y:o}=this.project(e[0],e[1]);return Ct({x:i,y:o},{x:t.containerX,y:t.containerY})}}function vt({unproject:t,point:e,pointerDistance:i}){const o=i/2,{x:n,y:s}=e;return{type:"Feature",properties:{},geometry:{type:"Polygon",coordinates:[[t(n-o,s-o),t(n+o,s-o),t(n+o,s+o),t(n-o,s+o),t(n-o,s-o)].map(t=>[t.lng,t.lat])]}}}class It extends nt{constructor(t){super(t)}create(t){const{containerX:e,containerY:i}=t;return vt({unproject:this.unproject,point:{x:e,y:i},pointerDistance:this.pointerDistance})}}class St extends nt{constructor(t,e,i){super(t),this.config=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappableCoordinateFirstClick(t){return this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode)).coordinate}getSnappableCoordinate(t,e){return this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e)).coordinate}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),n={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)i=e.geometry.coordinates;else{if("Point"!==e.geometry.type)return;var o,s,r,h,l,u;if(null!=(o=e.properties)&&o[d.EDITED]||null!=(s=e.properties)&&s[d.CLOSING_POINT]||null!=(r=e.properties)&&r[d.SNAPPING_POINT]||null!=(h=e.properties)&&h[d.COORDINATE_POINT]||null!=(l=e.properties)&&l[a.SELECTION_POINT]||null!=(u=e.properties)&&u[a.MID_POINT])return;i=[e.geometry.coordinates]}i.forEach((i,o)=>{const s=this.pixelDistance.measure(t,i);sI||Ot(n,w)>I?T(wt(w),wt(o))<=T(wt(w),wt(n))?[wt(o),!0,!1]:[wt(n),!1,!0]:[wt(w),!1,!1]}function Et(t,e,i){const o=e.x-t.x,n=e.y-t.y,s=Math.max(0,Math.min(1,((i.x-t.x)*o+(i.y-t.y)*n)/(o*o+n*n)));return{x:t.x+s*o,y:t.y+s*n}}class Dt extends nt{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?[L(e.coordinate[0],this.config.coordinatePrecision),L(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?[L(i.coordinate[0],this.config.coordinatePrecision),L(i.coordinate[1],this.config.coordinatePrecision)]:void 0},this.getSnappable=(t,e)=>{const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),n={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(void 0===e||t.id!==e)&&(!i||i(t));let r={coordinate:void 0,featureId:void 0,featureCoordinateIndex:void 0,minDistance:Infinity};if(!1!==(null==o?void 0:o.toCoordinate)){const e=this.coordinateSnapping.getSnappable(t,s);e.coordinate&&e.minDistancenull,project:this.project,unproject:this.unproject});e&&(n=e)}return n}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?(this.cancelled="drag"===this.drawType,this.cleanUp()):t.key===this.keyEvents.finish&&this.close()}onDragStart(t,e){"drawing"!==this.state&&this.allowPointerEvent(this.pointerEvents.onDragStart,t)&&this.dragDrawAllowed()&&(this.cancelled=!1,this.beginDrawing(t,"drag"),e(!1))}onDrag(t,e){this.allowPointerEvent(this.pointerEvents.onDrag,t)&&this.dragDrawAllowed()&&"drag"===this.drawType&&(this.cursorMovedAfterInitialCursorDown=!0,this.endPosition=[t.lng,t.lat],this.updateCircle(this.endPosition,s.Provisional))}onDragEnd(t,e){if(this.cancelled)return this.cancelled=!1,void e(!0);this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&this.dragDrawAllowed()&&"drag"===this.drawType&&(this.endPosition=[t.lng,t.lat],this.close(),e(!0))}cleanUp(){const t=this.currentCircleId;this.deleteSnappedPoint(),this.center=void 0,this.currentCircleId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted(),void 0!==t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t)}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 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.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.outlineOpacity,1,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=h,i):"Feature"===e.type&&e.properties.mode===this.mode&&"Point"===e.geometry.type&&e.properties[d.COORDINATE_POINT]?(i.pointWidth=this.getNumericStylingValue(this.styles.coordinatePointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.coordinatePointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.coordinatePointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.coordinatePointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.coordinatePointOutlineOpacity,1,e),i.zIndex=l,i):"Feature"===e.type&&e.properties.mode===this.mode&&"Point"===e.geometry.type&&e.properties[d.SNAPPING_POINT]?(i.pointWidth=this.getNumericStylingValue(this.styles.snappingPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.snappingPointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.snappingPointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.snappingPointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.snappingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.snappingPointOutlineOpacity,1,e),i.zIndex=u,i):i}validateFeature(t){return this.validateModeFeature(t,t=>ot(t,this.coordinatePrecision))}updateCircle(t,i){if(void 0===this.currentCircleId||void 0===this.center)return;const o=i===s.Finish;let n,r;if(this.cursorMovedAfterInitialCursorDown)if(r=T(this.center,t),"web-mercator"===this.projection){const e=function(t,e){const i=1e3*T(t,e);if(0===i)return 1;const{x:o,y:n}=H(t[0],t[1]),{x:s,y:r}=H(e[0],e[1]);return Math.sqrt(Math.pow(s-o,2)+Math.pow(r-n,2))/i}(this.center,t);n=function(t){const{center:e,radiusKilometers:i,coordinatePrecision:o}=t,n=t.steps?t.steps:64,s=1e3*i,[r,a]=e,{x:d,y:h}=H(r,a),l=[];for(let t=0;tnull,project:this.project,unproject:this.unproject});e&&(n=e)}return n}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?(this.cancelled="drag"===this.drawType,this.cleanUp()):t.key===this.keyEvents.finish&&this.close()}onDragStart(t,e){if("drawing"!==this.state&&this.allowPointerEvent(this.pointerEvents.onDragStart,t)&&this.dragDrawAllowed()){if(!this.beginDrawing(t,"drag"))return;e(!1)}}onDrag(t,e){this.allowPointerEvent(this.pointerEvents.onDrag,t)&&this.dragDrawAllowed()&&"drag"===this.drawType&&(this.cursorMovedAfterInitialCursorDown=!0,this.endPosition=[t.lng,t.lat],this.updateEllipse(this.endPosition,s.Provisional))}onDragEnd(t,e){if(this.cancelled)return this.cancelled=!1,void e(!0);this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&this.dragDrawAllowed()&&"drag"===this.drawType&&(this.endPosition=[t.lng,t.lat],this.close(),e(!0))}cleanUp(){const t=this.currentEllipseId;this.deleteSnappedPoint(),this.center=void 0,this.currentEllipseId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted(),void 0!==t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t)}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});if("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.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.outlineOpacity,1,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=h;else{if("Feature"===e.type&&e.properties.mode===this.mode&&"Point"===e.geometry.type&&e.properties[d.COORDINATE_POINT])return i.pointWidth=this.getNumericStylingValue(this.styles.coordinatePointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.coordinatePointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.coordinatePointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.coordinatePointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.coordinatePointOutlineOpacity,1,e),i.zIndex=l,i;if("Feature"===e.type&&e.properties.mode===this.mode&&"Point"===e.geometry.type&&e.properties[d.SNAPPING_POINT])return i.pointWidth=this.getNumericStylingValue(this.styles.snappingPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.snappingPointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.snappingPointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.snappingPointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.snappingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.snappingPointOutlineOpacity,1,e),i.zIndex=u,i}return i}validateFeature(t){return this.validateModeFeature(t,t=>ot(t,this.coordinatePrecision))}updateEllipse(t,i){if(void 0===this.currentEllipseId||void 0===this.center)return;const o=i===s.Finish;let n,r,a;if(this.cursorMovedAfterInitialCursorDown)if("web-mercator"===this.projection){const e=H(this.center[0],this.center[1]),i=H(t[0],t[1]);r=Math.abs(i.x-e.x)/1e3,a=Math.abs(i.y-e.y)/1e3,n=Ut({center:this.center,xRadiusKilometers:r,yRadiusKilometers:a,coordinatePrecision:this.coordinatePrecision,steps:this.segments})}else{if("globe"!==this.projection)throw new Error("Invalid projection");r=T(this.center,[t[0],this.center[1]]),a=T(this.center,[this.center[0],t[1]]),n=At({center:this.center,xRadiusKilometers:r,yRadiusKilometers:a,coordinatePrecision:this.coordinatePrecision,steps:this.segments})}const h={};n&&void 0!==r&&void 0!==a&&(h.xRadiusKilometers=r,h.yRadiusKilometers=a),o&&(h[d.CURRENTLY_DRAWING]=void 0);const l=this.mutateFeature.updatePolygon({featureId:this.currentEllipseId,coordinateMutations:n?{type:lt,coordinates:n.geometry.coordinates}:void 0,propertyMutations:h,context:o?{updateType:i,action:e}:{updateType:i}});return l&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentEllipseId,featureCoordinates:l.geometry.coordinates}),l}afterFeatureUpdated(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.currentEllipseId===t.id&&(this.cursorMovedAfterInitialCursorDown=!1,this.center=void 0,this.currentEllipseId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted())}afterFeatureAdded(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}registerBehaviors(t){this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature),this.pixelDistance=new Pt(t),this.clickBoundingBox=new It(t),this.coordinateSnapping=new St(t,this.pixelDistance,this.clickBoundingBox),this.lineSnapping=new Dt(t,this.pixelDistance,this.clickBoundingBox),this.featureSnapping=new kt(this.coordinateSnapping,this.lineSnapping)}}const zt={cancel:E,finish:M},Ht={start:D,close:k};class Gt extends x{constructor(t){super(t,!0),this.mode="freehand",this.canClose=!1,this.currentId=void 0,this.closingPointId=void 0,this.minDistance=20,this.keyEvents=zt,this.cursors=Ht,this.preventPointsNearClose=!0,this.autoClose=!1,this.autoCloseTimeout=500,this.hasLeftStartingPoint=!1,this.preventNewFeature=!1,this.drawInteraction="click-move",this.drawType=void 0,this.smoothing=0,this.showCoordinatePoints=!1,this.coordinatePoints=void 0,this.mutateFeature=void 0,this.readFeature=void 0,this.updateOptions(t)}updateOptions(e){var i;if(super.updateOptions(e),C(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints)),f(null==e?void 0:e.minDistance)&&(this.minDistance=e.minDistance),f(null==e?void 0:e.smoothing)){const t=.999;this.smoothing=Math.min(Math.max(e.smoothing,0),t)}C(null==e?void 0:e.preventPointsNearClose)&&(this.preventPointsNearClose=e.preventPointsNearClose),C(null==e?void 0:e.autoClose)&&(this.autoClose=e.autoClose),f(null==e?void 0:e.autoCloseTimeout)&&(this.autoCloseTimeout=e.autoCloseTimeout),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),S(null==e?void 0:e.drawInteraction)&&(this.drawInteraction=e.drawInteraction)}moveDrawAllowed(){return"click-move"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}dragDrawAllowed(){return"click-drag"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}beginDrawing(t,e="click"){const i=this.mutateFeature.createPolygon({coordinates:[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]],properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});this.currentId=i.id,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:i.id,featureCoordinates:i.geometry.coordinates}),this.drawType=e,this.closingPointId=this.mutateFeature.createGuidancePoint({coordinate:[t.lng,t.lat],type:d.CLOSING_POINT}),this.canClose=!0,"drawing"!==this.state&&this.setDrawing()}addCoordinate(t){if(void 0===this.currentId||!1===this.canClose)return void this.setCursor(this.cursors.start);const[e,i]=this.readFeature.getCoordinate(this.currentId,-2),{x:o,y:n}=this.project(e,i),r=Ct({x:o,y:n},{x:t.containerX,y:t.containerY}),[a,d]=this.readFeature.getCoordinate(this.currentId,0),{x:h,y:l}=this.project(a,d);if(Ct({x:h,y:l},{x:t.containerX,y:t.containerY}){this.preventNewFeature=!1},this.autoCloseTimeout),void this.close();if(this.setCursor(this.cursors.close),this.preventPointsNearClose)return}else this.hasLeftStartingPoint=!0,this.setCursor(this.cursors.start);if(r{this.preventNewFeature=!1},this.autoCloseTimeout),this.close(),e(!0))}cleanUp(){const t=this.currentId,e=this.closingPointId;this.closingPointId=void 0,this.currentId=void 0,this.canClose=!1,this.hasLeftStartingPoint=!1,this.drawType=void 0,"drawing"===this.state&&this.setStarted(),void 0!==t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t),this.mutateFeature.deleteFeatureIfPresent(e)}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 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.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.outlineOpacity,1,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&&e.properties.mode===this.mode&&"Point"===e.geometry.type&&e.properties[d.COORDINATE_POINT]?(i.pointWidth=this.getNumericStylingValue(this.styles.coordinatePointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.coordinatePointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.coordinatePointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.coordinatePointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.coordinatePointOutlineOpacity,1,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.pointOpacity=this.getNumericStylingValue(this.styles.closingPointOpacity,void 0===i.pointOpacity?1:i.pointOpacity,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.closingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.closingPointOutlineWidth,2,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.closingPointOutlineOpacity,void 0===i.pointOutlineOpacity?1:i.pointOutlineOpacity,e),i.zIndex=50,i):i}validateFeature(t){return this.validateModeFeature(t,t=>it(t,this.coordinatePrecision))}afterFeatureUpdated(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.currentId===t.id&&(this.mutateFeature.deleteFeatureIfPresent(this.closingPointId),this.canClose=!1,this.currentId=void 0,this.closingPointId=void 0,this.hasLeftStartingPoint=!1)}afterFeatureAdded(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}registerBehaviors(t){this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature)}}function jt(t,e){const i=W(t[0]),o=W(e[0]),n=W(t[1]),s=W(e[1]),r=Math.sin(o-i)*Math.cos(s),a=Math.cos(n)*Math.sin(s)-Math.sin(n)*Math.cos(s)*Math.cos(o-i);return U(Math.atan2(r,a))}function Kt({x:t,y:e},{x:i,y:o}){const n=i-t,s=o-e;if(0===n&&0===s)return 0;let r=Math.atan2(s,n);return r*=180/Math.PI,r>180?r-=360:r<-180&&(r+=360),r}function Yt(t){return(t+360)%360}function Xt(t,e,i){const o=[],n=t.length;let s,r,a,d=0;for(let n=0;n=d&&n===t.length-1);n++){if(d>e&&0===o.length){if(s=e-d,!s)return o.push(t[n]),o;r=jt(t[n],t[n-1])-180,a=Tt(t[n],s,r),o.push(a)}if(d>=i)return s=i-d,s?(r=jt(t[n],t[n-1])-180,a=Tt(t[n],s,r),o.push(a),o):(o.push(t[n]),o);if(d>=e&&o.push(t[n]),n===t.length-1)return o;d+=T(t[n],t[n+1])}if(d[L(t[0],this.config.coordinatePrecision),L(t[1],this.config.coordinatePrecision)])}}function Jt(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;ithis.cloneRecursively(t);const e=null==t?void 0:t.maxStackSize;this.maxStackSize=void 0!==e&&Number.isFinite(e)?Math.max(0,Math.floor(e)):Number.POSITIVE_INFINITY}setMaxStackSize(t){Number.isFinite(t)?(this.maxStackSize=Math.max(0,Math.floor(t)),this.trimHistoryToMax(this.undoHistory),this.trimHistoryToMax(this.redoHistory)):this.maxStackSize=Number.POSITIVE_INFINITY}trimHistoryToMax(t){if(Number.isFinite(this.maxStackSize))for(;t.length>this.maxStackSize;)t.shift()}pushUndoEntry(t){0!==this.maxStackSize&&(this.undoHistory.push(t),this.trimHistoryToMax(this.undoHistory))}pushRedoEntry(t){0!==this.maxStackSize&&(this.redoHistory.push(t),this.trimHistoryToMax(this.redoHistory))}cloneRecursively(e){return Array.isArray(e)?e.map(t=>this.cloneRecursively(t)):null!==e&&"object"==typeof e?t({},e):e}cloneCoordinates(t){return this.cloneCoordinatesFunction(t)}cloneEntry(t){return{featureCoordinates:this.cloneCoordinates(t.featureCoordinates),currentCoordinate:t.currentCoordinate}}clear(){this.undoHistory=[],this.redoHistory=[]}undoSize(){return this.undoHistory.length}redoSize(){return this.redoHistory.length}recordSnapshot(t){this.pushUndoEntry(this.cloneEntry(t)),this.redoHistory=[]}beginUndo(){const t=this.undoHistory.pop();if(!t)return;const e=this.cloneEntry(t);this.pushRedoEntry(e);const i=this.undoHistory[this.undoHistory.length-1];return{undoneEntry:e,previousEntry:i?this.cloneEntry(i):void 0}}takeRedo(){const t=this.redoHistory.pop();if(t)return this.cloneEntry(t)}commitRedo(t){this.pushUndoEntry(this.cloneEntry(t))}}class ee extends nt{constructor(t){super(t),this.config=void 0,this.EPSILON=1e-9,this.DEFAULT_DEGREE=90,this.config=t}getSnappableCoordinate(t,e,i,o={}){if(i<2)return;const n="Polygon"===e.type?e.coordinates[0]:e.coordinates,s=n[i-2],r=n[i-1];if(!s||!r)return;const a=this.getDegree(o.degree),d=[t.lng,t.lat],h=!0===o.backTracking;let l="globe"===this.projection?this.snapOnGlobe(s,r,d,a,h):this.snapOnWebMercator(s,r,d,a,h);if("Polygon"===e.type&&i>=3){const t=this.getClosingCandidate(s,r,n[0],d,a,h);t&&this.pixelDistance(d,t)0&&t<=180&&360/t<=1e4?t:this.DEFAULT_DEGREE}offsets(t,e){const i=[];for(let o=0;o<360-this.EPSILON;o+=t){const t=(o%360+360)%360;!e&&Math.abs(t-180)e?this.distance(t,i)1&&(this.finishOnNthCoordinate=Math.floor(e.finishOnNthCoordinate)),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),v(null==e?void 0:e.snapping)&&(this.snapping=e.snapping),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),v(null==e?void 0:e.insertCoordinates)&&(this.insertCoordinates=e.insertCoordinates),C(null==e?void 0:e.editable)&&(this.editable=e.editable),void 0!==(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints))}shouldFinishOnCommit(t){return!!this.finishOnNthCoordinate&&Math.max(0,t.coordinates.length-1)>=this.finishOnNthCoordinate}updateSnappedCoordinate(t){var e;const i=this.snapCoordinate(t);return i&&!1!==(null==(e=this.snapping)?void 0:e.showSnappingPoint)?this.snappedPointId?this.mutateFeature.updateGuidancePoints([{featureId:this.snappedPointId,coordinate:i}]):this.snappedPointId=this.mutateFeature.createGuidancePoint({coordinate:i,type:d.SNAPPING_POINT}):this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),i&&(t.lng=i[0],t.lat=i[1]),i}close(){if(void 0===this.currentId)return;const t=this.mutateFeature.updateLineString({featureId:this.currentId,context:{updateType:s.Finish,action:e},coordinateMutations:[{type:ht,index:-1}],propertyMutations:{[d.CURRENTLY_DRAWING]:void 0}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates});const i=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.lastCommittedCoordinates=void 0,this.undoRedo.clear(),"drawing"===this.state&&this.setStarted(),this.closingPoints.delete(),this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.editedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.editedPointId),this.editedPointId=void 0,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0),this.onFinish(i,{mode:this.mode,action:e})}generateInsertCoordinates(t,e){if(!this.insertCoordinates||!this.lastCommittedCoordinates)throw new Error("Not able to insert coordinates");if("amount"!==this.insertCoordinates.strategy)throw new Error("Strategy does not exist");const i=T(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.mutateFeature.createLineString({coordinates:[t,t],properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});this.lastCommittedCoordinates=e.geometry.coordinates,this.currentId=e.id,this.currentCoordinate++,this.pushHistorySnapshot(this.currentId,this.currentCoordinate),this.setDrawing(),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:e.geometry.coordinates})}firstUpdateToLine(t){if(!this.currentId)return;this.setCursor(this.cursors.close);const e=this.mutateFeature.updateLineString({featureId:this.currentId,context:{updateType:s.Commit},coordinateMutations:[{type:at,index:-1,coordinate:t}]});e&&(this.closingPoints.create(e.geometry.coordinates),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:e.geometry.coordinates}),this.lastCommittedCoordinates=e.geometry.coordinates,this.currentCoordinate++,this.pushHistorySnapshot(this.currentId,this.currentCoordinate),this.shouldFinishOnCommit(e.geometry)&&this.close())}updateToLine(t,e){if(!this.currentId)return;const{isClosing:i}=this.closingPoints.isLineStringClosingPoint(t);if(i)return void this.close();this.setCursor(this.cursors.close);const o=this.mutateFeature.updateLineString({featureId:this.currentId,context:{updateType:s.Commit},coordinateMutations:[{type:at,index:-1,coordinate:e}]});o&&(this.closingPoints.update(o.geometry.coordinates),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:o.geometry.coordinates}),this.lastCommittedCoordinates=o.geometry.coordinates,this.currentCoordinate++,this.pushHistorySnapshot(this.currentId,this.currentCoordinate),this.shouldFinishOnCommit(o.geometry)&&this.close())}undoSize(){return this.undoRedo.undoSize()}clearHistory(){this.undoRedo.clear()}pushHistorySnapshot(t,e){const i=this.readFeature.getGeometry(t);this.undoRedo.recordSnapshot({featureCoordinates:i.coordinates,currentCoordinate:e})}updateSnappedGuidancePointFromLastMouseMove(){this.snapping&&this.lastMouseMoveEvent?this.updateSnappedCoordinate(this.lastMouseMoveEvent):this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0)}syncClosingPoints(t){this.currentCoordinate>=2?this.closingPoints.ids.length?this.closingPoints.update(t):this.closingPoints.create(t):this.closingPoints.delete()}undo(){if("drawing"!==this.state||!this.currentId)return;const t=this.undoRedo.beginUndo();if(!t)return;const{previousEntry:e}=t;if(!e){const t=this.currentId;return this.currentId=void 0,this.currentCoordinate=0,this.lastCommittedCoordinates=void 0,this.closingPoints.delete(),"drawing"===this.state&&this.setStarted(),this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t),void this.updateSnappedGuidancePointFromLastMouseMove()}const i=this.mutateFeature.updateLineString({featureId:this.currentId,coordinateMutations:{type:lt,coordinates:e.featureCoordinates},propertyMutations:{[d.CURRENTLY_DRAWING]:!0},context:{updateType:s.Commit}});i&&(this.currentCoordinate=e.currentCoordinate,this.lastCommittedCoordinates=i.geometry.coordinates,this.syncClosingPoints(i.geometry.coordinates),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:i.geometry.coordinates}),this.updateSnappedGuidancePointFromLastMouseMove())}redoSize(){return this.undoRedo.redoSize()}redo(){const t=this.undoRedo.takeRedo();if(t){if(this.currentId){const e=this.mutateFeature.updateLineString({featureId:this.currentId,coordinateMutations:{type:lt,coordinates:t.featureCoordinates},propertyMutations:{[d.CURRENTLY_DRAWING]:!0},context:{updateType:s.Commit}});if(!e)return;this.currentCoordinate=t.currentCoordinate,this.lastCommittedCoordinates=e.geometry.coordinates,this.syncClosingPoints(e.geometry.coordinates),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:e.geometry.coordinates})}else{const{id:e,geometry:i}=this.mutateFeature.createLineString({coordinates:t.featureCoordinates,properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});this.currentId=e,this.currentCoordinate=t.currentCoordinate,this.lastCommittedCoordinates=i.coordinates,"started"===this.state&&this.setDrawing(),this.syncClosingPoints(i.coordinates),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:e,featureCoordinates:i.coordinates})}this.undoRedo.commitRedo(t),this.updateSnappedGuidancePointFromLastMouseMove()}}registerBehaviors(t){this.insertPoint=new Zt(t),this.clickBoundingBox=new It(t),this.pixelDistance=new Pt(t),this.lineSnapping=new Dt(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new St(t,this.pixelDistance,this.clickBoundingBox),this.featureSnapping=new kt(this.coordinateSnapping,this.lineSnapping),this.degreeSnapping=new ee(t),this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.closingPoints=new Qt(t,this.pixelDistance,this.mutateFeature,this.readFeature),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature),this.undoRedo=new te({maxStackSize:t.undoRedoMaxStackSize})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}onMouseMove(t){this.mouseMove=!0,this.setCursor(this.cursors.start),this.lastMouseMoveEvent=t;const e=this.updateSnappedCoordinate(t)||[t.lng,t.lat];if(void 0===this.currentId||0===this.currentCoordinate)return;const{isClosing:i}=this.closingPoints.isLineStringClosingPoint(t);i&&this.setCursor(this.cursors.close);let o=[{type:dt,index:-1,coordinate:e}];if(this.insertCoordinates){const t=this.getInsertCoordinates(e);t&&(o={type:lt,coordinates:t})}const n=this.mutateFeature.updateLineString({coordinateMutations:o,featureId:this.currentId,context:{updateType:s.Provisional}});n&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:n.geometry.coordinates})}getInsertCoordinates(t){if(!this.lastCommittedCoordinates)return;const e=this.lastCommittedCoordinates[this.lastCommittedCoordinates.length-1];if(ct(e,t))return;const i=this.generateInsertCoordinates(e,t);return[...this.lastCommittedCoordinates.slice(0,-1),...i,t]}onRightClick(t){if(!this.editable||"started"!==this.state)return;const{featureId:e,featureCoordinateIndex:n}=this.coordinateSnapping.getSnappable(t,t=>this.lineStringFilter(t));if(!e||void 0===n)return;const r=this.readFeature.getGeometry(e);let a;if("LineString"!==r.type)return;if(a=r.coordinates,a.length<=2)return;const d=this.mutateFeature.updateLineString({featureId:e,coordinateMutations:[{type:ht,index:n}],context:{updateType:s.Finish,action:i}});d&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:e,featureCoordinates:d.geometry.coordinates}),this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.editedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.editedPointId),this.editedPointId=void 0,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0),this.closingPoints.delete(),this.onFinish(e,{mode:this.mode,action:o})}onLeftClick(t){this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(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(t,e)}onClick(t){void 0===this.currentId||this.readFeature.hasFeature(this.currentId)||this.cleanUp(),("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)}this.editedFeatureId&&i&&(this.editedPointId||(this.editedPointId=this.mutateFeature.createGuidancePoint({coordinate:i,type:d.EDITED})),this.setCursor(this.cursors.dragStart),e(!1))}onDrag(t,e){if(this.allowPointerEvent(this.pointerEvents.onDrag,t)&&void 0!==this.editedFeatureId&&void 0!==this.editedFeatureCoordinateIndex){if("coordinate"===this.editedSnapType||"line"===this.editedSnapType&&void 0!==this.editedInsertIndex){const e=this.mutateFeature.updateLineString({featureId:this.editedFeatureId,context:{updateType:s.Provisional},coordinateMutations:[{type:dt,index:this.editedFeatureCoordinateIndex,coordinate:[t.lng,t.lat]}]});if(!e)return;this.showCoordinatePoints&&(void 0!==this.editedInsertIndex?this.coordinatePoints.createOrUpdate({featureId:this.editedFeatureId,featureCoordinates:e.geometry.coordinates}):this.coordinatePoints.updateOneAtIndex(this.editedFeatureId,this.editedFeatureCoordinateIndex,[t.lng,t.lat]))}else if("line"===this.editedSnapType&&void 0===this.editedInsertIndex){this.editedInsertIndex=this.editedFeatureCoordinateIndex+1;const t=this.mutateFeature.updateLineString({featureId:this.editedFeatureId,context:{updateType:s.Provisional}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.editedFeatureId,featureCoordinates:t.geometry.coordinates}),this.editedFeatureCoordinateIndex++}this.snapping&&this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.editedPointId&&this.mutateFeature.updateGuidancePoints([{featureId:this.editedPointId,coordinate:[t.lng,t.lat]}]),this.mutateFeature.updateLineString({featureId:this.editedFeatureId,context:{updateType:s.Provisional},propertyMutations:{[d.EDITED]:!0}})}}onDragEnd(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDragEnd,t))return;if(void 0===this.editedFeatureId)return;if(this.setCursor(this.cursors.dragEnd),!this.mutateFeature.updateLineString({featureId:this.editedFeatureId,propertyMutations:{[d.EDITED]:!1},context:{updateType:s.Finish,action:i}}))return;const o=this.editedFeatureId;e(!0),this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.editedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.editedPointId),this.editedPointId=void 0,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0),this.closingPoints.delete(),this.onFinish(o,{mode:this.mode,action:i})}cleanUp(){const t=this.currentId,e=this.snappedPointId;this.snappedPointId=void 0,this.currentId=void 0,this.currentCoordinate=0,this.lastCommittedCoordinates=void 0,this.undoRedo.clear(),"drawing"===this.state&&this.setStarted(),t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t),this.mutateFeature.deleteFeatureIfPresent(e),this.closingPoints.delete()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});if("Feature"===e.type&&"LineString"===e.geometry.type&&e.properties.mode===this.mode)return i.lineStringDash=this.getDashArrayStylingValue(this.styles.lineStringDash,void 0,e),i.lineStringColor=this.getHexColorStylingValue(this.styles.lineStringColor,i.lineStringColor,e),i.lineStringOpacity=this.getNumericStylingValue(this.styles.lineStringOpacity,void 0===i.lineStringOpacity?1:i.lineStringOpacity,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[d.COORDINATE_POINT],o=e.properties[d.CLOSING_POINT]?"closingPoint":e.properties[d.SNAPPING_POINT]?"snappingPoint":t?"coordinatePoint":void 0;if(!o)return i;const n={closingPoint:{width:this.styles.closingPointWidth,color:this.styles.closingPointColor,opacity:this.styles.closingPointOpacity,outlineColor:this.styles.closingPointOutlineColor,outlineWidth:this.styles.closingPointOutlineWidth,outlineOpacity:this.styles.closingPointOutlineOpacity},snappingPoint:{width:this.styles.snappingPointWidth,color:this.styles.snappingPointColor,opacity:this.styles.snappingPointOpacity,outlineColor:this.styles.snappingPointOutlineColor,outlineWidth:this.styles.snappingPointOutlineWidth,outlineOpacity:this.styles.snappingPointOutlineOpacity},coordinatePoint:{width:this.styles.coordinatePointWidth,color:this.styles.coordinatePointColor,opacity:this.styles.coordinatePointOpacity,outlineColor:this.styles.coordinatePointOutlineColor,outlineWidth:this.styles.coordinatePointOutlineWidth,outlineOpacity:this.styles.coordinatePointOutlineOpacity}};return i.pointWidth=this.getNumericStylingValue(n[o].width,i.pointWidth,e),i.pointOpacity=this.getNumericStylingValue(n[o].opacity,1,e),i.pointColor=this.getHexColorStylingValue(n[o].color,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(n[o].outlineColor,"#ffffff",e),i.pointOutlineWidth=this.getNumericStylingValue(n[o].outlineWidth,2,e),i.pointOutlineOpacity=this.getNumericStylingValue(n[o].outlineOpacity,1,e),i.zIndex=t?l:50,i}return i}validateFeature(t){return this.validateModeFeature(t,t=>Jt(t,this.coordinatePrecision))}lineStringFilter(t){return Boolean("LineString"===t.geometry.type&&t.properties&&t.properties.mode===this.mode)}snapCoordinate(t){var e,i,o,n,s;let r;if(null!=(e=this.snapping)&&e.toDegree&&this.currentId&&(r=this.degreeSnapping.getSnappableCoordinate(t,this.readFeature.getGeometry(this.currentId),this.currentCoordinate,this.snapping.toDegree)),null!=(i=this.snapping)&&i.toLine){let e;e=this.currentId?this.lineSnapping.getSnappableCoordinate(t,this.currentId):this.lineSnapping.getSnappableCoordinateFirstClick(t),e&&(r=e)}if(null!=(o=this.snapping)&&o.toCoordinate){let e;e=this.currentId?this.coordinateSnapping.getSnappableCoordinate(t,this.currentId):this.coordinateSnapping.getSnappableCoordinateFirstClick(t),e&&(r=e)}if(null!=(n=this.snapping)&&n.toCustom){const e=this.snapping.toCustom(t,{currentCoordinate:this.currentCoordinate,currentId:this.currentId,getCurrentGeometrySnapshot:this.currentId?()=>this.readFeature.getGeometry(this.currentId):()=>null,project:this.project,unproject:this.unproject});e&&(r=e)}if(null!=(s=this.snapping)&&s.toFeature){const e=this.featureSnapping.getSnappable(t,this.currentId,this.snapping.toFeature.filter,{toLine:this.snapping.toFeature.toLine,toCoordinate:this.snapping.toFeature.toCoordinate});e.coordinate&&(r=e.coordinate)}return r}afterFeatureUpdated(t){this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.editedFeatureId===t.id&&this.editedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.editedPointId),this.editedPointId=void 0,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedSnapType=void 0),this.snappedPointId&&this.lastMouseMoveEvent&&this.updateSnappedCoordinate(this.lastMouseMoveEvent),this.currentId===t.id&&(this.closingPoints.delete(),this.currentCoordinate=0,this.currentId=void 0,this.lastCommittedCoordinates=void 0,this.undoRedo.clear(),"drawing"===this.state&&this.setStarted())}afterFeatureAdded(t){this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}}const se={cancel:E,finish:M},re={start:D,close:k};class ae extends x{constructor(t){super(t,!0),this.mode="polyline",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=se,this.cursors=re,this.mouseMove=!1,this.snapping=void 0,this.snappedPointId=void 0,this.showCoordinatePoints=!1,this.mutateFeature=void 0,this.readFeature=void 0,this.pixelDistance=void 0,this.closingPoints=void 0,this.clickBoundingBox=void 0,this.lineSnapping=void 0,this.coordinateSnapping=void 0,this.featureSnapping=void 0,this.degreeSnapping=void 0,this.coordinatePoints=void 0,this.updateOptions(t)}updateOptions(e){var i;super.updateOptions(e),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),v(null==e?void 0:e.snapping)&&(this.snapping=e.snapping),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),C(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints))}registerBehaviors(t){this.clickBoundingBox=new It(t),this.pixelDistance=new Pt(t),this.lineSnapping=new Dt(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new St(t,this.pixelDistance,this.clickBoundingBox),this.featureSnapping=new kt(this.coordinateSnapping,this.lineSnapping),this.degreeSnapping=new ee(t),this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.closingPoints=new Qt(t,this.pixelDistance,this.mutateFeature,this.readFeature),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature)}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}finishLine(){if(!this.currentId)return;const t=this.mutateFeature.updateLineString({featureId:this.currentId,context:{updateType:s.Finish,action:e},coordinateMutations:[{type:ht,index:-1}],propertyMutations:{[d.CURRENTLY_DRAWING]:void 0}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates});const i=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.closingPoints.delete(),this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(i,{mode:this.mode,action:e})}toPolygonLikeCoordinates(t){return 0===t.length?[t]:[[...t,t[0]]]}closeAsPolygon(){if(!this.currentId)return;const t=this.readFeature.getGeometry(this.currentId).coordinates.slice(0,-1);if(t.length<3)return;const i=this.currentId,o=[...t,t[0]],n=this.mutateFeature.createPolygon({coordinates:o,properties:{mode:this.mode},context:{updateType:s.Finish,action:e}});n&&(this.showCoordinatePoints&&(this.coordinatePoints.deletePointsByFeatureIds([i]),this.coordinatePoints.createOrUpdate({featureId:n.id,featureCoordinates:n.geometry.coordinates})),this.mutateFeature.deleteFeatureIfPresent(i),this.currentCoordinate=0,this.currentId=void 0,this.closingPoints.delete(),this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(n.id,{mode:this.mode,action:e}))}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),this.updateSnappedCoordinate(t),!this.currentId||0===this.currentCoordinate)return;const e=this.mutateFeature.updateLineString({featureId:this.currentId,coordinateMutations:[{type:dt,index:-1,coordinate:[t.lng,t.lat]}],context:{updateType:s.Provisional}});if(!e)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:e.geometry.coordinates});const{isClosing:i,isPreviousClosing:o}=this.closingPoints.isPolygonClosingPoints(t);(i&&this.currentCoordinate>=3||o&&this.currentCoordinate>=2)&&this.setCursor(this.cursors.close)}onLeftClick(t){this.updateSnappedCoordinate(t);const e=[t.lng,t.lat];if(0===this.currentCoordinate){const t=this.mutateFeature.createLineString({coordinates:[e,e],properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});return this.currentId=t.id,this.currentCoordinate=1,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),void this.setDrawing()}if(!this.currentId)return;const{isClosing:i,isPreviousClosing:o}=this.closingPoints.isPolygonClosingPoints(t);if(i&&this.currentCoordinate>=3)return void this.closeAsPolygon();if(o&&this.currentCoordinate>=2)return void this.finishLine();const n=this.mutateFeature.updateLineString({featureId:this.currentId,context:{updateType:s.Commit},coordinateMutations:[{type:at,index:-1,coordinate:e}]});if(n&&(this.currentCoordinate++,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:n.geometry.coordinates}),this.currentCoordinate>=2)){const t=this.toPolygonLikeCoordinates(n.geometry.coordinates);0===this.closingPoints.ids.length?this.closingPoints.create(t):this.closingPoints.update(t)}}onClick(t){"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)&&(this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1,this.onLeftClick(t))}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.finishLine()}onKeyDown(){}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){const t=this.currentId;this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted(),t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t),this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0,this.closingPoints.delete()}updateSnappedCoordinate(t){var e;const i=this.snapCoordinate(t);i&&!1!==(null==(e=this.snapping)?void 0:e.showSnappingPoint)?this.snappedPointId?this.mutateFeature.updateGuidancePoints([{featureId:this.snappedPointId,coordinate:i}]):this.snappedPointId=this.mutateFeature.createGuidancePoint({coordinate:i,type:d.SNAPPING_POINT}):this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),i&&(t.lng=i[0],t.lat=i[1])}snapCoordinate(t){var e,i,o,n,s;let r;if(null!=(e=this.snapping)&&e.toDegree&&this.currentId&&(r=this.degreeSnapping.getSnappableCoordinate(t,this.readFeature.getGeometry(this.currentId),this.currentCoordinate,this.snapping.toDegree)),null!=(i=this.snapping)&&i.toLine){let e;e=this.currentId?this.lineSnapping.getSnappableCoordinate(t,this.currentId):this.lineSnapping.getSnappableCoordinateFirstClick(t),e&&(r=e)}if(null!=(o=this.snapping)&&o.toCoordinate){let e;e=this.currentId?this.coordinateSnapping.getSnappableCoordinate(t,this.currentId):this.coordinateSnapping.getSnappableCoordinateFirstClick(t),e&&(r=e)}if(null!=(n=this.snapping)&&n.toFeature){const e=this.featureSnapping.getSnappable(t,this.currentId,this.snapping.toFeature.filter,{toLine:this.snapping.toFeature.toLine,toCoordinate:this.snapping.toFeature.toCoordinate});e.coordinate&&(r=e.coordinate)}if(null!=(s=this.snapping)&&s.toCustom){const e=this.snapping.toCustom(t,{currentCoordinate:this.currentCoordinate,currentId:this.currentId,getCurrentGeometrySnapshot:this.currentId?()=>this.readFeature.getGeometry(this.currentId):()=>null,project:this.project,unproject:this.unproject});e&&(r=e)}return r}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});if(e.properties.mode!==this.mode)return i;if("LineString"===e.geometry.type)return i.lineStringColor=this.getHexColorStylingValue(this.styles.lineStringColor,i.lineStringColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.lineStringWidth,i.lineStringWidth,e),i.lineStringOpacity=this.getNumericStylingValue(this.styles.lineStringOpacity,1,e),i.lineStringDash=this.getDashArrayStylingValue(this.styles.lineStringDash,void 0,e),i.zIndex=h,i;if("Polygon"===e.geometry.type)return i.polygonFillColor=this.getHexColorStylingValue(this.styles.polygonFillColor,i.polygonFillColor,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.polygonFillOpacity,i.polygonFillOpacity,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.polygonOutlineColor,i.polygonOutlineColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.polygonOutlineWidth,i.polygonOutlineWidth,e),i.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.polygonOutlineOpacity,1,e),i.zIndex=h,i;if("Point"===e.geometry.type){const t=!0===e.properties[d.CLOSING_POINT],o=!0===e.properties[d.SNAPPING_POINT],n=!0===e.properties[d.COORDINATE_POINT];if(!t&&!o&&!n)return i;i.pointColor=this.getHexColorStylingValue(t?this.styles.closingPointColor:o?this.styles.snappingPointColor:this.styles.coordinatePointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(t?this.styles.closingPointWidth:o?this.styles.snappingPointWidth:this.styles.coordinatePointWidth,i.pointWidth,e),i.pointOpacity=this.getNumericStylingValue(t?this.styles.closingPointOpacity:o?this.styles.snappingPointOpacity:this.styles.coordinatePointOpacity,1,e),i.pointOutlineColor=this.getHexColorStylingValue(t?this.styles.closingPointOutlineColor:o?this.styles.snappingPointOutlineColor:this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(t?this.styles.closingPointOutlineWidth:o?this.styles.snappingPointOutlineWidth:this.styles.coordinatePointOutlineWidth,2,e),i.pointOutlineOpacity=this.getNumericStylingValue(t?this.styles.closingPointOutlineOpacity:o?this.styles.snappingPointOutlineOpacity:this.styles.coordinatePointOutlineOpacity,1,e),i.zIndex=n?l:u}return i}afterFeatureAdded(t){!this.showCoordinatePoints||"LineString"!==t.geometry.type&&"Polygon"!==t.geometry.type||this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}afterFeatureUpdated(t){!this.showCoordinatePoints||"LineString"!==t.geometry.type&&"Polygon"!==t.geometry.type||this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.currentId===t.id&&(this.currentCoordinate=0,this.currentId=void 0,this.closingPoints.delete(),"drawing"===this.state&&this.setStarted())}validateFeature(t){return this.validateModeFeature(t,t=>"LineString"===t.geometry.type?Jt(t,this.coordinatePrecision):"Polygon"===t.geometry.type?it(t,this.coordinatePrecision):{valid:!1,reason:"Only LineString or Polygon features are valid"})}}const de="Feature is not a Point",he="Feature has invalid coordinates",le="Feature has coordinates with excessive precision";function ue(t,e){return"Point"!==t.geometry.type?{valid:!1,reason:de}:q(t.geometry.coordinates)?$(t.geometry.coordinates,e)?{valid:!0}:{valid:!1,reason:le}:{valid:!1,reason:he}}class ce extends nt{constructor(t,e,i){super(t),this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.pixelDistance=e,this.clickBoundingBox=i}getNearestPointFeature(t){const e=this.clickBoundingBox.create(t),i=this.store.search(e);let o,n=Infinity;for(let e=0;en||r>this.pointerDistance||(n=r,o=s)}return o}}const pe={create:D,dragStart:N,dragEnd:D};class ge extends x{constructor(t){super(t,!0),this.mode="point",this.cursors=pe,this.editable=!1,this.snapping=void 0,this.editedFeatureId=void 0,this.snappedPointId=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.pointSearch=void 0,this.mutateFeature=void 0,this.coordinateSnapping=void 0,this.lineSnapping=void 0,this.featureSnapping=void 0,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),v(null==e?void 0:e.snapping)&&(this.snapping=e.snapping),C(null==e?void 0:e.editable)&&(this.editable=e.editable)}start(){this.setStarted(),this.setCursor(this.cursors.create)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}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(t){t&&this.updateSnappedCoordinate(t)}onKeyDown(){}onKeyUp(){}cleanUp(){this.editedFeatureId=void 0,this.deleteSnappedPoint()}onDragStart(t,e){if(this.allowPointerEvent(this.pointerEvents.onDragStart,t)){if(this.editable){const e=this.pointSearch.getNearestPointFeature(t);this.editedFeatureId=null==e?void 0:e.id}this.editedFeatureId&&(this.setCursor(this.cursors.dragStart),e(!1))}}onDrag(t,e){this.allowPointerEvent(this.pointerEvents.onDrag,t)&&void 0!==this.editedFeatureId&&this.mutateFeature.updatePoint({featureId:this.editedFeatureId,coordinateMutations:{type:lt,coordinates:this.snapCoordinate(t,this.editedFeatureId)},propertyMutations:{[d.EDITED]:!0},context:{updateType:s.Provisional}})}onDragEnd(t,i){if(!this.allowPointerEvent(this.pointerEvents.onDragEnd,t))return;if(void 0===this.editedFeatureId)return;if(!this.mutateFeature.updatePoint({featureId:this.editedFeatureId,propertyMutations:{mode:this.mode,[d.EDITED]:!1},context:{updateType:s.Finish,action:"edit"}}))return;const o=this.editedFeatureId;this.setCursor(this.cursors.dragEnd),this.editedFeatureId=void 0,this.deleteSnappedPoint(),i(!0),this.onFinish(o,{mode:this.mode,action:e})}registerBehaviors(t){this.pixelDistance=new Pt(t),this.clickBoundingBox=new It(t),this.pointSearch=new ce(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new St(t,this.pixelDistance,this.clickBoundingBox),this.lineSnapping=new Dt(t,this.pixelDistance,this.clickBoundingBox),this.featureSnapping=new kt(this.coordinateSnapping,this.lineSnapping),this.mutateFeature=new ut(t,{validate:this.validate})}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});if("Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode){const t=e.properties[d.SNAPPING_POINT],o=Boolean(e.id&&this.editedFeatureId===e.id);i.pointWidth=this.getNumericStylingValue(t?this.styles.snappingPointWidth:o?this.styles.editedPointWidth:this.styles.pointWidth,i.pointWidth,e),i.pointOpacity=this.getNumericStylingValue(t?this.styles.snappingPointOpacity:this.styles.pointOpacity,void 0===i.pointOpacity?1:i.pointOpacity,e),i.pointColor=this.getHexColorStylingValue(t?this.styles.snappingPointColor:o?this.styles.editedPointColor:this.styles.pointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(t?this.styles.snappingPointOutlineColor:o?this.styles.editedPointOutlineColor:this.styles.pointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(t?this.styles.snappingPointOutlineOpacity:this.styles.pointOutlineOpacity,void 0===i.pointOutlineOpacity?1:i.pointOutlineOpacity,e),i.pointOutlineWidth=this.getNumericStylingValue(t?this.styles.snappingPointOutlineWidth:o?this.styles.editedPointOutlineWidth:this.styles.pointOutlineWidth,2,e),i.zIndex=t?40:u}return i}validateFeature(t){return this.validateModeFeature(t,t=>ue(t,this.coordinatePrecision))}onLeftClick(t){this.deleteSnappedPoint();const i=this.mutateFeature.createPoint({coordinates:this.snapCoordinate(t),properties:{mode:this.mode},context:{updateType:s.Finish,action:e}});i&&this.onFinish(i.id,{mode:this.mode,action:e})}snapCoordinate(t,e){var i;return null!=(i=this.getSnappedCoordinate(t,e))?i:[t.lng,t.lat]}updateSnappedCoordinate(t){var e;const i=this.getSnappedCoordinate(t);i&&!0===(null==(e=this.snapping)?void 0:e.showSnappingPoint)?this.snappedPointId?this.mutateFeature.updateGuidancePoints([{featureId:this.snappedPointId,coordinate:i}]):this.snappedPointId=this.mutateFeature.createGuidancePoint({coordinate:i,type:d.SNAPPING_POINT}):this.deleteSnappedPoint()}deleteSnappedPoint(){this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0)}getSnappedCoordinate(t,e){var i,o,n;let s;if(null!=(i=this.snapping)&&i.toCoordinate){const i=e?this.coordinateSnapping.getSnappableCoordinate(t,e):this.coordinateSnapping.getSnappableCoordinateFirstClick(t);i&&(s=i)}if(null!=(o=this.snapping)&&o.toFeature){const i=this.featureSnapping.getSnappable(t,e,this.snapping.toFeature.filter,{toLine:this.snapping.toFeature.toLine,toCoordinate:this.snapping.toFeature.toCoordinate});i.coordinate&&(s=i.coordinate)}if(null!=(n=this.snapping)&&n.toCustom){const i=this.snapping.toCustom(t,{currentCoordinate:0,currentId:e,getCurrentGeometrySnapshot:()=>null,project:this.project,unproject:this.unproject});i&&(s=i)}return s}onRightClick(t){if(!this.editable)return;const e=this.pointSearch.getNearestPointFeature(t);e&&this.mutateFeature.deleteFeatureIfPresent(e.id)}afterFeatureUpdated(t){this.editedFeatureId===t.id&&(this.editedFeatureId=void 0,this.setCursor(this.cursors.create))}}const ye={cancel:E,finish:M},me={start:D,close:k,dragStart:N,dragEnd:D};class fe extends x{constructor(t){super(t,!0),this.mode="polygon",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=ye,this.cursors=me,this.mouseMove=!1,this.showCoordinatePoints=!1,this.lastMouseMoveEvent=void 0,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.featureSnapping=void 0,this.degreeSnapping=void 0,this.pixelDistance=void 0,this.closingPoints=void 0,this.clickBoundingBox=void 0,this.mutateFeature=void 0,this.readFeature=void 0,this.undoRedo=void 0,this.updateOptions(t)}updateOptions(e){var i;super.updateOptions(e),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),v(null==e?void 0:e.snapping)&&(this.snapping=e.snapping),C(null==e?void 0:e.editable)&&(this.editable=e.editable),v(null==e?void 0:e.pointerEvents)&&(this.pointerEvents=e.pointerEvents),C(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints))}close(){if(void 0===this.currentId)return;if(this.readFeature.getCoordinates(this.currentId).length<5)return;const t=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:[{type:ht,index:-2}],propertyMutations:{[d.CURRENTLY_DRAWING]:void 0,[d.COMMITTED_COORDINATE_COUNT]:void 0,[d.PROVISIONAL_COORDINATE_COUNT]:void 0},context:{updateType:s.Finish,action:e}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates}),"drawing"===this.state&&this.setStarted(),this.editedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.editedPointId),this.editedPointId=void 0),this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.closingPoints.delete();const i=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.undoRedo.clear(),this.onFinish(i,{mode:this.mode,action:e})}registerBehaviors(t){this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.clickBoundingBox=new It(t),this.pixelDistance=new Pt(t),this.lineSnapping=new Dt(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new St(t,this.pixelDistance,this.clickBoundingBox),this.featureSnapping=new kt(this.coordinateSnapping,this.lineSnapping),this.degreeSnapping=new ee(t),this.closingPoints=new Qt(t,this.pixelDistance,this.mutateFeature,this.readFeature),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature),this.undoRedo=new te({maxStackSize:t.undoRedoMaxStackSize})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}updateSnappedCoordinate(t){var e;const i=this.snapCoordinate(t);i&&!1!==(null==(e=this.snapping)?void 0:e.showSnappingPoint)?this.snappedPointId?this.mutateFeature.updateGuidancePoints([{featureId:this.snappedPointId,coordinate:i}]):this.snappedPointId=this.mutateFeature.createGuidancePoint({coordinate:i,type:d.SNAPPING_POINT}):this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),i&&(t.lng=i[0],t.lat=i[1])}undoSize(){return this.undoRedo.undoSize()}clearHistory(){this.undoRedo.clear()}pushHistorySnapshot(t,e){const i=this.readFeature.getGeometry(t);this.undoRedo.recordSnapshot({featureCoordinates:i.coordinates,currentCoordinate:e})}updateSnappedGuidancePointFromLastMouseMove(){this.snapping&&this.lastMouseMoveEvent?this.updateSnappedCoordinate(this.lastMouseMoveEvent):this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0)}syncClosingPoints(t){this.currentCoordinate>=3?this.closingPoints.ids.length?this.closingPoints.update(t):this.closingPoints.create(t):this.closingPoints.delete()}undo(){if("drawing"!==this.state||!this.currentId)return;const t=this.undoRedo.beginUndo();if(!t)return;const{previousEntry:e}=t;if(!e){const t=this.currentId;return this.currentId=void 0,this.currentCoordinate=0,this.closingPoints.delete(),"drawing"===this.state&&this.setStarted(),this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t),void this.updateSnappedGuidancePointFromLastMouseMove()}const i=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:{type:lt,coordinates:e.featureCoordinates},propertyMutations:{[d.CURRENTLY_DRAWING]:!0,[d.COMMITTED_COORDINATE_COUNT]:e.currentCoordinate,[d.PROVISIONAL_COORDINATE_COUNT]:e.currentCoordinate},context:{updateType:s.Commit}});i&&(this.currentCoordinate=e.currentCoordinate,this.syncClosingPoints(i.geometry.coordinates),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:i.geometry.coordinates}),this.updateSnappedGuidancePointFromLastMouseMove())}redoSize(){return this.undoRedo.redoSize()}redo(){const t=this.undoRedo.takeRedo();if(t){if(this.currentId){const e=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:{type:lt,coordinates:t.featureCoordinates},propertyMutations:{[d.CURRENTLY_DRAWING]:!0,[d.COMMITTED_COORDINATE_COUNT]:t.currentCoordinate,[d.PROVISIONAL_COORDINATE_COUNT]:t.currentCoordinate},context:{updateType:s.Commit}});if(!e)return;this.currentCoordinate=t.currentCoordinate,this.syncClosingPoints(e.geometry.coordinates),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:e.geometry.coordinates})}else{const e=this.undoRedo.cloneCoordinates(t.featureCoordinates)[0],{id:i,geometry:o}=this.mutateFeature.createPolygon({coordinates:e,properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0,[d.COMMITTED_COORDINATE_COUNT]:t.currentCoordinate,[d.PROVISIONAL_COORDINATE_COUNT]:t.currentCoordinate}});this.currentId=i,this.currentCoordinate=t.currentCoordinate,"started"===this.state&&this.setDrawing(),this.syncClosingPoints(o.coordinates),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:i,featureCoordinates:o.coordinates})}this.undoRedo.commitRedo(t),this.updateSnappedGuidancePointFromLastMouseMove()}}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),this.lastMouseMoveEvent=t,this.updateSnappedCoordinate(t),void 0===this.currentId||0===this.currentCoordinate)return;const e=this.readFeature.getCoordinate(this.currentId,0),i=[t.lng,t.lat];let o;if(1===this.currentCoordinate)o=[{type:dt,index:1,coordinate:i},{type:dt,index:2,coordinate:[t.lng,t.lat]}];else if(2===this.currentCoordinate)o=[{type:dt,index:2,coordinate:i}];else{const{isClosing:n,isPreviousClosing:s}=this.closingPoints.isPolygonClosingPoints(t);s||n?(this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.setCursor(this.cursors.close),o=[{type:dt,index:-1,coordinate:e},{type:dt,index:-2,coordinate:e}]):o=[{type:dt,index:-2,coordinate:i},{type:dt,index:-1,coordinate:e}]}const n=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:o,propertyMutations:{[d.PROVISIONAL_COORDINATE_COUNT]:this.currentCoordinate+1},context:{updateType:s.Provisional}});n&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:n.geometry.coordinates})}snapCoordinate(t){var e,i,o,n,s;let r;if(null!=(e=this.snapping)&&e.toDegree&&this.currentId&&(r=this.degreeSnapping.getSnappableCoordinate(t,this.readFeature.getGeometry(this.currentId),this.currentCoordinate,this.snapping.toDegree)),null!=(i=this.snapping)&&i.toLine){let e;e=this.currentId?this.lineSnapping.getSnappableCoordinate(t,this.currentId):this.lineSnapping.getSnappableCoordinateFirstClick(t),e&&(r=e)}if(null!=(o=this.snapping)&&o.toCoordinate){let e;e=this.currentId?this.coordinateSnapping.getSnappableCoordinate(t,this.currentId):this.coordinateSnapping.getSnappableCoordinateFirstClick(t),e&&(r=e)}if(null!=(n=this.snapping)&&n.toFeature){var a;const e=this.featureSnapping.getSnappable(t,this.currentId,null==(a=this.snapping)?void 0:a.toFeature.filter,{toLine:this.snapping.toFeature.toLine,toCoordinate:this.snapping.toFeature.toCoordinate});e.coordinate&&(r=e.coordinate)}if(null!=(s=this.snapping)&&s.toCustom){const e=this.snapping.toCustom(t,{currentCoordinate:this.currentCoordinate,currentId:this.currentId,getCurrentGeometrySnapshot:this.currentId?()=>this.readFeature.getGeometry(this.currentId):()=>null,project:this.project,unproject:this.unproject});e&&(r=e)}return r}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:e,featureCoordinateIndex:o}=this.coordinateSnapping.getSnappable(t,t=>this.polygonFilter(t));if(!e||void 0===o)return;const n=this.readFeature.getGeometry(e);if("Polygon"!==n.type)return;const r=n.coordinates[0];if(r.length<=4)return;let a;a=0===o||o===r.length-1?[{type:ht,index:0},{type:ht,index:-1},{type:at,index:-1,coordinate:r[1]}]:[{type:ht,index:o}];const h=this.mutateFeature.updatePolygon({featureId:e,coordinateMutations:a,context:{updateType:s.Finish,action:i}});if(h){if(this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:e,featureCoordinates:h.geometry.coordinates}),this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0,this.snapping)){const e=this.snapCoordinate(t);if(e){const[t]=this.mutateFeature.createGuidancePoints({type:d.SNAPPING_POINT,coordinates:[e]});this.snappedPointId=t}}this.onFinish(e,{mode:this.mode,action:i})}}onLeftClick(t){this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0);let e=this.snapCoordinate(t)||[t.lng,t.lat];if(0===this.currentCoordinate){const{id:t,geometry:i}=this.mutateFeature.createPolygon({coordinates:[e,e,e,e],properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0,[d.COMMITTED_COORDINATE_COUNT]:this.currentCoordinate+1,[d.PROVISIONAL_COORDINATE_COUNT]:this.currentCoordinate+1}});this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:t,featureCoordinates:i.coordinates}),this.currentId=t,this.currentCoordinate++,this.pushHistorySnapshot(this.currentId,this.currentCoordinate),this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){if(this.readFeature.coordinateAtIndexIsIdentical({featureId:this.currentId,newCoordinate:e,index:0}))return;const t=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:[{type:dt,index:1,coordinate:e},{type:dt,index:2,coordinate:e}],propertyMutations:{[d.COMMITTED_COORDINATE_COUNT]:this.currentCoordinate+1},context:{updateType:s.Commit}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates}),this.currentCoordinate++,this.pushHistorySnapshot(this.currentId,this.currentCoordinate)}else if(2===this.currentCoordinate&&this.currentId){if(this.readFeature.coordinateAtIndexIsIdentical({featureId:this.currentId,newCoordinate:e,index:1}))return;const t=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:[{type:dt,index:2,coordinate:e},{type:at,index:2,coordinate:e}],propertyMutations:{[d.COMMITTED_COORDINATE_COUNT]:this.currentCoordinate+1},context:{updateType:s.Commit}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates}),2===this.currentCoordinate&&this.closingPoints.create(t.geometry.coordinates),this.currentCoordinate++,this.pushHistorySnapshot(this.currentId,this.currentCoordinate)}else if(this.currentId){const{isClosing:i,isPreviousClosing:o}=this.closingPoints.isPolygonClosingPoints(t);if(o||i)this.close();else{if(this.readFeature.coordinateAtIndexIsIdentical({featureId:this.currentId,newCoordinate:e,index:this.currentCoordinate-1}))return;const t=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:[{type:rt,index:-1,coordinate:e}],propertyMutations:{[d.COMMITTED_COORDINATE_COUNT]:this.currentCoordinate+1},context:{updateType:s.Commit}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates}),this.currentCoordinate++,this.pushHistorySnapshot(this.currentId,this.currentCoordinate),this.closingPoints.ids.length&&this.closingPoints.update(t.geometry.coordinates)}}}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)}this.editedFeatureId&&i&&(this.editedPointId||(this.editedPointId=this.mutateFeature.createGuidancePoint({coordinate:i,type:d.EDITED})),this.setCursor(this.cursors.dragStart),e(!1))}onDrag(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDrag,t))return;if(void 0===this.editedFeatureId||void 0===this.editedFeatureCoordinateIndex)return;const i=this.readFeature.getGeometry(this.editedFeatureId),o=[t.lng,t.lat];let n=[];if("coordinate"===this.editedSnapType||"line"===this.editedSnapType&&void 0!==this.editedInsertIndex?n=0===this.editedFeatureCoordinateIndex||this.editedFeatureCoordinateIndex===i.coordinates[0].length-1?[{type:dt,index:0,coordinate:o},{type:dt,index:-1,coordinate:o}]:[{type:dt,index:this.editedFeatureCoordinateIndex,coordinate:o}]:"line"===this.editedSnapType&&void 0===this.editedInsertIndex&&(this.editedInsertIndex=this.editedFeatureCoordinateIndex+1,n=[{type:rt,index:this.editedInsertIndex,coordinate:o}],this.editedFeatureCoordinateIndex++),0===n.length)return;const r=this.mutateFeature.updatePolygon({featureId:this.editedFeatureId,coordinateMutations:n,propertyMutations:{[d.EDITED]:!0},context:{updateType:s.Provisional}});r&&(this.showCoordinatePoints&&(this.editedInsertIndex?this.coordinatePoints.createOrUpdate({featureId:this.editedFeatureId,featureCoordinates:r.geometry.coordinates}):this.coordinatePoints.updateOneAtIndex(this.editedFeatureId,this.editedFeatureCoordinateIndex,r.geometry.coordinates[0][this.editedFeatureCoordinateIndex])),this.snapping&&this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.editedPointId&&this.mutateFeature.updateGuidancePoints([{featureId:this.editedPointId,coordinate:o}]))}onDragEnd(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDragEnd,t))return;if(void 0===this.editedFeatureId)return;if(this.setCursor(this.cursors.dragEnd),!this.mutateFeature.updatePolygon({featureId:this.editedFeatureId,propertyMutations:{[d.EDITED]:!1},context:{updateType:s.Finish,action:i}}))return;const o=this.editedFeatureId;this.editedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.editedPointId),this.editedPointId=void 0),this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0),this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedInsertIndex=void 0,this.editedSnapType=void 0,e(!0),this.onFinish(o,{mode:this.mode,action:i})}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,this.undoRedo.clear(),"drawing"===this.state&&this.setStarted(),t&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t),this.mutateFeature.deleteFeatureIfPresent(i),this.mutateFeature.deleteFeatureIfPresent(e),this.closingPoints.ids.length&&this.closingPoints.delete()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 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.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.outlineOpacity,1,e),i.zIndex=h,i;if("Point"===e.geometry.type){const t=e.properties[d.EDITED],o=e.properties[d.COORDINATE_POINT],n=t?"editedPoint":e.properties[d.CLOSING_POINT]?"closingPoint":e.properties[d.SNAPPING_POINT]?"snappingPoint":o?"coordinatePoint":void 0;if(!n)return i;const s={editedPoint:{width:this.styles.editedPointOutlineWidth,color:this.styles.editedPointColor,opacity:this.styles.editedPointOpacity,outlineColor:this.styles.editedPointOutlineColor,outlineWidth:this.styles.editedPointOutlineWidth,outlineOpacity:this.styles.editedPointOutlineOpacity},closingPoint:{width:this.styles.closingPointWidth,color:this.styles.closingPointColor,opacity:this.styles.closingPointOpacity,outlineColor:this.styles.closingPointOutlineColor,outlineWidth:this.styles.closingPointOutlineWidth,outlineOpacity:this.styles.closingPointOutlineOpacity},snappingPoint:{width:this.styles.snappingPointWidth,color:this.styles.snappingPointColor,opacity:this.styles.snappingPointOpacity,outlineColor:this.styles.snappingPointOutlineColor,outlineWidth:this.styles.snappingPointOutlineWidth,outlineOpacity:this.styles.snappingPointOutlineOpacity},coordinatePoint:{width:this.styles.coordinatePointWidth,color:this.styles.coordinatePointColor,opacity:this.styles.coordinatePointOpacity,outlineColor:this.styles.coordinatePointOutlineColor,outlineWidth:this.styles.coordinatePointOutlineWidth,outlineOpacity:this.styles.coordinatePointOutlineOpacity}};return i.pointWidth=this.getNumericStylingValue(s[n].width,i.pointWidth,e),i.pointOpacity=this.getNumericStylingValue(s[n].opacity,1,e),i.pointColor=this.getHexColorStylingValue(s[n].color,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(s[n].outlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(s[n].outlineOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(s[n].outlineWidth,2,e),i.zIndex=t?40:o?l:u,i}}return i}afterFeatureAdded(t){this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}afterFeatureUpdated(t){this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.editedFeatureId===t.id&&this.editedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.editedPointId),this.editedPointId=void 0,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedSnapType=void 0),this.snappedPointId&&this.lastMouseMoveEvent&&this.updateSnappedCoordinate(this.lastMouseMoveEvent),this.currentId===t.id&&(this.currentCoordinate=0,this.currentId=void 0,this.undoRedo.clear(),this.closingPoints.delete(),"drawing"===this.state&&this.setStarted())}validateFeature(t){return this.validateModeFeature(t,t=>it(t,this.coordinatePrecision))}}const Ce={cancel:E,finish:M},Pe={start:D};class ve extends x{constructor(t){super(t,!0),this.mode="rectangle",this.startPosition=void 0,this.endPosition=void 0,this.currentRectangleId=void 0,this.keyEvents=Ce,this.cursors=Pe,this.drawInteraction="click-move",this.drawType=void 0,this.showCoordinatePoints=!1,this.mutateFeature=void 0,this.readFeature=void 0,this.coordinatePoints=void 0,this.updateOptions(t)}updateOptions(e){var i;super.updateOptions(e),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),S(null==e?void 0:e.drawInteraction)&&(this.drawInteraction=e.drawInteraction),C(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints))}updateRectangle(t,i){if(!this.startPosition||!this.currentRectangleId)return;const o=i===s.Finish,n=this.mutateFeature.updatePolygon({featureId:this.currentRectangleId,coordinateMutations:[{type:dt,index:1,coordinate:[t[0],this.startPosition[1]]},{type:dt,index:2,coordinate:t},{type:dt,index:3,coordinate:[this.startPosition[0],t[1]]}],propertyMutations:o?{[d.CURRENTLY_DRAWING]:void 0}:{},context:o?{updateType:i,action:e}:{updateType:i}});return n&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentRectangleId,featureCoordinates:n.geometry.coordinates}),n}close(){if(!this.currentRectangleId||!this.endPosition)return;if(!this.updateRectangle(this.endPosition,s.Finish))return;const t=this.currentRectangleId;this.startPosition=void 0,this.currentRectangleId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:e})}beginDrawing(t,e="click"){this.startPosition=[t.lng,t.lat],this.endPosition=[t.lng,t.lat];const i=this.mutateFeature.createPolygon({coordinates:[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]],properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});this.currentRectangleId=i.id,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:i.id,featureCoordinates:i.geometry.coordinates}),this.drawType=e,this.setDrawing()}moveDrawAllowed(){return"click-move"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}dragDrawAllowed(){return"click-drag"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}onClick(t){this.moveDrawAllowed()&&("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.startPosition?(this.endPosition=[t.lng,t.lat],this.close()):this.beginDrawing(t))}onMouseMove(t){this.endPosition=[t.lng,t.lat],this.updateRectangle(this.endPosition,s.Provisional)}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&this.close()}onDragStart(t,e){"drawing"!==this.state&&this.allowPointerEvent(this.pointerEvents.onDragStart,t)&&this.dragDrawAllowed()&&(this.beginDrawing(t,"drag"),e(!1))}onDrag(t,e){this.allowPointerEvent(this.pointerEvents.onDrag,t)&&this.dragDrawAllowed()&&"drag"===this.drawType&&(this.endPosition=[t.lng,t.lat],this.updateRectangle(this.endPosition,s.Provisional))}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&this.dragDrawAllowed()&&"drag"===this.drawType&&(this.endPosition=[t.lng,t.lat],this.close(),e(!0))}cleanUp(){const t=this.currentRectangleId;this.startPosition=void 0,this.currentRectangleId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted(),t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t)}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});return"Feature"===e.type&&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.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.outlineOpacity,1,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&&e.properties.mode===this.mode&&"Point"===e.geometry.type&&e.properties[d.COORDINATE_POINT]?(i.pointWidth=this.getNumericStylingValue(this.styles.coordinatePointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.coordinatePointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.coordinatePointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.coordinatePointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.coordinatePointOutlineOpacity,1,e),i.zIndex=l,i):i}validateFeature(t){return this.validateModeFeature(t,t=>ot(t,this.coordinatePrecision))}afterFeatureUpdated(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.currentRectangleId===t.id&&(this.startPosition=void 0,this.currentRectangleId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted())}afterFeatureAdded(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}registerBehaviors(t){this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature)}}class Ie extends x{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)}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),pointOpacity:this.getNumericStylingValue(this.styles.pointOpacity,1,t),pointOutlineColor:this.getHexColorStylingValue(this.styles.pointOutlineColor,"#ffffff",t),pointOutlineWidth:this.getNumericStylingValue(this.styles.pointOutlineWidth,0,t),pointOutlineOpacity:this.getNumericStylingValue(this.styles.pointOutlineOpacity,1,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),lineStringOpacity:this.getNumericStylingValue(this.styles.lineStringOpacity,1,t),zIndex:this.getNumericStylingValue(this.styles.zIndex,0,t),lineStringDash:void 0}}validateFeature(t){const e=super.validateFeature(t);if(e.valid){const e=t,i=ue(e,this.coordinatePrecision).valid||it(e,this.coordinatePrecision).valid||Jt(e,this.coordinatePrecision).valid;return i?{valid:!0}:{valid:i,reason:"Feature is not a valid Point, Polygon or LineString feature"}}return e}}function Se(t,e){const i=t,o=e,n=W(i[1]),s=W(o[1]);let r=W(o[0]-i[0]);r>Math.PI&&(r-=2*Math.PI),r<-Math.PI&&(r+=2*Math.PI);const a=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(n/2+Math.PI/4)),d=(U(Math.atan2(r,a))+360)%360;return d>180?-(360-d):d}function Fe(t,e,i){let o=e;e<0&&(o=-Math.abs(o));const n=o/R,s=t[0]*Math.PI/180,r=W(t[1]),a=W(i),d=n*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)),u=Math.abs(l)>1e-11?d/l:Math.cos(r),c=[(180*(s+n*Math.sin(a)/u)/Math.PI+540)%360-180,180*h/Math.PI];return c[0]+=c[0]-t[0]>180?-360:t[0]-c[0]>180?360:0,c}function Oe(t,e,i,o,n){const s=o(t[0],t[1]),r=o(e[0],e[1]),{lng:a,lat:d}=n((s.x+r.x)/2,(s.y+r.y)/2);return[L(a,i),L(d,i)]}function xe(t,e,i){const o=Fe(t,1e3*T(t,e)/2,Se(t,e));return[L(o[0],i),L(o[1],i)]}function we({featureCoords:t,precision:e,unproject:i,project:o,projection:n}){const s=[];for(let r=0;r{const n=this.readFeature.getGeometry(o),s=this.pixelDistance.measure(t,n.coordinates);s({mode:this.mode,midPointSegment:t,midPointFeatureId:e,[a.MID_POINT]:!0}),coordinates:o,type:a.MID_POINT})}delete(){this._midPoints.length&&(this.mutateFeature.deleteFeaturesIfPresent(this._midPoints),this._midPoints=[])}updateAllInPlace({featureCoordinates:t}){if(0===this._midPoints.length)return;const e=mt(t),i=we(this.getMidpointConfig(e));this.mutateFeature.updateGuidancePoints(this._midPoints.map((t,e)=>({featureId:t,coordinate:i[e]})))}updateOneAtIndex(t,e){if(t<0&&(t=this._midPoints.length+t),void 0===this._midPoints[t])return;const i=mt(e),o=we(this.getMidpointConfig(i));this.mutateFeature.updateGuidancePoints([{featureId:this._midPoints[t],coordinate:o[t]}])}}class Ee extends nt{constructor(t,e){super(t),this.mutateFeature=void 0,this._selectionPoints=[],this.mutateFeature=e}get ids(){return this._selectionPoints.concat()}set ids(t){}create({featureId:t,featureCoordinates:e}){const i=yt(e);this._selectionPoints=this.mutateFeature.createGuidancePoints({coordinates:i,type:a.SELECTION_POINT,additionalProperties:e=>({[a.SELECTION_POINT_FEATURE_ID]:t,index:e})})}delete(){this.ids.length&&(this.mutateFeature.deleteFeaturesIfPresent(this.ids),this._selectionPoints=[])}updateAllInPlace({featureCoordinates:t}){if(0===this._selectionPoints.length)return;const e=yt(t);e.length===this._selectionPoints.length&&this.mutateFeature.updateGuidancePoints(this._selectionPoints.map((t,i)=>({featureId:t,coordinate:e[i]})))}updateOneAtIndex(t,e){void 0!==this._selectionPoints[t]&&this.mutateFeature.updateGuidancePoints([{featureId:this._selectionPoints[t],coordinate:e}])}}function De(t,e){let i=!1;for(let r=0,a=e.length;r(o=t)[1]!=(s=a[d])[1]>o[1]&&o[0]<(s[0]-n[0])*(o[1]-n[1])/(s[1]-n[1])+n[0]&&(i=!i)}var o,n,s;return i}const ke=(t,e,i)=>{const o=t=>t*t,n=(t,e)=>o(t.x-e.x)+o(t.y-e.y);return Math.sqrt(((t,e,i)=>{const o=n(e,i);if(0===o)return n(t,e);let s=((t.x-e.x)*(i.x-e.x)+(t.y-e.y)*(i.y-e.y))/o;return s=Math.max(0,Math.min(1,s)),n(t,{x:e.x+s*(i.x-e.x),y:e.y+s*(i.y-e.y)})})(t,e,i))};class Ne extends nt{constructor(t,e,i){super(t),this.config=void 0,this.createClickBoundingBox=void 0,this.pixelDistance=void 0,this.config=t,this.createClickBoundingBox=e,this.pixelDistance=i}find(t,e){let i,o,n,s=Infinity,r=Infinity;const d=this.createClickBoundingBox.create(t),h=this.store.search(d);for(let d=0;d180||n<-180||s>90||s<-90)return!1;o[t]=[n,s]}"Polygon"===e.type&&(o[o.length-1]=[o[0][0],o[0][1]]);const r=this.draggedFeatureId;let a=null;if("Polygon"===e.type)a=this.mutateFeature.updatePolygon({featureId:r,coordinateMutations:{type:lt,coordinates:[o]},context:{updateType:s.Provisional}});else{if("LineString"!==e.type)return;a=this.mutateFeature.updateLineString({featureId:r,coordinateMutations:{type:lt,coordinates:o},context:{updateType:s.Provisional}})}if(!a)return!1;const d=a.geometry.coordinates;this.midPoints.updateAllInPlace({featureCoordinates:d}),this.selectionPoints.updateAllInPlace({featureCoordinates:d}),this.coordinatePoints.updateAllInPlace({featureId:r,featureCoordinates:d}),this.dragPosition=[t.lng,t.lat]}else"Point"===e.type&&(this.mutateFeature.updatePoint({featureId:this.draggedFeatureId,coordinateMutations:{type:lt,coordinates:i},context:{updateType:s.Provisional}}),this.dragPosition=[t.lng,t.lat])}}class _e extends nt{constructor(t,e,i,o,n,s,r,a,d){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.readFeature=void 0,this.mutateFeature=void 0,this.featureSnapping=void 0,this.draggedCoordinate={id:null,index:-1},this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o,this.coordinatePoints=n,this.coordinateSnapping=s,this.lineSnapping=r,this.readFeature=a,this.mutateFeature=d,this.featureSnapping=new kt(this.coordinateSnapping,this.lineSnapping)}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 n=0;nBoolean(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,n).coordinate,e&&(o=e)}if(e.toCoordinate){let e;e=this.coordinateSnapping.getSnappable(t,n).coordinate,e&&(o=e)}if(null!=e&&e.toFeature){const n=this.featureSnapping.getSnappable(t,i.id,e.toFeature.filter,{toLine:e.toFeature.toLine,toCoordinate:e.toFeature.toCoordinate});n.coordinate&&(o=n.coordinate)}if(null!=e&&e.toCustom){let n;n=e.toCustom(t,{currentCoordinate:this.draggedCoordinate.index,currentId:i.id,getCurrentGeometrySnapshot:i.id?()=>this.readFeature.getGeometry(i.id):()=>null,project:this.project,unproject:this.unproject}),n&&(o=n)}return o}drag(t,e,i){const o=this.draggedCoordinate.id;if(null===o)return!1;if(!1===this.readFeature.hasFeature(o))return this.stopDragging(),!1;const n=this.draggedCoordinate.index,r=this.readFeature.getGeometry(o),a=this.readFeature.getProperties(o),d="LineString"===r.type?r.coordinates:r.coordinates[0],h="Polygon"===r.type&&(n===d.length-1||0===n),l=this.snapCoordinate(t,i,{type:"Feature",id:o,geometry:r,properties:a});if(t.lng>180||t.lng<-180||t.lat>90||t.lat<-90)return!1;if(h){const t=d.length-1;d[0]=l,d[t]=l}else d[n]=l;if("Point"!==r.type&&!e&&Y({type:"Feature",geometry:r,properties:{}}))return!1;const u=o;let c=null;return"Polygon"===r.type?c=this.mutateFeature.updatePolygon({featureId:u,coordinateMutations:{type:lt,coordinates:[d]},context:{updateType:s.Provisional}}):"LineString"===r.type&&(c=this.mutateFeature.updateLineString({featureId:u,coordinateMutations:{type:lt,coordinates:d},context:{updateType:s.Provisional}})),!!c&&(this.midPoints.updateOneAtIndex(n>0?n-1:-1,d),this.midPoints.updateOneAtIndex(n,d),this.selectionPoints.updateOneAtIndex(n,l),this.coordinatePoints.updateOneAtIndex(u,n,l),!0)}isDragging(){return null!==this.draggedCoordinate.id}startDragging(t,e){this.draggedCoordinate={id:t,index:e}}stopDragging(){this.draggedCoordinate={id:null,index:-1}}}function Te(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]}const Re=(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])=>H(t,e)),n=o.reduce((t,e)=>({x:t.x+e.x,y:t.y+e.y}),{x:0,y:0});n.x/=o.length,n.y/=o.length;const s=o.map(t=>({x:n.x+(t.x-n.x)*Math.cos(i)-(t.y-n.y)*Math.sin(i),y:n.y+(t.x-n.x)*Math.sin(i)+(t.y-n.y)*Math.cos(i)})).map(({x:t,y:e})=>[G(t,e).lng,G(t,e).lat]);return"Polygon"===t.geometry.type?t.geometry.coordinates[0]=s:t.geometry.coordinates=s,t};function We(t){const e=("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).map(t=>{const{x:e,y:i}=H(t[0],t[1]);return[e,i]});return"Polygon"===t.geometry.type?function(t){let e=0,i=0,o=0;const n=t.length;for(let s=0;s{const o=Se(i,t)+e,n=function(t,e){t[0]+=t[0]-e[0]>180?-360:e[0]-t[0]>180?360:0;const i=R,o=e[1]*Math.PI/180,n=t[1]*Math.PI/180,s=n-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(n/2+Math.PI/4)/Math.tan(o/2+Math.PI/4)),d=Math.abs(a)>1e-11?s/a:Math.cos(o);return Math.sqrt(s*s+d*d*r*r)*i}(i,t),s=Fe(i,n,o);t[0]=s[0],t[1]=s[1]})}(r,-(this.lastBearing-(n+180)))}const a="Polygon"===i.type?i.coordinates[0]:i.coordinates;a.forEach(t=>{t[0]=L(t[0],this.coordinatePrecision),t[1]=L(t[1],this.coordinatePrecision)});const d={featureId:e,coordinateMutations:{type:lt,coordinates:"Polygon"===i.type?[a]:a},context:{updateType:s.Provisional}};let h=null;if("Polygon"===r.geometry.type)h=this.mutateFeature.updatePolygon(d);else{if("LineString"!==r.geometry.type)return;h=this.mutateFeature.updateLineString(d)}if(!h)return!1;const l=h.geometry.coordinates;this.midPoints.updateAllInPlace({featureCoordinates:l}),this.selectionPoints.updateAllInPlace({featureCoordinates:l}),this.coordinatePoints.updateAllInPlace({featureId:e,featureCoordinates:l}),"web-mercator"===this.projection?this.lastBearing=n:"globe"===this.projection&&(this.lastBearing=n+180)}}class Ue extends nt{constructor(t,e){super(t),this.config=void 0,this.dragCoordinateResizeBehavior=void 0,this.config=t,this.dragCoordinateResizeBehavior=e}scale(t,e){if(!this.dragCoordinateResizeBehavior.isDragging()){const i=this.dragCoordinateResizeBehavior.getDraggableIndex(t,e);this.dragCoordinateResizeBehavior.startDragging(e,i)}this.dragCoordinateResizeBehavior.drag(t,"center-fixed")}reset(){this.dragCoordinateResizeBehavior.stopDragging()}}function Le({coordinates:t,originX:e,originY:i,xScale:o,yScale:n}){1===o&&1===n||t.forEach(t=>{const{x:s,y:r}=H(t[0],t[1]),a=e+(s-e)*o,d=i+(r-i)*n,{lng:h,lat:l}=G(a,d);t[0]=h,t[1]=l})}class Be extends nt{constructor(t,e,i,o,n,s,r){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.readFeature=void 0,this.mutateFeature=void 0,this.minimumScale=1e-4,this.draggedCoordinate={id:null,index:-1},this.centerOrigin=null,this.oppositeOrigin=null,this.oppositeClosestBBoxIndex=null,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=n,this.readFeature=s,this.mutateFeature=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 n=0;n=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(null===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){var e;const i=this.getSelectedFeatureDataWebMercator();if(!i)return null;const{feature:o,boundingBox:n,updatedCoords:s,selectedCoordinate:r}=i,a=null!=(e=this.centerOrigin)?e:We(o);if(!a||!Number.isFinite(a.x)||!Number.isFinite(a.y))return null;this.centerOrigin=a;const d=H(r[0],r[1]),{closestBBoxIndex:h}=this.getIndexesWebMercator(n,d),l=H(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:h,updatedCoords:s,webMercatorCursor:l,webMercatorSelected:d,webMercatorOrigin:a})}centerFixedWebMercatorDrag(t){var e;const i=this.getSelectedFeatureDataWebMercator();if(!i)return null;const{feature:o,boundingBox:n,updatedCoords:s,selectedCoordinate:r}=i,a=null!=(e=this.centerOrigin)?e:We(o);if(!a||!Number.isFinite(a.x)||!Number.isFinite(a.y))return null;this.centerOrigin=a;const d=H(r[0],r[1]),{closestBBoxIndex:h}=this.getIndexesWebMercator(n,d),l=H(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:h,updatedCoords:s,webMercatorCursor:l,webMercatorSelected:d,webMercatorOrigin:a})}scaleFixedWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:i,webMercatorCursor:o,updatedCoords:n}){if(!this.isValidDragWebMercator(t,e.x-o.x,e.y-o.y))return null;let s=Ct(e,o)/Ct(e,i);return Number.isFinite(s)?(s{const{x:s,y:r}=H(t[0],t[1]),a=e+(s-e)*o,d=i+(r-i)*n,{lng:h,lat:l}=G(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}=H(t[0],t[1]);return[e,i]})).forEach(([t,i])=>{te[2]&&(e[2]=t),i>e[3]&&(e[3]=i)});const[i,o,n,s]=e;return[[i,s],[(i+n)/2,s],[n,s],[n,s+(o-s)/2],[n,o],[(i+n)/2,o],[i,o],[i,s+(o-s)/2]]}getIndexesWebMercator(t,e){let i,o=Infinity;for(let n=0;n{const i=t.properties.mode;return this.validations&&this.validations[i]?this.validations[i](t,e):{valid:!0}}}),this.pixelDistance=new Pt(t),this.clickBoundingBox=new It(t),this.featuresAtMouseEvent=new Ne(t,this.clickBoundingBox,this.pixelDistance),this.selectionPoints=new Ee(t,this.mutateFeature),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature),this.midPoints=new Me(t,this.selectionPoints,this.coordinatePoints,this.mutateFeature,this.readFeature,this.pixelDistance),this.coordinateSnap=new St(t,this.pixelDistance,this.clickBoundingBox),this.lineSnap=new Dt(t,this.pixelDistance,this.clickBoundingBox),this.rotateFeature=new Ae(t,this.selectionPoints,this.midPoints,this.coordinatePoints,this.readFeature,this.mutateFeature),this.dragFeature=new be(t,this.featuresAtMouseEvent,this.selectionPoints,this.midPoints,this.coordinatePoints,this.readFeature,this.mutateFeature),this.dragCoordinate=new _e(t,this.pixelDistance,this.selectionPoints,this.midPoints,this.coordinatePoints,this.coordinateSnap,this.lineSnap,this.readFeature,this.mutateFeature),this.dragCoordinateResizeFeature=new Be(t,this.pixelDistance,this.selectionPoints,this.midPoints,this.coordinatePoints,this.readFeature,this.mutateFeature),this.scaleFeature=new Ue(t,this.dragCoordinateResizeFeature)}deselectFeature(t){this.deselect(t)}deselect(t){this.selected.includes(t)&&(this.mutateFeature.setDeselected(this.selected),this.onDeselect(this.selected[0]),this.selected=[],this.selectionPoints.delete(),this.midPoints.delete(),this.dragTarget={type:"none"})}deleteSelected(){this.selected.length&&this.mutateFeature.deleteFeaturesIfPresent(this.selected),this.selected=[],this.dragTarget={type:"none"}}clearDragTargetAndCursor(){this.dragTarget={type:"none"},this.setCursor(_)}getSelectedFlags(t){const e=this.readFeature.getProperties(t),i=this.flags[e.mode],o=null==i?void 0:i.feature,n=null==o?void 0:o.coordinates;return{featureFlags:o,coordinatesFlags:n,hasDraggableFlags:o&&(o.draggable||(null==n?void 0:n.draggable)||(null==n?void 0:n.resizable)||"object"==typeof(null==n?void 0:n.midpoints)&&n.midpoints.draggable)}}onRightClick(t){if(!this.selectionPoints.ids.length)return;let e,i=Infinity;if(this.selectionPoints.ids.forEach(o=>{const n=this.readFeature.getGeometry(o),s=this.pixelDistance.measure(t,n.coordinates);s0),i=this.midPoints.getNearestMidPoint(t),o=this.selected[0];if(o){var s;const{featureFlags:e}=this.getSelectedFlags(o);if(null!=e&&null!=(s=e.coordinates)&&s.midpoints&&i){if(e.coordinates.draggable){const e=this.pixelDistance.measure(t,this.readFeature.getGeometry(i).coordinates),{dist:n}=this.dragCoordinate.getDraggable(t,o);if(void 0!==n&&e>n)return}return this.midPoints.insert({featureId:o,midPointId:i}),void this.onFinish(this.selected[0],{action:n,mode:this.mode})}}if(null!=e&&e.id)this.allowManualSelection&&this.select(e.id,!0);else if(this.selected.length&&this.allowManualDeselection)return void this.deselect(this.selected[0])}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(this.selected[0])}onDragStart(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDragStart,t))return;const i=this.selected[0];if(!i)return;const{featureFlags:o,coordinatesFlags:s,hasDraggableFlags:r}=this.getSelectedFlags(i);if(!r)return;this.dragEventCount=0;const a="none"!==this.dragTarget.type&&this.dragTarget.featureId===i?this.dragTarget:{type:"none"},d="coordinate"===a.type?a.coordinateIndex:this.dragCoordinate.getDraggableIndex(t,i),h="resize"===a.type?a.coordinateIndex:this.dragCoordinateResizeFeature.getDraggableIndex(t,i),l=(null==s?void 0:s.resizable)&&-1!==h,u=(null==s?void 0:s.draggable)&&-1!==d,c=s&&"object"==typeof s.midpoints&&s.midpoints.draggable,p=(null==o?void 0:o.draggable)&&("feature"===a.type||this.dragFeature.canDrag(t,i));if(l)return this.setCursor(this.cursors.dragStart),this.dragCoordinateResizeFeature.startDragging(i,h),void e(!1);if(u)return this.setCursor(this.cursors.dragStart),this.dragCoordinate.startDragging(i,d),void e(!1);if(c){const o="midpoint"===a.type?a.midPointId:this.midPoints.getNearestMidPoint(t);if(this.selected.length&&o){this.midPoints.insert({featureId:i,midPointId:o}),this.onFinish(this.selected[0],{action:n,mode:this.mode});const s=this.dragCoordinate.getDraggableIndex(t,i);return this.dragCoordinate.startDragging(i,s),void e(!1)}}if(p)return this.setCursor(this.cursors.dragStart),this.dragFeature.startDragging(t,i),void e(!1);this.setCursor(_)}onDrag(t,e){if(!this.allowPointerEvent(this.pointerEvents.onDrag,t))return;const i=this.selected[0];if(!i)return;const o=this.readFeature.getProperties(i),n=this.flags[o.mode],s=!0===(n&&n.feature&&n.feature.selfIntersectable);if(this.dragEventCount++,this.dragEventCount%this.dragEventThrottle!=0){if(n&&n.feature&&n.feature.rotateable&&this.canRotate(t))return e(!1),void this.rotateFeature.rotate(t,i);if(n&&n.feature&&n.feature.scaleable&&this.canScale(t))return e(!1),void this.scaleFeature.scale(t,i);if(this.dragCoordinateResizeFeature.isDragging()&&n.feature&&n.feature.coordinates&&n.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,n.feature.coordinates.resizable)}if(this.dragCoordinate.isDragging()){var r;const e=null==(r=n.feature)||null==(r=r.coordinates)?void 0:r.snappable;let i={toCoordinate:!1};return!0===e?i={toCoordinate:!0}:"object"==typeof e&&(i=e),void this.dragCoordinate.drag(t,s,i)}this.dragFeature.isDragging()?this.dragFeature.drag(t):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){const e=this.selected[0];if(!e)return void this.clearDragTargetAndCursor();if(this.dragFeature.isDragging()||this.dragCoordinate.isDragging()||this.dragCoordinateResizeFeature.isDragging())return;const{featureFlags:i}=this.getSelectedFlags(e);if(!i)return void this.clearDragTargetAndCursor();let o;const n=i.coordinates;if(null!=n&&n.midpoints&&(o=this.midPoints.getNearestMidPoint(t),o&&(this.dragTarget={type:"midpoint",featureId:e,midPointId:o},this.setCursor(this.cursors.insertMidpoint))),n&&n.draggable){const{index:i,dist:n}=this.dragCoordinate.getDraggable(t,e);if(i>-1){if(o&&this.pixelDistance.measure(t,this.readFeature.getGeometry(o).coordinates)-1)return this.dragTarget={type:"resize",featureId:e,coordinateIndex:i},void this.setCursor(this.getPointerOverResizeHandleCursor())}if(i.draggable&&this.dragFeature.canDrag(t,e)){if(o)return;return this.dragTarget={type:"feature",featureId:e},void this.setCursor(this.getPointerOverFeatureCursor())}o||this.clearDragTargetAndCursor()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});if(e.properties.mode===this.mode&&"Point"===e.geometry.type){if(e.properties[a.SELECTION_POINT])return i.pointColor=this.getHexColorStylingValue(this.styles.selectionPointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.selectionPointOpacity,1,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectionPointOutlineColor,i.pointOutlineColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.selectionPointWidth,i.pointWidth,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.selectionPointOutlineOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectionPointOutlineWidth,2,e),i.zIndex=u,i;if(e.properties[a.MID_POINT])return i.pointColor=this.getHexColorStylingValue(this.styles.midPointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.midPointOpacity,1,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.midPointOutlineColor,i.pointOutlineColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.midPointWidth,4,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.midPointOutlineOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.midPointOutlineWidth,2,e),i.zIndex=50,i}else if(e.properties[a.SELECTED]){if("Point"===e.geometry.type&&e.properties[d.MARKER])return i.markerUrl=this.getUrlStylingValue(this.styles.selectedMarkerUrl,r,e),i.markerHeight=this.getNumericStylingValue(this.styles.selectedMarkerHeight,40,e),i.markerWidth=this.getNumericStylingValue(this.styles.selectedMarkerWidth,32,e),i.markerAnchor=this.getMarkerAnchorStylingValue(this.styles.selectedMarkerAnchor,"bottom",e),i;if("Polygon"===e.geometry.type)return i.polygonFillColor=this.getHexColorStylingValue(this.styles.selectedPolygonColor,i.polygonFillColor,e),i.polygonOutlineWidth=this.getNumericStylingValue(this.styles.selectedPolygonOutlineWidth,i.polygonOutlineWidth,e),i.polygonOutlineColor=this.getHexColorStylingValue(this.styles.selectedPolygonOutlineColor,i.polygonOutlineColor,e),i.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.selectedPolygonOutlineOpacity,1,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.selectedPolygonFillOpacity,i.polygonFillOpacity,e),i.zIndex=h,i;if("LineString"===e.geometry.type)return i.lineStringColor=this.getHexColorStylingValue(this.styles.selectedLineStringColor,i.lineStringColor,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.selectedLineStringWidth,i.lineStringWidth,e),i.lineStringOpacity=this.getNumericStylingValue(this.styles.selectedLineStringOpacity,1,e),i.lineStringDash=this.getDashArrayStylingValue(this.styles.selectedLineStringDash,void 0,e),i.zIndex=h,i;if("Point"===e.geometry.type)return i.pointWidth=this.getNumericStylingValue(this.styles.selectedPointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.selectedPointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.selectedPointOpacity,1,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.selectedPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.selectedPointOutlineOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.selectedPointOutlineWidth,i.pointOutlineWidth,e),i.zIndex=h,i}return i}afterFeatureUpdated(t){if(this.selected.length&&t.id===this.selected[0]){var e,i;const o=this.flags[t.properties.mode];if(null==o||null==(e=o.feature)||!e.coordinates)return;const n=t.geometry.type,s=t.id;if(this.selectionPoints.delete(),this.midPoints.delete(),"LineString"!==n&&"Polygon"!==n)return;const r=t.geometry.coordinates;this.selectionPoints.create({featureCoordinates:r,featureId:s}),null!=o&&null!=(i=o.feature)&&null!=(i=i.coordinates)&&i.midpoints&&this.midPoints.create({featureCoordinates:r,featureId:s})}}}class Ge extends x{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,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0})}}function je(t,e,i,o,n){for(;o>i;){if(o-i>600){const s=o-i+1,r=e-i+1,a=Math.log(s),d=.5*Math.exp(2*a/3),h=.5*Math.sqrt(a*d*(s-d)/s)*(r-s/2<0?-1:1);je(t,e,Math.max(i,Math.floor(e-r*d/s+h)),Math.min(o,Math.floor(e+(s-r)*d/s+h)),n)}const s=t[e];let r=i,a=o;for(Ke(t,i,e),n(t[o],s)>0&&Ke(t,i,o);r0;)a--}0===n(t[i],s)?Ke(t,i,a):(a++,Ke(t,a,o)),a<=e&&(i=a+1),e<=a&&(o=a-1)}}function Ke(t,e,i){const o=t[e];t[e]=t[i],t[i]=o}function Ye(t,e){Xe(t,0,t.children.length,e,t)}function Xe(t,e,i,o,n){n||(n=oi([])),n.minX=Infinity,n.minY=Infinity,n.maxX=-Infinity,n.maxY=-Infinity;for(let s=e;s=t.minX&&e.maxY>=t.minY}function oi(t){return{children:t,height:1,leaf:!0,minX:Infinity,minY:Infinity,maxX:-Infinity,maxY:-Infinity}}function ni(t,e,i,o,n){const s=[e,i];for(;s.length;){if((i=s.pop())-(e=s.pop())<=o)continue;const r=e+Math.ceil((i-e)/o/2)*o;je(t,r,e,i,n),s.push(e,r,r,i)}}class si{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(!ii(t,e))return i;const o=this.toBBox,n=[];for(;e;){for(let s=0;s=0&&n[e].children.length>this._maxEntries;)this._split(n,e),e--;this._adjustParentBBoxes(o,n,e)}_split(t,e){const i=t[e],o=i.children.length,n=this._minEntries;this._chooseSplitAxis(i,n,o);const s=this._chooseSplitIndex(i,n,o),r=oi(i.children.splice(s,i.children.length-s));r.height=i.height,r.leaf=i.leaf,Ye(i,this.toBBox),Ye(r,this.toBBox),e?t[e-1].children.push(r):this._splitRoot(i,r)}_splitRoot(t,e){this.data=oi([t,e]),this.data.height=t.height+1,this.data.leaf=!1,Ye(this.data,this.toBBox)}_chooseSplitIndex(t,e,i){let o,n=Infinity,s=Infinity;for(let r=e;r<=i-e;r++){const e=Xe(t,0,r,this.toBBox),a=Xe(t,r,i,this.toBBox),d=ti(e,a),h=Je(e)+Je(a);d=e;o--){const e=t.children[o];$e(r,t.leaf?n(e):e),a+=Qe(r)}return a}_adjustParentBBoxes(t,e,i){for(let o=i;o>=0;o--)$e(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():Ye(t[i],this.toBBox)}}class ri{constructor(t){this.tree=void 0,this.idToNode=void 0,this.nodeToId=void 0,this.tree=new si(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{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 ai={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},di={target:"geometry"},hi={target:"properties"};class li{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 ri,this.tracked=!t||!1!==t.tracked,this.idStrategy=t&&t.idStrategy?t.idStrategy:ai}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 n=this.clone(t);const s=[],r=[];n=n.filter(t=>{null==t.id&&(t.id=this.idStrategy.getId());const i=t.id;if(e){const o=e(t);if(!o.valid)return s.push({id:i,valid:!1,reason:o.reason}),!1}if(this.tracked){if(t.properties.createdAt){if(!g(t.properties.createdAt))return s.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(!g(t.properties.updatedAt))return s.push({id:t.id,valid:!1,reason:"updatedAt is not a valid numeric timestamp"}),!1}else t.properties.updatedAt=+new Date}return this.has(i)?(s.push({id:i,valid:!1,reason:`Feature already exists with this id: ${i}`}),!1):(this.store[i]=t,r.push(t),s.push({id:i,valid:!0}),!0)}),this.spatialIndex.load(n);const a=r.map(({id:t})=>t);return a.length>0&&(this._onChange(a,"create",o),i&&r.forEach(t=>{i(t)})),s}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(e,i){const o=new Set;e.forEach(({id:t,property:e,value:i})=>{const n=this.store[t];if(!n)throw new Error(`No feature with this (${t}), can not update geometry`);n.properties[e]!==i&&(o.add(t),void 0===i?delete n.properties[e]:n.properties[e]=i,this.tracked&&(n.properties.updatedAt=+new Date))}),this._onChange&&o.size>0&&this._onChange(Array.from(o),"update",i?t({},i,hi):hi)}updateGeometry(e,i){const o=new Set;e.forEach(({id:t,geometry:e})=>{o.add(t);const i=this.store[t];if(!i)throw new Error(`No feature with this (${t}), can not update geometry`);i.geometry=this.clone(e),this.spatialIndex.update(i),this.tracked&&(i.properties.updatedAt=+new Date)}),this._onChange&&o.size>0&&this._onChange(Array.from(o),"update",i?t({},i,di):di)}create(e,i){const o=[];return e.forEach(({geometry:e,properties:i})=>{let n,s=t({},i);this.tracked&&(n=+new Date,i?(s.createdAt="number"==typeof i.createdAt?i.createdAt:n,s.updatedAt="number"==typeof i.updatedAt?i.updatedAt:n):s={createdAt:n,updatedAt:n});const r=this.getId(),a={id:r,type:"Feature",geometry:e,properties:s};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(t){const e=Object.keys(this.store);this.store={},this.spatialIndex.clear(),this._onChange(e,"delete",t)}size(){return Object.keys(this.store).length}}function ui(t){const e=t.coordinates;let i=0;if(e&&e.length>0){i+=Math.abs(gi(e[0]));for(let t=1;t=e?(o+2)%e:o+2][0]*pi-t[o][0]*pi)*Math.sin(t[o+1===e?0:o+1][1]*pi),o++;return i*ci}const yi="Feature is smaller than the minimum area",mi=(t,e)=>"Polygon"!==t.geometry.type?{valid:!1,reason:y}:ui(t.geometry)"Polygon"!==t.geometry.type?{valid:!1,reason:y}:ui(t.geometry)>e?{valid:!1,reason:"Feature is larger than the maximum area"}:{valid:!0},Ci="Feature is not a Polygon or LineString",Pi="Feature intersects itself",vi=t=>"Polygon"!==t.geometry.type&&"LineString"!==t.geometry.type?{valid:!1,reason:Ci}:Y(t)?{valid:!1,reason:Pi}:{valid:!0};function Ii(t,e,i){const o=Kt(t,e);let n=Kt(e,i)-o;return n<0&&(n+=360),180-Math.abs(n-90-90)}const Si={cancel:E,finish:M},Fi={start:D,close:k};class Oi extends x{constructor(t){super(t,!0),this.mode="angled-rectangle",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=Si,this.cursors=Fi,this.mouseMove=!1,this.showCoordinatePoints=!1,this.mutateFeature=void 0,this.readFeature=void 0,this.coordinatePoints=void 0,this.updateOptions(t)}updateOptions(e){var i;super.updateOptions(e),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),C(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints))}close(){if(void 0===this.currentId)return;const t=this.mutateFeature.updatePolygon({featureId:this.currentId,propertyMutations:{[d.CURRENTLY_DRAWING]:void 0},context:{updateType:s.Finish,action:e}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates});const i=this.currentId;this.currentCoordinate=0,this.currentId=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(i,{mode:this.mode,action:e})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}onMouseMove(t){if(this.mouseMove=!0,this.setCursor(this.cursors.start),void 0===this.currentId||0===this.currentCoordinate)return;let e=[];if(1===this.currentCoordinate)e=this.getUpdateForSecondCoordinate(t);else{if(2!==this.currentCoordinate)return;e=this.getNewSecondAndThirdCoordinates(t)}const i=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:e,context:{updateType:s.Provisional}});i&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:i.geometry.coordinates})}getUpdateForSecondCoordinate(t){return[{type:dt,index:1,coordinate:[t.lng,t.lat]},{type:dt,index:2,coordinate:[t.lng,t.lat]}]}getNewSecondAndThirdCoordinates(t){if(!this.currentId)throw new Error("No current feature being drawn");const e=this.readFeature.getCoordinate(this.currentId,0),i=this.readFeature.getCoordinate(this.currentId,1),o=Oe(e,i,this.coordinatePrecision,this.project,this.unproject),n=H(e[0],e[1]),s=H(o[0],o[1]),r=H(i[0],i[1]),a=H(t.lng,t.lat),d=Ct(a,n)1e-10?"left":o<-1e-10?"right":"left"}(n,r,a)?-90:90),g=Rt(n,c,p),y=Rt(r,c,p),m=G(g.x,g.y),f=G(y.x,y.y);return[{type:dt,index:2,coordinate:[L(f.lng,this.coordinatePrecision),L(f.lat,this.coordinatePrecision)]},{type:dt,index:3,coordinate:[L(m.lng,this.coordinatePrecision),L(m.lat,this.coordinatePrecision)]}]}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.mutateFeature.createPolygon({coordinates:[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]],properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});this.currentId=e.id,this.currentCoordinate++,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:e.id,featureCoordinates:e.geometry.coordinates}),this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){const e=this.readFeature.getCoordinate(this.currentId,0);if(ct([t.lng,t.lat],e))return;const i=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:[{type:dt,index:1,coordinate:[t.lng,t.lat]},{type:at,index:1,coordinate:[t.lng,t.lat]}],context:{updateType:s.Commit}});if(!i)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:i.geometry.coordinates}),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(){const t=this.currentId;this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted(),t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t)}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 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.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.outlineOpacity,1,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=h):"Point"===e.geometry.type&&e.properties[d.COORDINATE_POINT]&&(i.pointWidth=this.getNumericStylingValue(this.styles.coordinatePointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.coordinatePointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.coordinatePointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.coordinatePointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.coordinatePointOutlineOpacity,1,e),i.zIndex=l)),i}validateFeature(t){return this.validateModeFeature(t,t=>ot(t,this.coordinatePrecision))}afterFeatureUpdated(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.currentId===t.id&&(this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted())}afterFeatureAdded(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}registerBehaviors(t){this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature)}}function xi(t,e,i,o){const n=("anticlockwise"===t?Yt(i-e):-Yt(e-i))+(Yt(o-i+180)-180);return"anticlockwise"===t&&n<0?"clockwise":"clockwise"===t&&n>0?"anticlockwise":t}function wi(t,e,i){return(e.x-t.x)*(i.y-t.y)-(e.y-t.y)*(i.x-t.x)<=0}const Mi={cancel:E,finish:M},Ei={start:D,close:k};class Di extends x{constructor(t){super(t,!0),this.mode="sector",this.snapping=void 0,this.snappedPointId=void 0,this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=Mi,this.direction=void 0,this.arcPoints=64,this.cursors=Ei,this.mouseMove=!1,this.showCoordinatePoints=!1,this.allowDirectionChange=!1,this.previousEndBearing=void 0,this.coordinateSnapping=void 0,this.featureSnapping=void 0,this.readFeature=void 0,this.mutateFeature=void 0,this.coordinatePoints=void 0,this.updateOptions(t)}updateOptions(e){var i;super.updateOptions(e),v(null==e?void 0:e.snapping)&&(this.snapping=e.snapping),C(null==e?void 0:e.allowDirectionChange)&&(this.allowDirectionChange=e.allowDirectionChange),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),f(null==e?void 0:e.arcPoints)&&e.arcPoints>0&&(this.arcPoints=e.arcPoints),C(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints))}updateSnappedCoordinate(t){var e;t&&!1!==(null==(e=this.snapping)?void 0:e.showSnappingPoint)?this.snappedPointId?this.mutateFeature.updateGuidancePoints([{featureId:this.snappedPointId,coordinate:t}]):this.snappedPointId=this.mutateFeature.createGuidancePoint({coordinate:t,type:d.SNAPPING_POINT}):this.deleteSnappedPoint()}deleteSnappedPoint(){this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0)}getSnappedCoordinate(t){var e,i,o;if(this.currentCoordinate>=2)return;const n=this.currentId;let s;if(null!=(e=this.snapping)&&e.toCoordinate){const e=void 0!==n?this.coordinateSnapping.getSnappableCoordinate(t,n):this.coordinateSnapping.getSnappableCoordinateFirstClick(t);e&&(s=e)}if(null!=(i=this.snapping)&&i.toFeature){const e=this.featureSnapping.getSnappable(t,n,this.snapping.toFeature.filter,{toLine:this.snapping.toFeature.toLine,toCoordinate:this.snapping.toFeature.toCoordinate});e.coordinate&&(s=e.coordinate)}if(null!=(o=this.snapping)&&o.toCustom){const e=this.snapping.toCustom(t,{currentCoordinate:this.currentCoordinate,currentId:n,getCurrentGeometrySnapshot:void 0!==n?()=>this.readFeature.getGeometry(n):()=>null,project:this.project,unproject:this.unproject});e&&(s=e)}return s}close(){if(void 0===this.currentId)return;const t=this.mutateFeature.updatePolygon({featureId:this.currentId,propertyMutations:{[d.CURRENTLY_DRAWING]:void 0},coordinateMutations:{coordinates:this.readFeature.getGeometry(this.currentId).coordinates,type:lt},context:{updateType:s.Finish,action:e}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates}),this.deleteSnappedPoint();const i=this.currentId;this.currentCoordinate=0,this.currentId=void 0,this.direction=void 0,this.previousEndBearing=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(i,{mode:this.mode,action:e})}getSectorCoordinates(t){const e=this.readFeature.getCoordinates(this.currentId),i=e[0],o=e[1],n=[t.lng,t.lat],s=H(i[0],i[1]),r=H(o[0],o[1]),a=H(n[0],n[1]);if(void 0===this.direction){const t=wi(s,r,a);this.direction=t?"clockwise":"anticlockwise"}const d=Ct(s,r),h=Kt(s,r),l=Kt(s,a);this.allowDirectionChange&&void 0!==this.previousEndBearing&&(this.direction=xi(this.direction,h,this.previousEndBearing,l)),this.previousEndBearing=l;const u=this.arcPoints,c=[i],p=Yt(h),g=Yt(l);let y;"anticlockwise"===this.direction?(y=g-p,y<0&&(y+=360)):(y=p-g,y<0&&(y+=360));const m=("anticlockwise"===this.direction?1:-1)*y/u;c.push(o);for(let t=0;t<=u;t++){const e=Rt(s,d,p+t*m),{lng:i,lat:o}=G(e.x,e.y),n=[L(i,this.coordinatePrecision),L(o,this.coordinatePrecision)];n[0]!==c[c.length-1][0]&&n[1]!==c[c.length-1][1]&&c.push(n)}return c.push(i),c}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}onMouseMove(t){this.mouseMove=!0,this.setCursor(this.cursors.start);const e=this.getSnappedCoordinate(t);this.updateSnappedCoordinate(e);const i=null!=e?e:[t.lng,t.lat];if(void 0===this.currentId||0===this.currentCoordinate)return;let o;if(1===this.currentCoordinate)o=[{type:dt,index:1,coordinate:i},{type:dt,index:2,coordinate:i}];else{if(2!==this.currentCoordinate)return;{const e=this.getSectorCoordinates(t);if(!e)return;o={type:lt,coordinates:[e]}}}const n=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:o,context:{updateType:s.Provisional}});n&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:n.geometry.coordinates})}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)){var e;this.currentCoordinate>0&&!this.mouseMove&&this.onMouseMove(t),this.mouseMove=!1;const i=null!=(e=this.getSnappedCoordinate(t))?e:[t.lng,t.lat];if(this.deleteSnappedPoint(),0===this.currentCoordinate){const t=this.mutateFeature.createPolygon({coordinates:[i,i,i,i],properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});this.currentId=null==t?void 0:t.id,this.currentCoordinate++,t&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.setDrawing()}else if(1===this.currentCoordinate&&this.currentId){if(this.readFeature.coordinateAtIndexIsIdentical({featureId:this.currentId,index:0,newCoordinate:i}))return;if(!this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:[{type:dt,index:1,coordinate:i},{type:dt,index:2,coordinate:i}],context:{updateType:s.Provisional}}))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(){this.deleteSnappedPoint();const t=this.currentId;this.currentId=void 0,this.direction=void 0,this.previousEndBearing=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted(),this.mutateFeature.deleteFeatureIfPresent(t),t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t])}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});if(e.properties.mode===this.mode)if("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.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.outlineOpacity,1,e),i.polygonFillOpacity=this.getNumericStylingValue(this.styles.fillOpacity,i.polygonFillOpacity,e),i.zIndex=h;else if("Point"===e.geometry.type&&e.properties[d.SNAPPING_POINT]){var o,n;i.zIndex=40,i.pointColor=this.getHexColorStylingValue(this.styles.snappingPointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.snappingPointWidth,i.pointWidth,e),i.pointOpacity=this.getNumericStylingValue(this.styles.snappingPointOpacity,null!=(o=i.pointOpacity)?o:1,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.snappingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.snappingPointOutlineWidth,2,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.snappingPointOutlineOpacity,null!=(n=i.pointOutlineOpacity)?n:1,e),i.zIndex=40}else"Point"===e.geometry.type&&e.properties[d.COORDINATE_POINT]&&(i.pointWidth=this.getNumericStylingValue(this.styles.coordinatePointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.coordinatePointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.coordinatePointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.coordinatePointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.coordinatePointOutlineOpacity,1,e),i.zIndex=l);return i}validateFeature(t){return this.validateModeFeature(t,t=>ot(t,this.coordinatePrecision))}afterFeatureUpdated(t){this.currentId===t.id&&(this.deleteSnappedPoint(),this.currentId=void 0,this.direction=void 0,this.previousEndBearing=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted()),this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}registerBehaviors(t){const e=new Pt(t),i=new It(t);this.coordinateSnapping=new St(t,e,i);const o=new Dt(t,e,i);this.featureSnapping=new kt(this.coordinateSnapping,o),this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature)}}const ki={cancel:E,finish:M},Ni={start:D,close:k};class bi extends x{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=ki,this.direction=void 0,this.arcPoints=64,this.cursors=Ni,this.mouseMove=!1,this.showCoordinatePoints=!1,this.allowDirectionChange=!1,this.previousEndBearing=void 0,this.readFeature=void 0,this.mutateFeature=void 0,this.coordinatePoints=void 0,this.updateOptions(t)}updateOptions(e){var i;super.updateOptions(e),C(null==e?void 0:e.allowDirectionChange)&&(this.allowDirectionChange=e.allowDirectionChange),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),f(null==e?void 0:e.arcPoints)&&e.arcPoints>0&&(this.arcPoints=e.arcPoints),C(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints),this.updateInitialArcCoordinatePoints())}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}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 e=this.getUpdatedLineStringCoordinates(t);if(!e)return;this.mutateFeature.updateLineString({featureId:this.currentInitialArcId,coordinateMutations:{type:lt,coordinates:e},context:{updateType:s.Provisional}})&&this.updateInitialArcCoordinatePoints()}else if(3===this.currentCoordinate){const e=this.getUpdatedPolygonCoordinates(t);if(!e)return;const i=e.map(t=>[...t]);if(this.currentId)this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:{type:lt,coordinates:[e]},context:{updateType:s.Provisional}})&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:[i]});else{const t=this.mutateFeature.createPolygon({coordinates:e,properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});if(!t)return;this.currentId=t.id,this.coordinatePoints.deletePointsByFeatureIds([this.currentInitialArcId]),this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:[i]})}}}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.mutateFeature.createPoint({coordinates:[t.lng,t.lat],properties:{mode:this.mode}});if(!e)return;this.currentStartingPointId=e.id,this.currentCoordinate++,this.setDrawing()}else if(1===this.currentCoordinate&&this.currentStartingPointId){const e=this.mutateFeature.createLineString({coordinates:[[t.lng,t.lat],[t.lng,t.lat]],properties:{mode:this.mode}});if(!e)return;this.currentInitialArcId=e.id,this.updateInitialArcCoordinatePoints(),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(){this.mutateFeature.deleteFeatureIfPresent(this.currentStartingPointId),this.deleteInitialArc(),this.currentId&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([this.currentId]),this.mutateFeature.deleteFeatureIfPresent(this.currentId),this.currentStartingPointId=void 0,this.direction=void 0,this.previousEndBearing=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,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 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.polygonOutlineOpacity=this.getNumericStylingValue(this.styles.outlineOpacity,1,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&&e.properties[d.COORDINATE_POINT]?(i.pointColor=this.getHexColorStylingValue(this.styles.coordinatePointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.coordinatePointOpacity,1,e),i.pointWidth=this.getNumericStylingValue(this.styles.coordinatePointWidth,i.pointWidth,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.coordinatePointOutlineOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.coordinatePointOutlineWidth,2,e),i.zIndex=l):"Point"===e.geometry.type&&(i.pointColor=this.getHexColorStylingValue(this.styles.centerPointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.centerPointOpacity,1,e),i.pointWidth=this.getNumericStylingValue(this.styles.centerPointWidth,i.pointWidth,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.centerPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.centerPointOutlineOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.centerPointOutlineWidth,i.pointOutlineWidth,e),i.zIndex=l)),i}validateFeature(t){return this.validateModeFeature(t,t=>ot(t,this.coordinatePrecision))}afterFeatureUpdated(t){this.currentId===t.id&&(this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.mutateFeature.deleteFeatureIfPresent(this.currentStartingPointId),this.deleteInitialArc(),this.currentStartingPointId=void 0,this.direction=void 0,this.previousEndBearing=void 0,this.currentId=void 0,this.currentCoordinate=0,"drawing"===this.state&&this.setStarted())}registerBehaviors(t){this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature)}updateInitialArcCoordinatePoints(){if(!this.showCoordinatePoints||void 0===this.currentInitialArcId)return;if(void 0!==this.currentId)return void this.coordinatePoints.deletePointsByFeatureIds([this.currentInitialArcId]);const t=this.readFeature.getCoordinates(this.currentInitialArcId);this.coordinatePoints.createOrUpdate({featureId:this.currentInitialArcId,featureCoordinates:t})}deleteInitialArc(t=this.currentInitialArcId){void 0!==t&&(this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t)),this.currentInitialArcId=void 0}close(){if(void 0===this.currentStartingPointId)return;const t=this.currentStartingPointId,i=this.currentInitialArcId;if(this.currentId&&!this.mutateFeature.updatePolygon({featureId:this.currentId,propertyMutations:{[d.CURRENTLY_DRAWING]:void 0},coordinateMutations:{coordinates:this.readFeature.getGeometry(this.currentId).coordinates,type:lt},context:{updateType:s.Finish,action:e}}))return;const o=this.currentId;this.mutateFeature.deleteFeatureIfPresent(t),this.deleteInitialArc(i),this.currentCoordinate=0,this.currentStartingPointId=void 0,this.currentInitialArcId=void 0,this.currentId=void 0,this.direction=void 0,this.previousEndBearing=void 0,"drawing"===this.state&&this.setStarted(),o&&this.onFinish(o,{mode:this.mode,action:e})}getUpdatedPolygonCoordinates(t){if(void 0===this.currentInitialArcId||void 0===this.currentStartingPointId||this.currentCoordinate<3)return;const e=this.readFeature.getCoordinates(this.currentInitialArcId);if(e.length<2)return;if(!this.direction)return;const i=this.readFeature.getGeometry(this.currentStartingPointId).coordinates,o=e[0],n=e[e.length-1],s=H(t.lng,t.lat),r=H(o[0],o[1]),a=H(n[0],n[1]),d=H(i[0],i[1]),h=Ct(d,r),l=Ct(d,s)=e&&t<=i:t>=e||t<=i:e>=i?t<=e&&t>=i:t<=e||t>=i}}class _i{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 Ti={__proto__:null,GeoJSONStore:li,TerraDrawBaseDrawMode:x,TerraDrawBaseSelectMode:w,TerraDrawBaseAdapter:class{constructor(t){this._nextKeyUpIsContextMenu=!1,this._lastPointerDownEventTarget=void 0,this._ignoreMismatchedPointerEvents=!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._ignoreMismatchedPointerEvents="boolean"==typeof t.ignoreMismatchedPointerEvents&&t.ignoreMismatchedPointerEvents,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(t.type),{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:n}=i,{containerX:s,containerY:r}=this.getMapElementXYPosition(t),a=this.getButton(t),d=Array.from(this._heldKeys);return{lng:L(o,this._coordinatePrecision),lat:L(n,this._coordinatePrecision),containerX:s,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 _i({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,this._lastPointerDownEventTarget=t.target?t.target:void 0)},register:t=>{this.getMapEventElement("pointerdown").addEventListener("pointerdown",t)},unregister:t=>{this.getMapEventElement("pointerdown").removeEventListener("pointerdown",t)}}),new _i({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(),n=Ct(t,i);let s=!1;if(s="drawing"===o?n{this.setDraggability.bind(this)(t)})}else"dragging"===this._dragState&&this._currentModeCallbacks.onDrag(e,t=>{this.setDraggability.bind(this)(t)})},register:t=>{this.getMapEventElement("pointermove").addEventListener("pointermove",t)},unregister:t=>{this.getMapEventElement("pointermove").removeEventListener("pointermove",t)}}),new _i({name:"contextmenu",callback:t=>{this._currentModeCallbacks&&(t.preventDefault(),this._nextKeyUpIsContextMenu=!0)},register:t=>{this.getMapEventElement("contextmenu").addEventListener("contextmenu",t)},unregister:t=>{this.getMapEventElement("contextmenu").removeEventListener("contextmenu",t)}}),new _i({name:"pointerup",callback:t=>{if(!this._currentModeCallbacks)return;if(t.target!==this.getMapEventElement("pointerup"))return;if(this._ignoreMismatchedPointerEvents&&this._lastPointerDownEventTarget!==t.target)return;if(this._lastPointerDownEventTarget=void 0,!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("pointerup").addEventListener("pointerup",t)},unregister:t=>{this.getMapEventElement("pointerup").removeEventListener("pointerup",t)}}),new _i({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("keyup").addEventListener("keyup",t)},unregister:t=>{this.getMapEventElement("keyup").removeEventListener("keyup",t)}}),new _i({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("keydown").addEventListener("keydown",t)},unregister:t=>{this.getMapEventElement("keydown").removeEventListener("keydown",t)}})]}unregister(){this._listeners.forEach(t=>{t.unregister()}),this.clear(),this._currentModeCallbacks=void 0,this._lastDrawEvent=void 0,this._lastPointerDownEventTarget=void 0,this._nextKeyUpIsContextMenu=!1}},getDefaultStyling:()=>({polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0}),SELECT_PROPERTIES:a};const Ri={ValidationReasonFeatureNotPoint:de,ValidationReasonFeatureInvalidCoordinates:he,ValidationReasonFeatureInvalidCoordinatePrecision:le,ValidationReasonFeatureNotPolygon:y,ValidationReasonFeatureHasHoles:J,ValidationReasonFeatureLessThanFourCoordinates:Q,ValidationReasonFeatureHasInvalidCoordinates:tt,ValidationReasonFeatureCoordinatesNotClosed:et,ValidationReasonFeatureNotPolygonOrLineString:Ci,ValidationReasonFeatureSelfIntersects:Pi,ValidationReasonFeatureLessThanMinSize:yi,ValidationReasonModeMismatch:m},Wi={cancel:E,finish:M},Ai={start:D,close:k};class Ui extends x{constructor(t){super(t,!0),this.mode="freehand-linestring",this.canClose=!1,this.showCoordinatePoints=!1,this.currentId=void 0,this.minDistance=20,this.keyEvents=Wi,this.cursors=Ai,this.preventNewFeature=!1,this.drawInteraction="click-move",this.drawType=void 0,this.coordinatePoints=void 0,this.mutateFeature=void 0,this.readFeature=void 0,this.pixelDistance=void 0,this.closingPoints=void 0,this.updateOptions(t)}updateOptions(e){var i;super.updateOptions(e),C(null==e?void 0:e.showCoordinatePoints)&&(this.showCoordinatePoints=e.showCoordinatePoints,null==(i=this.coordinatePoints)||i.setEnabled(this.showCoordinatePoints)),f(null==e?void 0:e.minDistance)&&(this.minDistance=e.minDistance),P(null==e?void 0:e.keyEvents)?this.keyEvents={cancel:null,finish:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),S(null==e?void 0:e.drawInteraction)&&(this.drawInteraction=e.drawInteraction),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors))}moveDrawAllowed(){return"click-move"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}dragDrawAllowed(){return"click-drag"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}close(){if(void 0===this.currentId)return;const t=this.mutateFeature.updateLineString({featureId:this.currentId,propertyMutations:{[d.CURRENTLY_DRAWING]:void 0},context:{updateType:s.Finish,action:e}});if(!t)return;this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:t.geometry.coordinates});const i=this.currentId;this.closingPoints.delete(),this.canClose=!1,this.currentId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(i,{mode:this.mode,action:e})}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}addCoordinate(t){if(void 0===this.currentId||!1===this.canClose)return void this.setCursor(this.cursors.start);const[e,i]=this.readFeature.getCoordinate(this.currentId,-2),{x:o,y:n}=this.project(e,i),r=Ct({x:o,y:n},{x:t.containerX,y:t.containerY}),[a,d]=this.readFeature.getCoordinate(this.currentId,-1),{x:h,y:l}=this.project(a,d),u=Ct({x:h,y:l},{x:t.containerX,y:t.containerY});if(this.setCursor(u{this.preventNewFeature=!1},500),this.close(),e(!0))}cleanUp(){const t=this.currentId;this.currentId=void 0,this.drawType=void 0,this.canClose=!1,"drawing"===this.state&&this.setStarted(),void 0!==t&&this.showCoordinatePoints&&this.coordinatePoints.deletePointsByFeatureIds([t]),this.mutateFeature.deleteFeatureIfPresent(t),this.closingPoints.delete()}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});return"Feature"===e.type&&"LineString"===e.geometry.type&&e.properties.mode===this.mode?(i.lineStringDash=this.getDashArrayStylingValue(this.styles.lineStringDash,void 0,e),i.lineStringColor=this.getHexColorStylingValue(this.styles.lineStringColor,i.lineStringColor,e),i.lineStringOpacity=this.getNumericStylingValue(this.styles.lineStringOpacity,void 0===i.lineStringOpacity?1:i.lineStringOpacity,e),i.lineStringWidth=this.getNumericStylingValue(this.styles.lineStringWidth,i.lineStringWidth,e),i.zIndex=h,i):"Feature"===e.type&&e.properties.mode===this.mode&&"Point"===e.geometry.type&&e.properties[d.COORDINATE_POINT]?(i.pointWidth=this.getNumericStylingValue(this.styles.coordinatePointWidth,i.pointWidth,e),i.pointColor=this.getHexColorStylingValue(this.styles.coordinatePointColor,i.pointColor,e),i.pointOpacity=this.getNumericStylingValue(this.styles.coordinatePointOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.coordinatePointOutlineWidth,2,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.coordinatePointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.coordinatePointOutlineOpacity,1,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.pointOpacity=this.getNumericStylingValue(this.styles.closingPointOpacity,1,e),i.pointColor=this.getHexColorStylingValue(this.styles.closingPointColor,i.pointColor,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.closingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.closingPointOutlineOpacity,1,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.closingPointOutlineWidth,2,e),i.zIndex=50,i):i}validateFeature(t){return this.validateModeFeature(t,t=>Jt(t,this.coordinatePrecision))}afterFeatureUpdated(t){this.showCoordinatePoints&&"LineString"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.currentId===t.id&&(this.closingPoints.delete(),this.canClose=!1,this.currentId=void 0,this.drawType=void 0)}afterFeatureAdded(t){this.showCoordinatePoints&&"LineString"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates})}registerBehaviors(t){this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:this.validate}),this.coordinatePoints=new ft(t,this.readFeature,this.mutateFeature),this.pixelDistance=new Pt(t),this.closingPoints=new Qt(t,this.pixelDistance,this.mutateFeature,this.readFeature)}}function Li(t){if(null===t)return!0;if("boolean"==typeof t)return!0;if("string"==typeof t)return!0;if(void 0===t)return!1;if("number"==typeof t)return Number.isFinite(t);if("bigint"==typeof t)return!1;if("symbol"==typeof t)return!1;if("function"==typeof t)return!1;if(t instanceof RegExp)return!1;if(t instanceof Map)return!1;if(t instanceof Set)return!1;if(t instanceof Date)return!1;if("object"==typeof t&&null!==t&&!Array.isArray(t)){const e=Object.getPrototypeOf(t);if(e!==Object.prototype&&null!==e)return!1}if(ArrayBuffer.isView(t)&&!(t instanceof DataView))return!1;if(Array.isArray(t))for(const e of t)if(!Li(e))return!1;return"object"==typeof t&&Object.keys(t).every(e=>"string"==typeof e&&Li(t[e]))}const Bi={create:D,dragStart:N,dragEnd:D};class Vi extends x{constructor(t){super(t,!0),this.mode="marker",this.cursors=Bi,this.editable=!1,this.snapping=void 0,this.editedFeatureId=void 0,this.snappedPointId=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.pointSearch=void 0,this.mutateFeature=void 0,this.coordinateSnapping=void 0,this.lineSnapping=void 0,this.featureSnapping=void 0,this.updateOptions(t)}updateOptions(e){super.updateOptions(e),v(null==e?void 0:e.cursors)&&(this.cursors=t({},this.cursors,e.cursors)),v(null==e?void 0:e.snapping)&&(this.snapping=e.snapping),C(null==e?void 0:e.editable)&&(this.editable=e.editable)}start(){this.setStarted(),this.setCursor(this.cursors.create)}stop(){this.cleanUp(),this.setStopped(),this.setCursor(_)}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(t){t&&this.updateSnappedCoordinate(t)}onKeyDown(){}onKeyUp(){}cleanUp(){this.editedFeatureId=void 0,this.deleteSnappedPoint()}onDragStart(t,e){if(this.allowPointerEvent(this.pointerEvents.onDragStart,t)){if(this.editable){const e=this.pointSearch.getNearestPointFeature(t);this.editedFeatureId=null==e?void 0:e.id}this.editedFeatureId&&(this.setCursor(this.cursors.dragStart),e(!1))}}onDrag(t,e){this.allowPointerEvent(this.pointerEvents.onDrag,t)&&void 0!==this.editedFeatureId&&this.mutateFeature.updatePoint({featureId:this.editedFeatureId,coordinateMutations:{type:lt,coordinates:this.snapCoordinate(t,this.editedFeatureId)},propertyMutations:{[d.EDITED]:!0},context:{updateType:s.Provisional}})}onDragEnd(t,i){if(!this.allowPointerEvent(this.pointerEvents.onDragEnd,t))return;if(void 0===this.editedFeatureId)return;if(!this.mutateFeature.updatePoint({featureId:this.editedFeatureId,propertyMutations:{mode:this.mode,[d.EDITED]:!1},context:{updateType:s.Finish,action:"edit"}}))return;const o=this.editedFeatureId;this.setCursor(this.cursors.dragEnd),this.editedFeatureId=void 0,this.deleteSnappedPoint(),i(!0),this.onFinish(o,{mode:this.mode,action:e})}registerBehaviors(t){this.pixelDistance=new Pt(t),this.clickBoundingBox=new It(t),this.pointSearch=new ce(t,this.pixelDistance,this.clickBoundingBox),this.coordinateSnapping=new St(t,this.pixelDistance,this.clickBoundingBox),this.lineSnapping=new Dt(t,this.pixelDistance,this.clickBoundingBox),this.featureSnapping=new kt(this.coordinateSnapping,this.lineSnapping),this.mutateFeature=new ut(t,{validate:this.validate})}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonOutlineOpacity:1,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOpacity:1,pointOutlineColor:"#ffffff",pointOutlineOpacity:1,pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,lineStringOpacity:1,zIndex:0,markerUrl:void 0,markerHeight:void 0,markerWidth:void 0,markerAnchor:"bottom",lineStringDash:void 0});if("Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode){const t=!0===e.properties[d.SNAPPING_POINT];var o,n,s,a,h,l;i.zIndex=t?40:u,t?(i.pointColor=this.getHexColorStylingValue(this.styles.snappingPointColor,i.pointColor,e),i.pointWidth=this.getNumericStylingValue(this.styles.snappingPointWidth,i.pointWidth,e),i.pointOpacity=this.getNumericStylingValue(this.styles.snappingPointOpacity,null!=(o=i.pointOpacity)?o:1,e),i.pointOutlineColor=this.getHexColorStylingValue(this.styles.snappingPointOutlineColor,i.pointOutlineColor,e),i.pointOutlineWidth=this.getNumericStylingValue(this.styles.snappingPointOutlineWidth,2,e),i.pointOutlineOpacity=this.getNumericStylingValue(this.styles.snappingPointOutlineOpacity,null!=(n=i.pointOutlineOpacity)?n:1,e)):(i.markerHeight=this.getNumericStylingValue(null==(s=this.styles)?void 0:s.markerHeight,40,e),i.markerWidth=this.getNumericStylingValue(null==(a=this.styles)?void 0:a.markerWidth,32,e),i.markerUrl=this.getUrlStylingValue(null==(h=this.styles)?void 0:h.markerUrl,r,e),i.markerAnchor=this.getMarkerAnchorStylingValue(null==(l=this.styles)?void 0:l.markerAnchor,"bottom",e))}return i}validateFeature(t){return this.validateModeFeature(t,t=>ue(t,this.coordinatePrecision))}onLeftClick(t){this.deleteSnappedPoint();const i=this.mutateFeature.createPoint({coordinates:this.snapCoordinate(t),properties:{mode:this.mode,[d.MARKER]:!0},context:{updateType:s.Finish,action:e}});i&&this.onFinish(i.id,{mode:this.mode,action:e})}snapCoordinate(t,e){var i;return null!=(i=this.getSnappedCoordinate(t,e))?i:[t.lng,t.lat]}updateSnappedCoordinate(t){var e;const i=this.getSnappedCoordinate(t);i&&!0===(null==(e=this.snapping)?void 0:e.showSnappingPoint)?this.snappedPointId?this.mutateFeature.updateGuidancePoints([{featureId:this.snappedPointId,coordinate:i}]):this.snappedPointId=this.mutateFeature.createGuidancePoint({coordinate:i,type:d.SNAPPING_POINT}):this.deleteSnappedPoint()}deleteSnappedPoint(){this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0)}getSnappedCoordinate(t,e){var i,o,n;let s;if(null!=(i=this.snapping)&&i.toCoordinate){const i=e?this.coordinateSnapping.getSnappableCoordinate(t,e):this.coordinateSnapping.getSnappableCoordinateFirstClick(t);i&&(s=i)}if(null!=(o=this.snapping)&&o.toFeature){const i=this.featureSnapping.getSnappable(t,e,this.snapping.toFeature.filter,{toLine:this.snapping.toFeature.toLine,toCoordinate:this.snapping.toFeature.toCoordinate});i.coordinate&&(s=i.coordinate)}if(null!=(n=this.snapping)&&n.toCustom){const i=this.snapping.toCustom(t,{currentCoordinate:0,currentId:e,getCurrentGeometrySnapshot:()=>null,project:this.project,unproject:this.unproject});i&&(s=i)}return s}onRightClick(t){if(!this.editable)return;const e=this.pointSearch.getNearestPointFeature(t);e&&this.mutateFeature.deleteFeatureIfPresent(e.id)}afterFeatureUpdated(t){this.editedFeatureId===t.id&&(this.editedFeatureId=void 0,this.setCursor(this.cursors.create))}}const zi=[{key:"z",heldKeys:["meta"]},{key:"z",heldKeys:["control"]}],Hi=[{key:"z",heldKeys:["meta","shift"]},{key:"z",heldKeys:["control","shift"]},{key:"y",heldKeys:["control"]}],Gi=(t,e)=>{const i=t.key.toLowerCase(),o=new Set(t.heldKeys.map(t=>t.toLowerCase()).filter(t=>t!==i));if(e.key.toLowerCase()!==i)return!1;const n=new Set(e.heldKeys.map(t=>t.toLowerCase()));if(o.size!==n.size)return!1;for(const t of n)if(!o.has(t))return!1;return!0};class ji{constructor(t){var e,i;this.undoKeyboardShortcuts=void 0,this.redoKeyboardShortcuts=void 0,this.undoKeyboardShortcuts=null!=(e=null==t?void 0:t.undo)?e:zi,this.redoKeyboardShortcuts=null!=(i=null==t?void 0:t.redo)?i:Hi}isUndoKeyboardShortcut(t){return this.undoKeyboardShortcuts.some(e=>Gi(t,e))}isRedoKeyboardShortcut(t){return this.redoKeyboardShortcuts.some(e=>Gi(t,e))}}function Ki(t){return void 0!==t&&Number.isFinite(t)?Math.max(0,Math.floor(t)):Number.POSITIVE_INFINITY}const Yi="undo",Xi="redo",$i="push",qi="mode",Zi="session";class Ji{constructor(t){this.getModeState=void 0,this.getModeHistorySizes=void 0,this.undoMode=void 0,this.redoMode=void 0,this.clearModeHistory=void 0,this.onHistoryChange=void 0,this.maxStackSize=void 0,this.lastHistorySizes={undoSize:0,redoSize:0},this.maxStackSize=Ki(null==t?void 0:t.maxStackSize)}getMaxStackSize(){return this.maxStackSize}register(t){this.getModeState=t.getModeState,this.getModeHistorySizes=t.getModeHistorySizes,this.undoMode=t.undoMode,this.redoMode=t.redoMode,this.clearModeHistory=t.clearModeHistory,this.onHistoryChange=t.onHistoryChange}inDrawingState(){return!!this.getModeState&&"drawing"===this.getModeState()}canUndo(){if(!this.inDrawingState())return!1;const{undoSize:t}=this.getHistorySizes();return t>0}canRedo(){if(!this.inDrawingState())return!1;const{redoSize:t}=this.getHistorySizes();return t>0}undo(){return!(!this.canUndo()||!this.undoMode||(this.undoMode(),this.emitHistoryChange(Yi),0))}redo(){return!(!this.canRedo()||!this.redoMode||(this.redoMode(),this.emitHistoryChange(Xi),0))}clearHistory(){this.clearModeHistory&&this.clearModeHistory(),this.lastHistorySizes={undoSize:0,redoSize:0}}getHistorySizes(){return this.getModeHistorySizes?this.getModeHistorySizes():{undoSize:0,redoSize:0}}undoSize(){return this.getHistorySizes().undoSize}redoSize(){return this.getHistorySizes().redoSize}emitPushIfHistoryChangedFromLastSnapshot(){if(!this.inDrawingState())return;const t=this.getHistorySizes();t.undoSize===this.lastHistorySizes.undoSize&&t.redoSize===this.lastHistorySizes.redoSize||this.emitHistoryChange($i)}emitPushIfHistoryChanged(t){if(!this.inDrawingState())return;const e=this.getHistorySizes();e.undoSize===t.undoSize&&e.redoSize===t.redoSize||this.emitHistoryChange($i)}emitHistoryChange(t){if(!this.onHistoryChange)return;const{undoSize:e,redoSize:i}=this.getHistorySizes();this.lastHistorySizes={undoSize:e,redoSize:i},this.onHistoryChange({cause:t,stack:qi,undoStackSize:e,redoStackSize:i})}}class Qi{constructor(t){this.draw=void 0,this.onHistoryChange=void 0,this.maxStackSize=void 0,this.historyById={},this.undoStack=[],this.ignoreProgrammaticCreate={},this.ignoreProgrammaticDelete={},this.deletedFeatureIds={},this.redoStack=[],this.isReplayingHistory=!1,this.emitStackChange=t=>{this.onHistoryChange&&this.onHistoryChange({cause:t,stack:Zi,undoStackSize:this.undoStack.length,redoStackSize:this.redoStack.length})},this.handleChange=(t,e,i)=>{if(!this.draw||this.isDrawing())return;if(0===this.maxStackSize)return;if("update"===e){if(void 0===i||!("origin"in i)||"api"!==i.origin||this.isReplayingHistory)return;const e=Array.isArray(t)?t:[t];let o=!1;for(const t of e){if(null==t)continue;const e=String(t),i=this.draw.getSnapshotFeature(t);i&&(this.historyById[e]||(this.historyById[e]=[]),this.historyById[e].push(i),this.pushUndoStackEntry({id:t,toIndex:this.historyById[e].length-1,action:"single"}),o=!0)}return void(o&&(this.redoStack.length=0,this.emitStackChange($i)))}if("delete"!==e&&"create"!==e)return;if("create"===e){if(void 0===i||!("origin"in i)||"api"!==i.origin)return;let e=!1;const o=Array.isArray(t)?t:[t],n=[];for(const t of o){if(this.ignoreProgrammaticCreate[t]){delete this.ignoreProgrammaticCreate[t],delete this.deletedFeatureIds[t];continue}const i=String(t),o=this.draw.getSnapshotFeature(t);o&&(this.deletedFeatureIds[t]&&(this.historyById[i]=[],delete this.deletedFeatureIds[t]),this.historyById[i]||(this.historyById[i]=[]),this.historyById[i].push(o),n.push({id:t,toIndex:this.historyById[i].length-1,snapshot:o}),e=!0)}if(n.length>1)this.pushUndoStackEntry({id:n[0].id,toIndex:n[0].toIndex,action:"batch-create",metadata:{entries:n}});else if(1===n.length){const t=n[0];this.pushUndoStackEntry({id:t.id,toIndex:t.toIndex,action:"single"})}return void(e&&(this.redoStack.length=0,this.emitStackChange($i)))}let o=!1;const n=Array.isArray(t)?t:[t],s=[];for(const t of n){const e=String(t);if(this.ignoreProgrammaticDelete[t]){delete this.ignoreProgrammaticDelete[t];continue}if(!this.historyById[e])continue;const i=this.historyById[e].length-1;if(i>=0){const n=this.historyById[e][i];if(!n)continue;s.push({id:t,toIndex:i,snapshot:n}),this.deletedFeatureIds[t]=!0,o=!0}}if(s.length>1)this.pushUndoStackEntry({id:s[0].id,toIndex:s[0].toIndex,action:"batch-delete",metadata:{entries:s}});else if(1===s.length){const t=s[0];this.pushUndoStackEntry({id:t.id,toIndex:t.toIndex,action:"single"})}o&&(this.redoStack.length=0,this.emitStackChange($i))},this.handleFinish=t=>{if(!this.draw)return;if(0===this.maxStackSize)return;if(this.isReplayingHistory)return;const e=Array.isArray(t)?t:[t];let i=!1;for(const t of e){if(null==t)continue;const e=String(t),o=this.draw.getSnapshotFeature(t);o&&(this.historyById[e]||(this.historyById[e]=[]),this.historyById[e].push(o),i||(this.redoStack.length=0,i=!0),this.pushUndoStackEntry({id:t,toIndex:this.historyById[e].length-1,action:"single"}),this.emitStackChange($i))}},this.maxStackSize=Ki(null==t?void 0:t.maxStackSize)}register(t){this.draw!==t.draw?(this.draw&&(this.draw.off("change",this.handleChange),this.draw.off("finish",this.handleFinish)),this.draw=t.draw,this.draw.on("change",this.handleChange),this.draw.on("finish",this.handleFinish),this.onHistoryChange=t.onHistoryChange):this.onHistoryChange=t.onHistoryChange}pushUndoStackEntry(t){0!==this.maxStackSize&&(this.undoStack.push(t),this.undoStack.length>this.maxStackSize&&this.undoStack.shift())}pushRedoStackEntry(t){0!==this.maxStackSize&&(this.redoStack.push(t),this.redoStack.length>this.maxStackSize&&this.redoStack.shift())}isDrawing(){return!!this.draw&&"drawing"===this.draw.getModeState()}applySnapshotDuringReplay(t,e){if(this.draw){this.isReplayingHistory=!0;try{this.draw.hasFeature(t)&&(this.ignoreProgrammaticDelete[t]=!0,this.draw.removeFeatures([t])),this.ignoreProgrammaticCreate[t]=!0,delete this.deletedFeatureIds[t],this.draw.addFeatures([e])}finally{this.isReplayingHistory=!1}}}canUndo(){return!(!this.draw||this.isDrawing())&&this.undoStack.length>0}canRedo(){return!(!this.draw||this.isDrawing())&&this.redoStack.length>0}undo(){if(!this.canUndo())return!1;if(!this.draw)return!1;const t=this.undoStack.pop();if(!t)return this.emitStackChange(Yi),!1;if("batch-create"===t.action){var e;const i=(null==(e=t.metadata)?void 0:e.entries)||[];if(0===i.length)return this.emitStackChange(Yi),!1;const o=i.map(t=>t.id);return o.forEach(t=>{this.ignoreProgrammaticDelete[t]=!0,this.deletedFeatureIds[t]=!0}),this.draw.removeFeatures(o),this.pushRedoStackEntry({id:i[0].id,toIndex:i[0].toIndex,action:"batch-create",metadata:{entries:i}}),this.emitStackChange(Yi),!0}if("batch-delete"===t.action){var i;const e=(null==(i=t.metadata)?void 0:i.entries)||[];if(0===e.length)return this.emitStackChange(Yi),!1;const o=e.map(t=>t.snapshot).filter(t=>void 0!==t);return o.length>0&&(e.forEach(t=>{this.ignoreProgrammaticCreate[t.id]=!0,delete this.deletedFeatureIds[t.id]}),this.draw.addFeatures(o)),this.pushRedoStackEntry({id:e[0].id,toIndex:e[0].toIndex,action:"batch-delete",metadata:{entries:e}}),this.emitStackChange(Yi),!0}const o=t.id,n=t.toIndex,s=String(o),r=this.historyById[s];if(!r||0===r.length)return this.emitStackChange(Yi),!1;const a=Math.min(n,r.length-1);if(!this.draw.hasFeature(o)){const t=r[a];return t?(this.ignoreProgrammaticCreate[o]=!0,delete this.deletedFeatureIds[o],this.draw.addFeatures([t]),this.pushRedoStackEntry({id:o,toIndex:a,action:"delete",snapshot:t}),this.emitStackChange(Yi),!0):(this.emitStackChange(Yi),!1)}if(a<=0)return this.pushRedoStackEntry({id:o,toIndex:0,action:"create"}),this.ignoreProgrammaticDelete[o]=!0,this.deletedFeatureIds[o]=!0,this.draw.removeFeatures([o]),this.undoStack=this.undoStack.filter(t=>t.id!==o),this.emitStackChange(Yi),!0;const d=r[a],h=r[a-1];return d&&this.pushRedoStackEntry({id:o,toIndex:a,snapshot:d,action:"update"}),this.applySnapshotDuringReplay(o,h),r.length=a,this.emitStackChange(Yi),!0}redo(){if(!this.canRedo())return!1;if(!this.draw)return!1;const t=this.redoStack.pop(),{id:e,toIndex:i,snapshot:o,action:n,metadata:s}=t;if("batch-create"===n){const t=(null==s?void 0:s.entries)||[];if(0===t.length)return this.emitStackChange(Xi),!1;const e=t.map(t=>t.snapshot).filter(t=>void 0!==t);return e.length>0&&(t.forEach(t=>{this.ignoreProgrammaticCreate[t.id]=!0}),this.draw.addFeatures(e)),this.pushUndoStackEntry({id:t[0].id,toIndex:t[0].toIndex,action:"batch-create",metadata:{entries:t}}),this.emitStackChange(Xi),!0}if("batch-delete"===n){const t=(null==s?void 0:s.entries)||[];if(0===t.length)return this.emitStackChange(Xi),!1;const e=t.map(t=>t.id);return e.forEach(t=>{this.ignoreProgrammaticDelete[t]=!0,this.deletedFeatureIds[t]=!0}),this.draw.removeFeatures(e),this.pushUndoStackEntry({id:t[0].id,toIndex:t[0].toIndex,action:"batch-delete",metadata:{entries:t}}),this.emitStackChange(Xi),!0}const r=String(e),a=this.historyById[r]||(this.historyById[r]=[]);if("delete"===n)return this.ignoreProgrammaticDelete[e]=!0,this.deletedFeatureIds[e]=!0,this.draw.removeFeatures([e]),this.pushUndoStackEntry({id:e,toIndex:i,action:"single"}),this.emitStackChange(Xi),!0;if(i<=0){const t=a[0];return!!t&&(this.ignoreProgrammaticCreate[e]=!0,this.draw.addFeatures([t]),this.pushUndoStackEntry({id:e,toIndex:0,action:"single"}),this.emitStackChange(Xi),!0)}const d=o||a[i];return!!d&&(a.length===i?a.push(d):(a[i]=d,a.length=i+1),this.applySnapshotDuringReplay(e,d),this.pushUndoStackEntry({id:e,toIndex:i,action:"single"}),this.emitStackChange(Xi),!0)}clearHistory(){const t={};if(this.draw&&!this.isDrawing()){const e=this.draw.getSnapshot();for(const i of e)t[String(i.id)]=[i]}this.historyById=t,this.undoStack=[],this.ignoreProgrammaticCreate={},this.ignoreProgrammaticDelete={},this.deletedFeatureIds={},this.redoStack=[]}undoSize(){return this.undoStack.length}redoSize(){return this.redoStack.length}}class to{constructor(t){var e;this.modeLevel=void 0,this.sessionLevel=void 0,this.shouldPreferMode=void 0,this.onHistoryChange=void 0,this.shouldEmitHistoryChange=void 0,this.modeLevel=t.modeLevel,this.sessionLevel=t.sessionLevel,this.shouldPreferMode=t.shouldPreferMode,this.onHistoryChange=t.onHistoryChange,this.shouldEmitHistoryChange=null!=(e=t.shouldEmitHistoryChange)?e:()=>!0}emitStackHistoryChange(t){this.shouldEmitHistoryChange()&&this.onHistoryChange&&this.onHistoryChange({cause:t.cause,stack:t.stack,undoSize:t.undoStackSize,redoSize:t.redoStackSize})}hasSessionUndo(){return Boolean(this.sessionLevel&&this.sessionLevel.canUndo())}hasSessionRedo(){return Boolean(this.sessionLevel&&this.sessionLevel.canRedo())}activeStackForUndo(){var t,e;return this.shouldPreferMode()&&null!=(t=this.modeLevel)&&t.canUndo()?qi:this.hasSessionUndo()?Zi:null!=(e=this.modeLevel)&&e.canUndo()?qi:void 0}activeStackForRedo(){var t,e;return this.shouldPreferMode()&&null!=(t=this.modeLevel)&&t.canRedo()?qi:this.hasSessionRedo()?Zi:null!=(e=this.modeLevel)&&e.canRedo()?qi:void 0}canUndo(){return void 0!==this.activeStackForUndo()}canRedo(){return void 0!==this.activeStackForRedo()}undo(){const t=this.activeStackForUndo();return!!t&&(t===qi?!!this.modeLevel&&this.modeLevel.undo():!(!this.sessionLevel||!this.sessionLevel.canUndo())&&this.sessionLevel.undo())}redo(){const t=this.activeStackForRedo();return!!t&&(t===qi?!!this.modeLevel&&this.modeLevel.redo():!(!this.sessionLevel||!this.sessionLevel.canRedo())&&this.sessionLevel.redo())}clearHistory(){this.modeLevel&&this.modeLevel.clearHistory(),this.sessionLevel&&this.sessionLevel.clearHistory()}emitHistoryPushForCompletedAction(){this.sessionLevel?this.emitStackHistoryChange({cause:$i,undoStackSize:this.sessionLevel.undoSize(),redoStackSize:this.sessionLevel.redoSize(),stack:Zi}):this.modeLevel&&this.emitStackHistoryChange({cause:$i,undoStackSize:this.modeLevel.undoSize(),redoStackSize:this.modeLevel.redoSize(),stack:qi})}}class eo{constructor(e){var i,o,n,s;this._modes=void 0,this._mode=void 0,this._adapter=void 0,this._enabled=!1,this._store=void 0,this._eventListeners=void 0,this._instanceSelectModes=void 0,this.sessionUndoRedoEnabled=!1,this.keyboardShortcutsMatcher=void 0,this.drawingUndoRedo=void 0,this.sessionUndoRedo=void 0,this.undoRedoCoordinator=void 0,this._adapter=e.adapter,this._instanceSelectModes=[];const r=null==e||null==(i=e.undoRedo)?void 0:i.modeLevel;r&&(this.drawingUndoRedo=r);const a=null==e||null==(o=e.undoRedo)?void 0:o.keyboardShortcuts;a&&(this.keyboardShortcutsMatcher=a),this.sessionUndoRedoEnabled=Boolean(null==e||null==(n=e.undoRedo)?void 0:n.sessionLevel);const d=null==e||null==(s=e.undoRedo)?void 0:s.sessionLevel;this._mode=new Ge;const h=new Set,l=e.modes.reduce((t,e)=>{if(h.has(e.mode))throw new Error(`There is already a ${e.mode} mode provided`);return h.add(e.mode),t[e.mode]=e,t},{}),u=Object.keys(l);if(0===u.length)throw new Error("No modes provided");u.forEach(t=>{l[t].type===F.Select&&this._instanceSelectModes.push(t)}),this._modes=t({},l,{static:this._mode}),this._eventListeners={change:[],select:[],deselect:[],finish:[],ready:[],history:[]},this._store=new li({tracked:!!e.tracked,idStrategy:e.idStrategy?e.idStrategy:void 0});const c=t=>{const e=[],i=this._store.copyAll().filter(i=>!t.includes(i.id)||(e.push(i),!1));return{changed:e,unchanged:i}},p=(t,e)=>{var i;this._enabled&&(this._eventListeners.finish.forEach(i=>{i(t,e)}),null==(i=this.undoRedoCoordinator)||i.emitHistoryPushForCompletedAction())},g=(t,e,i)=>{if(!this._enabled)return;this._eventListeners.change.forEach(o=>{o(t,e,i)}),this.emitDrawingPushIfHistoryChangedFromLastSnapshot();const{changed:o,unchanged:n}=c(t);"create"===e?this._adapter.render({created:o,deletedIds:[],unchanged:n,updated:[]},this.getModeStyles()):"update"===e?this._adapter.render({created:[],deletedIds:[],unchanged:n,updated:o},this.getModeStyles()):"delete"===e?this._adapter.render({created:[],deletedIds:t,unchanged:n,updated:[]},this.getModeStyles()):"styling"===e&&this._adapter.render({created:[],deletedIds:[],unchanged:n,updated:[]},this.getModeStyles())},y=t=>{if(!this._enabled)return;this._eventListeners.select.forEach(e=>{e(t)});const{changed:e,unchanged:i}=c([t]);this._adapter.render({created:[],deletedIds:[],unchanged:i,updated:e},this.getModeStyles())},m=t=>{if(!this._enabled)return;this._eventListeners.deselect.forEach(e=>{e(t)});const{changed:e,unchanged:i}=c([t]);e&&this._adapter.render({created:[],deletedIds:[],unchanged:i,updated:e},this.getModeStyles())};Object.keys(this._modes).forEach(t=>{var e;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:g,onSelect:y,onDeselect:m,onFinish:p,coordinatePrecision:this._adapter.getCoordinatePrecision(),undoRedoMaxStackSize:null==(e=this.drawingUndoRedo)||null==e.getMaxStackSize?void 0:e.getMaxStackSize()})}),this.sessionUndoRedoEnabled&&d&&(this.sessionUndoRedo=d,d.register({draw:this,onHistoryChange:t=>{var e;null==(e=this.undoRedoCoordinator)||e.emitStackHistoryChange(t)}})),this.drawingUndoRedo&&this.drawingUndoRedo.register({getModeState:()=>this.getModeState(),getModeHistorySizes:()=>this.getDrawingHistorySizes(),undoMode:()=>{this._mode.undo&&this._mode.undo()},redoMode:()=>{this._mode.redo&&this._mode.redo()},clearModeHistory:()=>{const t=this._mode;t.clearHistory&&t.clearHistory()},onHistoryChange:t=>{var e;null==(e=this.undoRedoCoordinator)||e.emitStackHistoryChange(t)}}),this.undoRedoCoordinator=new to({modeLevel:this.drawingUndoRedo,sessionLevel:this.sessionUndoRedo,shouldPreferMode:()=>"drawing"===this.getModeState(),onHistoryChange:t=>{this._eventListeners.history.forEach(e=>{e(t)})},shouldEmitHistoryChange:()=>this._enabled})}checkEnabled(){if(!this._enabled)throw new Error("Terra Draw is not enabled")}handleUndoRedoKeyboardShortcut(t){if(!this.drawingUndoRedo&&!this.sessionUndoRedoEnabled)return!1;if(!this.keyboardShortcutsMatcher)return!1;const e=this.keyboardShortcutsMatcher.isUndoKeyboardShortcut(t),i=this.keyboardShortcutsMatcher.isRedoKeyboardShortcut(t);if(e){if(!this.canUndo())return!1;const e=this.undo();return e&&t.preventDefault(),e}if(i){if(!this.canRedo())return!1;const e=this.redo();return e&&t.preventDefault(),e}return!1}getDrawingHistorySizes(){return{undoSize:this._mode.undoSize&&"function"==typeof this._mode.undoSize?this._mode.undoSize():0,redoSize:this._mode.redoSize&&"function"==typeof this._mode.redoSize?this._mode.redoSize():0}}emitDrawingPushIfHistoryChangedFromLastSnapshot(){this.drawingUndoRedo&&this.drawingUndoRedo.emitPushIfHistoryChangedFromLastSnapshot()}emitDrawingPushIfHistoryChanged(t){this.drawingUndoRedo&&this.drawingUndoRedo.emitPushIfHistoryChanged(t)}getModeStyles(){const t={},e=this._instanceSelectModes.includes(this._mode.mode)?this._mode.mode:void 0;return Object.keys(this._modes).forEach(i=>{t[i]=t=>e&&t.properties[a.SELECTED]?this._modes[e].styleFeature.bind(this._modes[e])(t):this._modes[i].styleFeature.bind(this._modes[i])(t)}),t}featuresAtLocation({lng:t,lat:e},i){const o=i&&void 0!==i.pointerDistance?i.pointerDistance:30,n=!i||void 0===i.ignoreSelectFeatures||i.ignoreSelectFeatures,s=!(!i||void 0===i.ignoreCoordinatePoints)&&i.ignoreCoordinatePoints,r=!(!i||void 0===i.ignoreCurrentlyDrawing)&&i.ignoreCurrentlyDrawing,h=!(!i||void 0===i.ignoreClosingPoints)&&i.ignoreClosingPoints,l=!(!i||void 0===i.ignoreSnappingPoints)&&i.ignoreSnappingPoints,u=this._adapter.unproject.bind(this._adapter),c=this._adapter.project.bind(this._adapter),p=c(t,e),g=vt({unproject:u,point:p,pointerDistance:o});return this._store.search(g).filter(u=>{if(n&&(u.properties[a.MID_POINT]||u.properties[a.SELECTION_POINT]))return!1;if(s&&u.properties[d.COORDINATE_POINT])return!1;if(h&&u.properties[d.CLOSING_POINT])return!1;if(r&&u.properties[d.CURRENTLY_DRAWING])return!1;if(l&&u.properties[d.SNAPPING_POINT])return!1;if("Point"===u.geometry.type){const t=u.geometry.coordinates,e=c(t[0],t[1]);return Ct(p,e){if(null==i||!i.addClosestCoordinateInfoToProperties)return o;let n;if("Polygon"===o.geometry.type)n=o.geometry.coordinates[0].slice(0,-1);else{if("LineString"!==o.geometry.type)return o;n=o.geometry.coordinates}let s,r=-1,a=Infinity;for(let t=0;t{if(!this._store.has(t))throw new Error(`No feature with id ${t}, can not delete`);const n=this._store.getPropertiesCopy(t);n[a.SELECTED]&&this.deselectFeature(t),n[d.CURRENTLY_DRAWING]&&this._modes[n.mode]?o=n.mode:(n[d.COORDINATE_POINT_IDS]&&e.push(...n[d.COORDINATE_POINT_IDS]),i.push(t))}),this._store.delete([...i,...e],{origin:"api"}),o&&this._modes[o]&&this._modes[o].cleanUp()&&this._modes[o].cleanUp()}selectFeature(t,e){this.getSelectModeOrThrow(e).selectFeature(t)}deselectFeature(t){this.getSelectModeOrThrow().deselectFeature(t)}getFeatureId(){return this._store.getId()}hasFeature(t){return this._store.has(t)}checkIsReservedProperty(t){return![...Object.values(a),...Object.values(d)].includes(t)}updateFeatureProperties(t,e){var i;if(!this._store.has(t))throw new Error(`No feature with id ${t} present in store`);const o=this._store.copy(t);if(this.isGuidanceFeature(o))throw new Error("Guidance features are not allowed to be updated directly.");const n=o.properties.mode;if(!this._modes[n])throw new Error(`No mode with name ${n} present in instance`);const s=Object.entries(e);s.forEach(([t,e])=>{if(!this.checkIsReservedProperty(t))throw new Error(`You are trying to update a reserved property name: ${t}. Please choose another name.`);if(void 0!==e&&!Li(e))throw new Error(`Invalid JSON value provided for property ${t}`)}),this._store.updateProperty(s.map(([t,e])=>({id:o.id,property:t,value:e})),{origin:"api"}),null==(i=this.undoRedoCoordinator)||i.emitHistoryPushForCompletedAction()}updateFeatureGeometry(e,i){var o;if(!this._store.has(e))throw new Error(`No feature with id ${e} present in store`);const n=this._store.copy(e);if(this.isGuidanceFeature(n))throw new Error("Guidance features are not allowed to be updated directly.");if(!(n&&i&&i.type&&i.coordinates))throw new Error("Invalid geometry provided");if(i.type!==n.geometry.type)throw new Error(`Geometry type mismatch: expected ${n.geometry.type}, got ${i.type}`);const s=n.properties.mode,r=this._modes[s];if(!r)throw new Error(`No mode with name ${s} present in instance`);const d=t({},n,{geometry:i}),h=r.validateFeature(d);if(!h.valid)throw new Error(`Feature validation failed: ${h.reason||"Unknown reason"}`);if(this._store.updateGeometry([{id:n.id,geometry:i}],{origin:"api"}),r.afterFeatureUpdated){r.afterFeatureUpdated(d);const t=d.properties[a.SELECTED],e=this.getSelectMode({switchToSelectMode:!1});e&&t&&e.afterFeatureUpdated(d)}null==(o=this.undoRedoCoordinator)||o.emitHistoryPushForCompletedAction()}transformFeatureGeometry(t,e){var i;if(!this._store.has(t))throw new Error(`No feature with id ${t} present in store`);let o=this._store.copy(t);if(this.isGuidanceFeature(o))throw new Error("Guidance features are not allowed to be updated directly.");const n=o.properties.mode,s=this._modes[n];if(!s)throw new Error(`No mode with name ${n} present in instance`);let r;if("Polygon"===o.geometry.type)r=o.geometry.coordinates[0];else{if("LineString"!==o.geometry.type)throw new Error(`Feature geometry type ${o.geometry.type} is not supported for transformation`);r=o.geometry.coordinates}if("web-mercator"!=e.projection)throw new Error(`Projection ${e.projection} is not currently supported for transformation`);if("scale"===e.type){const{x:t,y:i}=H(e.origin[0],e.origin[1]);Le({coordinates:r,originX:t,originY:i,xScale:e.options.xScale||1,yScale:e.options.yScale||1})}else"rotate"===e.type&&(o=Re(o,e.options.angle||0),r="Polygon"===o.geometry.type?o.geometry.coordinates[0]:o.geometry.coordinates);if(r=r.map(t=>[L(t[0],this._adapter.getCoordinatePrecision()),L(t[1],this._adapter.getCoordinatePrecision())]),o.geometry.coordinates="Polygon"===o.geometry.type?[r]:r,this._store.updateGeometry([{id:o.id,geometry:o.geometry}],{origin:"api"}),s.afterFeatureUpdated){s.afterFeatureUpdated(o);const t=o.properties[a.SELECTED],e=this.getSelectMode({switchToSelectMode:!1});e&&t&&e.afterFeatureUpdated(o)}null==(i=this.undoRedoCoordinator)||i.emitHistoryPushForCompletedAction()}undo(){return this.checkEnabled(),!!this.undoRedoCoordinator&&this.undoRedoCoordinator.undo()}canUndo(){return this.checkEnabled(),!!this.undoRedoCoordinator&&this.undoRedoCoordinator.canUndo()}canRedo(){return this.checkEnabled(),!!this.undoRedoCoordinator&&this.undoRedoCoordinator.canRedo()}redo(){return this.checkEnabled(),!!this.undoRedoCoordinator&&this.undoRedoCoordinator.redo()}clearUndoRedoHistory(){this.checkEnabled(),this.undoRedoCoordinator&&this.undoRedoCoordinator.clearHistory()}addFeatures(t){return this.checkEnabled(),0===t.length?[]:this._store.load(t,t=>{if(p(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(p(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=>{const e=this.drawingUndoRedo?this.drawingUndoRedo.getHistorySizes():{undoSize:0,redoSize:0};this._mode.onClick(t),this.emitDrawingPushIfHistoryChanged(e)},onMouseMove:t=>{this._mode.onMouseMove(t)},onKeyDown:t=>{this.handleUndoRedoKeyboardShortcut(t)||this._mode.onKeyDown(t)},onKeyUp:t=>{const e=this.drawingUndoRedo?this.drawingUndoRedo.getHistorySizes():{undoSize:0,redoSize:0};this._mode.onKeyUp(t),this.emitDrawingPushIfHistoryChanged(e)},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({origin:"api"})}}))}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{eo as TerraDraw,Oi as TerraDrawAngledRectangleMode,_t as TerraDrawCircleMode,Vt as TerraDrawEllipseMode,Ti as TerraDrawExtend,Ui as TerraDrawFreehandLineStringMode,Gt as TerraDrawFreehandMode,ne as TerraDrawLineStringMode,Vi as TerraDrawMarkerMode,Ji as TerraDrawModeUndoRedo,ge as TerraDrawPointMode,ae as TerraDrawPolyLineMode,fe as TerraDrawPolygonMode,ve as TerraDrawRectangleMode,Ie as TerraDrawRenderMode,Di as TerraDrawSectorMode,He as TerraDrawSelectMode,bi as TerraDrawSensorMode,Qi as TerraDrawSessionUndoRedo,ji as TerraDrawUndoRedoKeyboardShortcuts,fi as ValidateMaxAreaSquareMeters,mi as ValidateMinAreaSquareMeters,vi as ValidateNotSelfIntersecting,Ri as ValidationReasons}; //# sourceMappingURL=terra-draw.modern.js.map