3 lines
261 KiB
JavaScript
3 lines
261 KiB
JavaScript
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var o in i)({}).hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t},t.apply(null,arguments)}const e="draw",i="edit",o="deleteCoordinate",n="insertMidpoint";var s;!function(t){t.Commit="commit",t.Provisional="provisional",t.Finish="finish"}(s||(s={}));const r="https://raw.githubusercontent.com/JamesLMilner/terra-draw/refs/heads/main/assets/markers/marker-blue.png",a={SELECTED:"selected",MID_POINT:"midPoint",SELECTION_POINT_FEATURE_ID:"selectionPointFeatureId",SELECTION_POINT:"selectionPoint"},d={MODE:"mode",CURRENTLY_DRAWING:"currentlyDrawing",EDITED:"edited",CLOSING_POINT:"closingPoint",SNAPPING_POINT:"snappingPoint",COORDINATE_POINT:"coordinatePoint",COORDINATE_POINT_FEATURE_ID:"coordinatePointFeatureId",COORDINATE_POINT_IDS:"coordinatePointIds",PROVISIONAL_COORDINATE_COUNT:"provisionalCoordinateCount",COMMITTED_COORDINATE_COUNT:"committedCoordinateCount",MARKER:"marker"},h=10,l=20,u=30;function c(t){return Boolean(t&&"object"==typeof t&&null!==t&&!Array.isArray(t))}function p(t){return Boolean(t&&"object"==typeof t&&"properties"in t&&"object"==typeof t.properties&&null!==t.properties&&"mode"in t.properties)}function g(t){return!!function(t){return"number"==typeof t&&!isNaN(new Date(t).valueOf())}(t)}const y="Feature is not a Polygon",m="Feature mode property does not match the mode being added to",f=t=>"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;t<n;t++){const r=j(e,i,-360*t/n);s.push([L(r[0],o),L(r[1],o)])}return s.push(s[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[s]},properties:{}}}function Y(t){let e;if("Polygon"===t.geometry.type)e=t.geometry.coordinates;else{if("LineString"!==t.geometry.type)throw new Error("Self intersects only accepts Polygons and LineStrings");e=[t.geometry.coordinates]}const i=[];for(let t=0;t<e.length;t++)for(let i=0;i<e[t].length-1;i++)for(let o=0;o<e.length;o++)for(let s=0;s<e[o].length-1;s++)n(t,i,o,s);return i.length>0;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<t.geometry.coordinates[0].length;i++){if(!q(t.geometry.coordinates[0][i]))return{valid:!1,reason:tt};if(!$(t.geometry.coordinates[0][i],e))return{valid:!1,reason:"Feature has coordinates with excessive precision"}}return(i=t.geometry.coordinates[0][0])[0]!==(o=t.geometry.coordinates[0][t.geometry.coordinates[0].length-1])[0]||i[1]!==o[1]?{valid:!1,reason:et}:{valid:!0};var i,o}function ot(t,e){const i=it(t,e);return i.valid?Y(t)?{valid:!1,reason:"Feature intersects itself"}:{valid:!0}:i}class nt{constructor({store:t,mode:e,project:i,unproject:o,pointerDistance:n,coordinatePrecision:s,projection:r,undoRedoMaxStackSize:a}){this.store=void 0,this.mode=void 0,this.project=void 0,this.unproject=void 0,this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.projection=void 0,this.undoRedoMaxStackSize=void 0,this.store=t,this.mode=e,this.project=i,this.unproject=o,this.pointerDistance=n,this.coordinatePrecision=s,this.projection=r,this.undoRedoMaxStackSize=a}}function st(t){const e=function(t){const e=t.coordinates[0];let i=0;for(let t=0;t<e.length-1;t++){const[o,n]=e[t],[s,r]=e[t+1];i+=(s-o)*(r+n)}return i<0}(t);if(!e)return{type:"Polygon",coordinates:[t.coordinates[0].reverse()]}}const rt="insert-before",at="insert-after",dt="update",ht="delete",lt="replace";class ut extends nt{constructor(t,e){super(t),this.options=void 0,this.options=e}createPoint({coordinates:t,properties:e,context:i}){if((null==i?void 0:i.updateType)!==s.Finish||this.validateGeometryWithUpdateType({geometry:{type:"Point",coordinates:t},properties:e,updateType:s.Finish}))return this.handleCreateFeature({geometry:{type:"Point",coordinates:t},properties:e})}createLineString({coordinates:t,properties:e}){return this.handleCreateFeature({geometry:{type:"LineString",coordinates:t},properties:e})}createPolygon({coordinates:t,properties:e,context:i}){const o=st({type:"Polygon",coordinates:[t]}),n={type:"Polygon",coordinates:o?o.coordinates:[t]};if((null==i?void 0:i.updateType)!==s.Finish||this.validateGeometryWithUpdateType({geometry:n,properties:e,updateType:s.Finish}))return this.handleCreateFeature({geometry:n,properties:e})}createGuidancePoint({coordinate:t,type:e}){return this.createGuidancePoints({coordinates:[t],type:e})[0]}createGuidancePoints({coordinates:e,type:i,additionalProperties:o}){const n=e.map((e,n)=>({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;t<s;t++){const e=d[t];for(const t of e)u.push(t.coordinate);const i=a[t];i?i.type===ht||u.push(i.coordinate):u.push(n[t]);const o=h[t];for(const t of o)u.push(t.coordinate)}for(const t of l)u.push(t.coordinate);return t({},e,o?{coordinates:[u,...e.coordinates.slice(1)]}:{coordinates:u})}isReplaceMutation(t){return t.type===lt}createFeatureWithGeometry({geometry:t,properties:e}){const[i]=this.createFeatures([{type:"Feature",geometry:t,properties:e}]);return{id:i,type:"Feature",properties:this.store.getPropertiesCopy(i),geometry:this.store.getGeometryCopy(i)}}validateGeometryWithUpdateType({geometry:t,properties:e,updateType:i}){return!this.options.validate||this.options.validate({type:"Feature",geometry:t,properties:e||{}},{project:this.project,unproject:this.unproject,coordinatePrecision:this.coordinatePrecision,updateType:i}).valid}buildFeatureWithGeometry(t){return{id:t,type:"Feature",properties:this.store.getPropertiesCopy(t),geometry:this.store.getGeometryCopy(t)}}createFeatures(t){return this.store.create(t)}updateFeatureGeometries(t){this.store.updateGeometry(t)}updateFeatureProperties(t){const e=t.map(({featureId:t,properties:e})=>Object.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);s<n.minDistance&&s<this.pointerDistance&&(n.coordinate=i,n.minDistance=s,n.featureId=e.id,n.featureCoordinateIndex=o)})}),n}}function Ft(t){return Math.sqrt(Math.pow(t[0],2)+Math.pow(t[1],2)+Math.pow(t[2],2))}function Ot(t,e){const i=function(t,e){const[i,o,n]=t,[s,r,a]=e;return i*s+o*r+n*a}(t,e)/(Ft(t)*Ft(e));return Math.acos(Math.min(Math.max(i,-1),1))}function xt(t){const e=W(t[1]),i=W(t[0]);return[Math.cos(e)*Math.cos(i),Math.cos(e)*Math.sin(i),Math.sin(e)]}function wt(t){const[e,i,o]=t,n=U(Math.asin(o));return[U(Math.atan2(i,e)),n]}function Mt(t,e,i){const o=xt(t),n=xt(e),s=xt(i),[r,a,d]=s,[h,l,u]=function(t,e){const[i,o,n]=t,[s,r,a]=e;return[o*a-n*r,n*s-i*a,i*r-o*s]}(o,n),c=l*d-u*a,p=u*r-h*d,g=h*a-l*r,y=g*l-p*u,m=c*u-g*h,f=p*h-c*l,C=1/Math.sqrt(Math.pow(y,2)+Math.pow(m,2)+Math.pow(f,2)),P=[y*C,m*C,f*C],v=[-1*y*C,-1*m*C,-1*f*C],I=Ot(o,n),S=Ot(o,P),F=Ot(n,P),O=Ot(o,v),x=Ot(n,v);let w;return w=S<O&&S<x||F<O&&F<x?P:v,Ot(o,w)>I||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<i.length-1;t++)o.push([i[t],i[t+1]]);let s;const r=[t.lng,t.lat];if("web-mercator"===this.config.projection?s=function(t,e){let i=[Infinity,Infinity],o=Infinity,n=0;for(let s of e){const r=s[0],a=s[1];let d,h=Infinity;const l=H(r[0],r[1]),u=H(a[0],a[1]),c=H(t[0],t[1]);if(r[0]===t[0]&&r[1]===t[1])d=r;else if(a[0]===t[0]&&a[1]===t[1])d=a;else{const{x:t,y:e}=Et(l,u,c),{lng:i,lat:o}=G(t,e);d=[i,o]}d&&(h=Ct(c,H(d[0],d[1])),h<o&&(i=d,o=h,n=e.indexOf(s)))}return Infinity===o?void 0:{coordinate:i,lineIndex:n,distance:o}}(r,o):"globe"===this.config.projection&&(s=function(t,e){let i=[Infinity,Infinity],o=Infinity,n=0;for(let s of e){const r=s[0],a=s[1];let d,h=Infinity;r[0]===t[0]&&r[1]===t[1]?d=r:a[0]===t[0]&&a[1]===t[1]?d=a:[d]=Mt(r,a,t),d&&(h=T(t,d),h<o&&(i=d,o=h,n=e.indexOf(s)))}return Infinity===o?void 0:{coordinate:i,distance:o,lineIndex:n}}(r,o)),!s)return;const a=this.pixelDistance.measure(t,s.coordinate);a<n.minDistance&&a<this.pointerDistance&&(n.featureId=e.id,n.coordinate=[L(s.coordinate[0],this.config.coordinatePrecision),L(s.coordinate[1],this.config.coordinatePrecision)],n.featureCoordinateIndex=s.lineIndex,n.minDistance=a)}),n},this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}}class kt{constructor(t,e){this.coordinateSnapping=void 0,this.lineSnapping=void 0,this.coordinateSnapping=t,this.lineSnapping=e}getSnappable(t,e,i,o){const n=!1!==(null==o?void 0:o.toLine),s=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.minDistance<r.minDistance&&(r=e)}if(n){const e=this.lineSnapping.getSnappable(t,s);e.coordinate&&e.minDistance<r.minDistance&&(r=e)}return r}}const Nt={cancel:E,finish:M},bt={start:D};class _t extends x{constructor(t){super(t,!0),this.mode="circle",this.center=void 0,this.endPosition=void 0,this.segments=64,this.currentCircleId=void 0,this.keyEvents=Nt,this.cursors=bt,this.startingRadiusKilometers=1e-5,this.cursorMovedAfterInitialCursorDown=!1,this.drawInteraction="click-move",this.drawType=void 0,this.cancelled=!1,this.minimumSegments=3,this.showCoordinatePoints=!1,this.snapping=void 0,this.snappedPointId=void 0,this.readFeature=void 0,this.coordinatePoints=void 0,this.mutateFeature=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.coordinateSnapping=void 0,this.lineSnapping=void 0,this.featureSnapping=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)),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)),f(null==e?void 0:e.startingRadiusKilometers)&&(this.startingRadiusKilometers=e.startingRadiusKilometers),S(null==e?void 0:e.drawInteraction)&&(this.drawInteraction=e.drawInteraction),f(null==e?void 0:e.segments)){const t=Math.trunc(e.segments);this.segments=t<this.minimumSegments?this.minimumSegments:t}}close(){if(void 0===this.currentCircleId||void 0===this.endPosition)return;if(!this.updateCircle(this.endPosition,s.Finish))return;const t=this.currentCircleId;this.cursorMovedAfterInitialCursorDown=!1,this.center=void 0,this.currentCircleId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:e})}beginDrawing(t,e="click"){var i;this.deleteSnappedPoint(),this.center=null!=(i=this.getSnappedCoordinate(t))?i:[t.lng,t.lat],this.endPosition=[t.lng,t.lat];const o=K({center:this.center,radiusKilometers:this.startingRadiusKilometers,coordinatePrecision:this.coordinatePrecision,steps:this.segments}),n=this.mutateFeature.createPolygon({coordinates:o.geometry.coordinates[0],properties:{mode:this.mode,radiusKilometers:this.startingRadiusKilometers,[d.CURRENTLY_DRAWING]:!0}});n&&(this.currentCircleId=n.id,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:n.id,featureCoordinates:n.geometry.coordinates}),this.cursorMovedAfterInitialCursorDown=!1,this.drawType=e,this.setDrawing())}dragDrawAllowed(){return"click-drag"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}moveDrawAllowed(){return"click-move"===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.center?this.center&&void 0!==this.currentCircleId&&(this.endPosition=[t.lng,t.lat],this.close()):this.beginDrawing(t))}onMouseMove(t){void 0!==this.center?(this.cursorMovedAfterInitialCursorDown=!0,this.endPosition=[t.lng,t.lat],this.updateCircle(this.endPosition,s.Provisional)):this.updateSnappedCoordinate(t)}updateSnappedCoordinate(t){var e;const i=this.getSnappedCoordinate(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.deleteSnappedPoint()}deleteSnappedPoint(){this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0)}getSnappedCoordinate(t){var e,i,o;let n;if(null!=(e=this.snapping)&&e.toCoordinate){const e=this.coordinateSnapping.getSnappableCoordinateFirstClick(t);e&&(n=e)}if(null!=(i=this.snapping)&&i.toFeature){const e=this.featureSnapping.getSnappable(t,void 0,this.snapping.toFeature.filter,{toLine:this.snapping.toFeature.toLine,toCoordinate:this.snapping.toFeature.toCoordinate});e.coordinate&&(n=e.coordinate)}if(null!=(o=this.snapping)&&o.toCustom){const e=this.snapping.toCustom(t,{currentCoordinate:0,currentId:void 0,getCurrentGeometrySnapshot:()=>null,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;t<n;t++){const e=360*t/n*Math.PI/180,i=s*Math.cos(e),r=s*Math.sin(e),[a,u]=[d+i,h+r],{lng:c,lat:p}=G(a,u);l.push([L(c,o),L(p,o)])}return l.push(l[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[l]},properties:{}}}({center:this.center,radiusKilometers:r*e,coordinatePrecision:this.coordinatePrecision,steps:this.segments})}else{if("globe"!==this.projection)throw new Error("Invalid projection");n=K({center:this.center,radiusKilometers:r,coordinatePrecision:this.coordinatePrecision,steps:this.segments})}const a={};n&&void 0!==r&&(a.radiusKilometers=r),o&&(a[d.CURRENTLY_DRAWING]=void 0);const h=this.mutateFeature.updatePolygon({featureId:this.currentCircleId,coordinateMutations:n?{type:lt,coordinates:n.geometry.coordinates}:void 0,propertyMutations:a,context:o?{updateType:i,action:e}:{updateType:i}});return h&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentCircleId,featureCoordinates:h.geometry.coordinates}),h}afterFeatureUpdated(t){this.showCoordinatePoints&&"Polygon"===t.geometry.type&&this.coordinatePoints.createOrUpdate({featureId:t.id,featureCoordinates:t.geometry.coordinates}),this.currentCircleId===t.id&&(this.cursorMovedAfterInitialCursorDown=!1,this.center=void 0,this.currentCircleId=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)}}function Tt(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 Rt({x:t,y:e},i,o){const n=W(o);return{x:t+i*Math.cos(n),y:e+i*Math.sin(n)}}function Wt(t,e,i){return(t=Math.max(t,Number.EPSILON))*(e=Math.max(e,Number.EPSILON))/Math.sqrt(e**2*Math.sin(i)**2+t**2*Math.cos(i)**2)}function At(t){var e;const{center:i,xRadiusKilometers:o,yRadiusKilometers:n,coordinatePrecision:s}=t,r=null!=(e=t.steps)?e:64,a=[];for(let t=0;t<r;t++){const e=Tt(i,Wt(o,n,2*t*Math.PI/r),360*t/r);a.push([L(e[0],s),L(e[1],s)])}return a.push(a[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[a]},properties:{}}}function Ut(t){var e;const{center:i,xRadiusKilometers:o,yRadiusKilometers:n,coordinatePrecision:s}=t,r=null!=(e=t.steps)?e:64,a=1e3*o,d=1e3*n,{x:h,y:l}=H(i[0],i[1]),u=[];for(let t=0;t<r;t++){const e=2*t*Math.PI/r,i=G(h+a*Math.cos(e),l+d*Math.sin(e));u.push([L(i.lng,s),L(i.lat,s)])}return u.push(u[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[u]},properties:{}}}const Lt={cancel:"Escape",finish:"Enter"},Bt={start:"crosshair"};class Vt extends x{constructor(t){super(t,!0),this.mode="ellipse",this.center=void 0,this.endPosition=void 0,this.segments=64,this.currentEllipseId=void 0,this.keyEvents=Lt,this.cursors=Bt,this.startingRadiusKilometers=1e-5,this.cursorMovedAfterInitialCursorDown=!1,this.drawInteraction="click-move",this.drawType=void 0,this.minimumSegments=3,this.showCoordinatePoints=!1,this.cancelled=!1,this.snapping=void 0,this.snappedPointId=void 0,this.readFeature=void 0,this.coordinatePoints=void 0,this.mutateFeature=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.coordinateSnapping=void 0,this.lineSnapping=void 0,this.featureSnapping=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)),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)),f(null==e?void 0:e.startingRadiusKilometers)&&(this.startingRadiusKilometers=e.startingRadiusKilometers),S(null==e?void 0:e.drawInteraction)&&(this.drawInteraction=e.drawInteraction),f(null==e?void 0:e.segments)){const t=Math.trunc(e.segments);this.segments=t<this.minimumSegments?this.minimumSegments:t}}close(){if(void 0===this.currentEllipseId||void 0===this.endPosition)return;if(!this.updateEllipse(this.endPosition,s.Finish))return;const t=this.currentEllipseId;this.cursorMovedAfterInitialCursorDown=!1,this.center=void 0,this.currentEllipseId=void 0,this.drawType=void 0,"drawing"===this.state&&this.setStarted(),this.onFinish(t,{mode:this.mode,action:e})}beginDrawing(t,e="click"){var i;let o;if(this.cancelled=!1,this.deleteSnappedPoint(),this.center=null!=(i=this.getSnappedCoordinate(t))?i:[t.lng,t.lat],this.endPosition=[t.lng,t.lat],"globe"===this.projection)o=At({steps:this.segments,center:this.center,xRadiusKilometers:this.startingRadiusKilometers,yRadiusKilometers:this.startingRadiusKilometers,coordinatePrecision:this.coordinatePrecision});else{if("web-mercator"!==this.projection)throw new Error(`Unsupported projection: ${this.projection}`);o=Ut({steps:this.segments,center:this.center,xRadiusKilometers:this.startingRadiusKilometers,yRadiusKilometers:this.startingRadiusKilometers,coordinatePrecision:this.coordinatePrecision})}const n=this.mutateFeature.createPolygon({coordinates:o.geometry.coordinates[0],properties:{mode:this.mode,xRadiusKilometers:this.startingRadiusKilometers,yRadiusKilometers:this.startingRadiusKilometers,[d.CURRENTLY_DRAWING]:!0}});return!!n&&(this.currentEllipseId=n.id,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:n.id,featureCoordinates:n.geometry.coordinates}),this.cursorMovedAfterInitialCursorDown=!1,this.drawType=e,this.setDrawing(),!0)}dragDrawAllowed(){return"click-drag"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}moveDrawAllowed(){return"click-move"===this.drawInteraction||"click-move-or-drag"===this.drawInteraction}start(){this.setStarted(),this.setCursor(this.cursors.start)}stop(){this.cleanUp(),this.setStopped(),this.setCursor("unset")}onClick(t){const e="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.moveDrawAllowed()&&e&&(this.center?void 0!==this.currentEllipseId&&(this.endPosition=[t.lng,t.lat],this.close()):this.beginDrawing(t))}onMouseMove(t){void 0!==this.center?(this.cursorMovedAfterInitialCursorDown=!0,this.endPosition=[t.lng,t.lat],this.updateEllipse(this.endPosition,s.Provisional)):this.updateSnappedCoordinate(t)}updateSnappedCoordinate(t){var e;const i=this.getSnappedCoordinate(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.deleteSnappedPoint()}deleteSnappedPoint(){this.snappedPointId&&(this.mutateFeature.deleteFeatureIfPresent(this.snappedPointId),this.snappedPointId=void 0)}getSnappedCoordinate(t){var e,i,o;let n;if(null!=(e=this.snapping)&&e.toCoordinate){const e=this.coordinateSnapping.getSnappableCoordinateFirstClick(t);e&&(n=e)}if(null!=(i=this.snapping)&&i.toFeature){const e=this.featureSnapping.getSnappable(t,void 0,this.snapping.toFeature.filter,{toLine:this.snapping.toFeature.toLine,toCoordinate:this.snapping.toFeature.toCoordinate});e.coordinate&&(n=e.coordinate)}if(null!=(o=this.snapping)&&o.toCustom){const e=this.snapping.toCustom(t,{currentCoordinate:0,currentId:void 0,getCurrentGeometrySnapshot:()=>null,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.pointerDistance){if(this.autoClose&&this.hasLeftStartingPoint)return this.preventNewFeature=!0,setTimeout(()=>{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.minDistance)return;const u=this.getSmoothedCoordinate([e,i],[t.lng,t.lat]),c=this.mutateFeature.updatePolygon({featureId:this.currentId,coordinateMutations:[{type:rt,index:-1,coordinate:u}],context:{updateType:s.Provisional}});c&&this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:c.geometry.coordinates})}getSmoothedCoordinate(t,e){if(0===this.smoothing)return e;const[i,o]=t,[n,s]=e;return[i*this.smoothing+n*(1-this.smoothing),o*this.smoothing+s*(1-this.smoothing)]}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.mutateFeature.deleteFeatureIfPresent(this.closingPointId),this.canClose=!1,this.currentId=void 0,this.closingPointId=void 0,this.hasLeftStartingPoint=!1,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(_)}onMouseMove(t){this.moveDrawAllowed()&&"click"===this.drawType&&this.addCoordinate(t)}onClick(t){if(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))){if(this.preventNewFeature)return;if(!1===this.canClose)return void this.beginDrawing(t);if(!this.hasLeftStartingPoint)return;this.close()}}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&!0===this.canClose&&this.close()}onDragStart(t,e){"drawing"!==this.state&&(this.preventNewFeature||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.addCoordinate(t)}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&this.dragDrawAllowed()&&"drag"===this.drawType&&(this.preventNewFeature=!0,setTimeout(()=>{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<t.length&&!(e>=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<e&&t.length===n)throw new Error("Start position is beyond line");const h=t[t.length-1];return[h,h]}function $t(t){return t*(Math.PI/180)}function qt(t){return t*(180/Math.PI)}class Zt extends nt{constructor(t){super(t),this.config=void 0,this.config=t}generateInsertionCoordinates(t,e,i){const o=[t,e];let n=0;for(let t=0;t<o.length-1;t++)n+=T(o[0],o[1]);if(n<=i)return o;let s=n/i-1;Number.isInteger(s)||(s=Math.floor(s)+1);const r=[];for(let t=0;t<s;t++){const e=Xt(o,i*t,i*(t+1));r.push(e)}const a=[];for(let t=0;t<r.length;t++)a.push(r[t][1]);return this.limitCoordinates(a)}generateInsertionGeodesicCoordinates(t,e,i){const o=T(t,e),n=function(t,e,i){const o=[],n=$t(t[1]),s=$t(t[0]),r=$t(e[1]),a=$t(e[0]);i+=1;const d=2*Math.asin(Math.sqrt(Math.sin((r-n)/2)**2+Math.cos(n)*Math.cos(r)*Math.sin((a-s)/2)**2));if(0===d||isNaN(d))return o;for(let t=0;t<=i;t++){const e=t/i,h=Math.sin((1-e)*d)/Math.sin(d),l=Math.sin(e*d)/Math.sin(d),u=h*Math.cos(n)*Math.cos(s)+l*Math.cos(r)*Math.cos(a),c=h*Math.cos(n)*Math.sin(s)+l*Math.cos(r)*Math.sin(a),p=h*Math.sin(n)+l*Math.sin(r);if(isNaN(u)||isNaN(c)||isNaN(p))continue;const g=Math.atan2(p,Math.sqrt(u**2+c**2)),y=Math.atan2(c,u);isNaN(g)||isNaN(y)||o.push([qt(y),qt(g)])}return o.slice(1,-1)}(t,e,Math.floor(o/i));return this.limitCoordinates(n)}limitCoordinates(t){return t.map(t=>[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;i<t.geometry.coordinates.length;i++){if(!q(t.geometry.coordinates[i]))return{valid:!1,reason:"Feature has invalid coordinates"};if(!$(t.geometry.coordinates[i],e))return{valid:!1,reason:"Feature has coordinates with excessive precision"}}return{valid:!0}}class Qt extends nt{constructor(t,e,i,o){super(t),this.config=void 0,this.pixelDistance=void 0,this.mutateFeatureBehavior=void 0,this.readFeatureBehavior=void 0,this._startEndPoints=[],this.config=t,this.pixelDistance=e,this.mutateFeatureBehavior=i,this.readFeatureBehavior=o}get ids(){return this._startEndPoints.concat()}set ids(t){}create(t){if(this.ids.length)throw new Error("Opening and closing points already created");const e=gt(t),i=mt(t);if(e){if(i.length<=3)throw new Error("Requires at least 4 coordinates");this._startEndPoints=this.mutateFeatureBehavior.createGuidancePoints({coordinates:[i[0],i[i.length-2]],type:d.CLOSING_POINT})}else this._startEndPoints=[this.mutateFeatureBehavior.createGuidancePoint({coordinate:i[i.length-2],type:d.CLOSING_POINT})]}delete(){this.ids.length&&(this.mutateFeatureBehavior.deleteFeaturesIfPresent(this.ids),this._startEndPoints=[])}updateOne(t,e){this.mutateFeatureBehavior.updateGuidancePoints([{featureId:this.ids[t],coordinate:e}])}update(t){const e=mt(t);1!==this.ids.length?2===this.ids.length&&this.mutateFeatureBehavior.updateGuidancePoints([{featureId:this.ids[0],coordinate:e[0]},{featureId:this.ids[1],coordinate:e[e.length-3]}]):this.mutateFeatureBehavior.updateGuidancePoints([{featureId:this.ids[0],coordinate:e[e.length-2]}])}isLineStringClosingPoint(t){if(1!==this.ids.length)return{isClosing:!1};const e=this.readFeatureBehavior.getGeometry(this.ids[0]);return{isClosing:this.pixelDistance.measure(t,e.coordinates)<this.pointerDistance}}isPolygonClosingPoints(t){if(2!==this.ids.length)return{isClosing:!1,isPreviousClosing:!1};const e=this.readFeatureBehavior.getGeometry(this.ids[0]),i=this.readFeatureBehavior.getGeometry(this.ids[1]),o=this.pixelDistance.measure(t,e.coordinates),n=this.pixelDistance.measure(t,i.coordinates);return{isClosing:o<this.pointerDistance,isPreviousClosing:n<this.pointerDistance}}}class te{constructor(t){this.undoHistory=[],this.redoHistory=[],this.cloneCoordinatesFunction=void 0,this.maxStackSize=void 0,this.cloneCoordinatesFunction=t=>this.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)<this.pointerDistance&&(l=t)}return l?[L(l[0],this.coordinatePrecision),L(l[1],this.coordinatePrecision)]:void 0}getDegree(t){return"number"==typeof t&&Number.isFinite(t)&&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)<this.EPSILON||i.push(t)}return i}snapOnWebMercator(t,e,i,o,n){const s=H(t[0],t[1]),r=H(e[0],e[1]),a=H(i[0],i[1]),d=this.projectedCandidates(s,r,a,o,n),h=this.closestPoint(a,d);if(!h)return;const l=G(h.x,h.y);return[l.lng,l.lat]}snapOnGlobe(t,e,i,o,n){const s=jt(e,t)+180,r=jt(e,i),a=T(e,i);let d,h=Infinity;for(const t of this.offsets(o,n)){const o=s+t,n=this.angleDifference(r,o),l=a*Math.cos(n*Math.PI/180);if(l<=this.EPSILON)continue;const u=Tt(e,l,o),c=T(i,u);c<h&&(d=u,h=c)}return d}projectedCandidates(t,e,i,o,n){const s=Math.atan2(e.y-t.y,e.x-t.x),r=[];for(const t of this.offsets(o,n)){const o=s+t*Math.PI/180,n=Math.cos(o),a=Math.sin(o),d=(i.x-e.x)*n+(i.y-e.y)*a;d<=this.EPSILON||r.push({x:e.x+d*n,y:e.y+d*a})}return r}getClosingCandidate(t,e,i,o,n,s){const r=this.toLocalPoint(e,e),a=this.toLocalPoint(t,e),d=this.toLocalPoint(i,e),h=this.toLocalPoint(o,e),l=Math.atan2(r.y-a.y,r.x-a.x),u=[];for(const t of this.offsets(n,s)){const e=l+t*Math.PI/180,i=Math.cos(e),o=Math.sin(e),s=d.x-r.x,a=d.y-r.y,c=s*i+a*o,p=Math.max(0,s*s+a*a-c*c),g=Math.max(this.EPSILON,(h.x-r.x)*i+(h.y-r.y)*o),y=this.angle(r,{x:r.x+g*i,y:r.y+g*o},d);if(void 0!==y)for(const t of this.closingDegrees(n,y)){const e=t*Math.PI/180,n=Math.sin(e),s=Math.abs(Math.cos(e))/Math.abs(n)*Math.sqrt(p);for(const e of[c-s,c+s]){if(e<=this.EPSILON)continue;const n={x:r.x+e*i,y:r.y+e*o};this.hasAngle(r,n,d,t)&&u.push(n)}}}const c=this.closestPoint(h,u);return c?this.fromLocalPoint(c,e):void 0}closingDegrees(t,e){const i=Math.ceil(180/t)-1;if(i<1)return[];const o=e/t,n=Math.max(1,Math.min(i,Math.floor(o))),s=Math.max(1,Math.min(i,Math.ceil(o)));return[...new Set([n*t,s*t])]}hasAngle(t,e,i,o){const n=this.angle(t,e,i);return void 0!==n&&Math.abs(n-o)<1e-6}angle(t,e,i){const o=t.x-e.x,n=t.y-e.y,s=i.x-e.x,r=i.y-e.y,a=Math.hypot(o,n)*Math.hypot(s,r);if(0===a)return;const d=Math.max(-1,Math.min(1,(o*s+n*r)/a));return 180*Math.acos(d)/Math.PI}toLocalPoint(t,e){if("web-mercator"===this.projection)return H(t[0],t[1]);const i=T(e,t),o=jt(e,t)*Math.PI/180;return{x:i*Math.sin(o),y:i*Math.cos(o)}}fromLocalPoint(t,e){if("web-mercator"===this.projection){const e=G(t.x,t.y);return[e.lng,e.lat]}return Tt(e,Math.hypot(t.x,t.y),180*Math.atan2(t.x,t.y)/Math.PI)}closestPoint(t,e){return e.reduce((e,i)=>e?this.distance(t,i)<this.distance(t,e)?i:e:i,void 0)}distance(t,e){return Math.hypot(t.x-e.x,t.y-e.y)}pixelDistance(t,e){const i=this.project(t[0],t[1]),o=this.project(e[0],e[1]);return this.distance(i,o)}angleDifference(t,e){return(t-e+540)%360-180}}const ie={cancel:E,finish:M},oe={start:D,close:k,dragStart:N,dragEnd:D};class ne extends x{constructor(t){super(t,!0),this.mode="linestring",this.currentCoordinate=0,this.currentId=void 0,this.keyEvents=ie,this.snapping=void 0,this.cursors=oe,this.mouseMove=!1,this.insertCoordinates=void 0,this.lastCommittedCoordinates=void 0,this.snappedPointId=void 0,this.lastMouseMoveEvent=void 0,this.showCoordinatePoints=!1,this.finishOnNthCoordinate=void 0,this.editable=!1,this.editedFeatureId=void 0,this.editedFeatureCoordinateIndex=void 0,this.editedSnapType=void 0,this.editedInsertIndex=void 0,this.editedPointId=void 0,this.coordinateSnapping=void 0,this.insertPoint=void 0,this.lineSnapping=void 0,this.featureSnapping=void 0,this.degreeSnapping=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.mutateFeature=void 0,this.readFeature=void 0,this.closingPoints=void 0,this.coordinatePoints=void 0,this.undoRedo=void 0,this.updateOptions(t)}updateOptions(e){var i;super.updateOptions(e),void 0!==(null==e?void 0:e.finishOnNthCoordinate)&&Number.isInteger(e.finishOnNthCoordinate)&&e.finishOnNthCoordinate>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;e<i.length;e++){const s=i[e];if("Point"!==s.geometry.type||s.properties.mode!==this.mode||s.properties[d.SNAPPING_POINT])continue;const r=this.pixelDistance.measure(t,s.geometry.coordinates);r>n||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<t.length-1;r++){let a;if("web-mercator"===n)a=Oe(t[r],t[r+1],e,o,i);else{if("globe"!==n)throw new Error("Invalid projection");a=xe(t[r],t[r+1],e)}s.push(a)}return s}class Me extends nt{constructor(t,e,i,o,n,s){super(t),this.config=void 0,this.selectionPointBehavior=void 0,this.coordinatePointBehavior=void 0,this.mutateFeature=void 0,this.readFeature=void 0,this.pixelDistance=void 0,this._midPoints=[],this.config=t,this.selectionPointBehavior=e,this.coordinatePointBehavior=i,this.mutateFeature=o,this.readFeature=n,this.pixelDistance=s}getMidpointConfig(t){return{featureCoords:t,precision:this.coordinatePrecision,project:this.config.project,unproject:this.config.unproject,projection:this.config.projection}}get ids(){return this._midPoints.concat()}set ids(t){}getNearestMidPoint(t){let e,i=Infinity;return this.ids.forEach(o=>{const n=this.readFeature.getGeometry(o),s=this.pixelDistance.measure(t,n.coordinates);s<this.pointerDistance&&s<i&&(i=s,e=o)}),e}insert({featureId:t,midPointId:e}){const i=this.readFeature.getGeometry(e),{midPointFeatureId:o,midPointSegment:n}=this.readFeature.getProperties(e),r=this.readFeature.getGeometry(o),a={featureId:o,coordinateMutations:[{type:at,index:n,coordinate:i.coordinates}],context:{updateType:s.Commit}};let h=null;if("Polygon"===r.type)h=this.mutateFeature.updatePolygon(a);else{if("LineString"!==r.type)throw new Error("Midpoints can only be added to polygons or linestrings");h=this.mutateFeature.updateLineString(a)}if(!h)throw new Error("Failed to insert midpoint coordinate");const l=h.geometry.coordinates;this.readFeature.getProperties(t)[d.COORDINATE_POINT_IDS]&&this.coordinatePointBehavior.createOrUpdate({featureId:t,featureCoordinates:l}),this.mutateFeature.deleteFeaturesIfPresent([...this.selectionPointBehavior.ids,...this._midPoints]),this.create({featureCoordinates:l,featureId:o}),this.selectionPointBehavior.create({featureCoordinates:l,featureId:t})}create({featureCoordinates:t,featureId:e}){if(!this.readFeature.hasFeature(e))throw new Error("Store does not have feature with this id");const i=mt(t),o=we(this.getMidpointConfig(i));this._midPoints=this.mutateFeature.createGuidancePoints({additionalProperties:t=>({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<a;r++){const a=e[r];for(let e=0,r=a.length,d=r-1;e<r;d=e++)(n=a[e])[1]>(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;d<h.length;d++){const l=h[d],u=l.geometry;if("Point"===u.type){if(l.properties.selectionPoint||l.properties.coordinatePoint||!e&&l.properties[a.MID_POINT])continue;const o=this.pixelDistance.measure(t,u.coordinates);!l.properties[a.MID_POINT]&&o<this.pointerDistance&&o<s&&(s=o,i=l)}else if("LineString"===u.type){if(i)continue;for(let e=0;e<u.coordinates.length-1;e++){const i=u.coordinates[e],n=u.coordinates[e+1],s=ke({x:t.containerX,y:t.containerY},this.project(i[0],i[1]),this.project(n[0],n[1]));s<this.pointerDistance&&s<r&&(r=s,o=l)}}else if("Polygon"===u.type){if(i||o)continue;De([t.lng,t.lat],u.coordinates)&&(n=l)}}return{clickedFeature:i||o||n}}}class be extends nt{constructor(t,e,i,o,n,s,r){super(t),this.config=void 0,this.featuresAtCursorEvent=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.readFeature=void 0,this.mutateFeature=void 0,this.draggedFeatureId=null,this.dragPosition=void 0,this.config=t,this.featuresAtCursorEvent=e,this.selectionPoints=i,this.midPoints=o,this.coordinatePoints=n,this.readFeature=s,this.mutateFeature=r}startDragging(t,e){this.draggedFeatureId=e,this.dragPosition=[t.lng,t.lat]}stopDragging(){this.draggedFeatureId=null,this.dragPosition=void 0}isDragging(){return null!==this.draggedFeatureId}canDrag(t,e){const{clickedFeature:i}=this.featuresAtCursorEvent.find(t,!0);return!(!i||i.id!==e)}drag(t){if(!this.draggedFeatureId)return;if(!1===this.readFeature.hasFeature(this.draggedFeatureId))return this.stopDragging(),!1;const e=this.readFeature.getGeometry(this.draggedFeatureId),i=[t.lng,t.lat];if("Polygon"===e.type||"LineString"===e.type){let o,n;if("Polygon"===e.type?(o=e.coordinates[0],n=o.length-1):(o=e.coordinates,n=o.length),!this.dragPosition)return!1;for(let t=0;t<n;t++){const e=o[t];let n,s;if("web-mercator"===this.config.projection){const t=H(this.dragPosition[0],this.dragPosition[1]),o=H(i[0],i[1]),r=H(e[0],e[1]),a={x:t.x-o.x,y:t.y-o.y},d=r.x-a.x,h=r.y-a.y,{lng:l,lat:u}=G(d,h);n=l,s=u}else{const t=[this.dragPosition[0]-i[0],this.dragPosition[1]-i[1]];n=e[0]-t[0],s=e[1]-t[1]}if(n=L(n,this.config.coordinatePrecision),s=L(s,this.config.coordinatePrecision),n>180||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;n<o.length;n++){const s=this.pixelDistance.measure(t,o[n]);if(s<this.pointerDistance&&s<i.dist){const t="Polygon"===e.type&&(n===o.length-1||0===n);i.dist=s,i.index=t?0:n,i.isFirstOrLastPolygonCoord=t}}return i}getDraggable(t,e){const i=this.readFeature.getGeometry(e);return this.getClosestCoordinate(t,i)}getDraggableIndex(t,e){const i=this.readFeature.getGeometry(e),o=this.getClosestCoordinate(t,i);return-1===o.index?-1:o.index}snapCoordinate(t,e,i){let o=[t.lng,t.lat];const n=t=>Boolean(t.properties&&t.properties.mode===i.properties.mode&&t.id!==this.draggedCoordinate.id);if(null!=e&&e.toLine){let e;e=this.lineSnapping.getSnappable(t,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<n-1;s++){const[n,r]=t[s],[a,d]=t[s+1],h=n*d-a*r;e+=h,i+=(n+a)*h,o+=(r+d)*h}return e/=2,i/=6*e,o/=6*e,{x:i,y:o}}(e):function(t){const e=t.length;let i=0,o=0;for(let n=0;n<e;n++){const[e,s]=t[n];i+=e,o+=s}return{x:i/e,y:o/e}}(e)}class Ae extends nt{constructor(t,e,i,o,n,s){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.coordinatePoints=void 0,this.readFeature=void 0,this.mutateFeature=void 0,this.lastBearing=void 0,this.selectedGeometry=void 0,this.selectedGeometryCentroid=void 0,this.selectedGeometryWebMercatorCentroid=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i,this.coordinatePoints=o,this.readFeature=n,this.mutateFeature=s}reset(){this.lastBearing=void 0,this.selectedGeometry=void 0,this.selectedGeometryWebMercatorCentroid=void 0,this.selectedGeometryCentroid=void 0}rotate(t,e){this.selectedGeometry||(this.selectedGeometry=this.readFeature.getGeometry(e));const i=this.selectedGeometry;if("Polygon"!==i.type&&"LineString"!==i.type)return;const o=[t.lng,t.lat];let n;const r={type:"Feature",geometry:i,properties:{}};if("web-mercator"===this.config.projection){this.selectedGeometryWebMercatorCentroid||(this.selectedGeometryWebMercatorCentroid=We(r));const e=H(t.lng,t.lat);if(n=Kt(this.selectedGeometryWebMercatorCentroid,e),0===n)return;if(!this.lastBearing)return void(this.lastBearing=n);Re(r,-(this.lastBearing-n))}else{if("globe"!==this.config.projection)throw new Error("Unsupported projection");if(this.selectedGeometryCentroid||(this.selectedGeometryCentroid=Te({type:"Feature",geometry:i,properties:{}})),n=Se(this.selectedGeometryCentroid,o),!this.lastBearing)return void(this.lastBearing=n+180);!function(t,e){if(0===e||360===e||-360===e)return t;const i=Te(t);("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{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<o.length;n++){const s=this.pixelDistance.measure(t,o[n]);if(s<this.pointerDistance&&s<i.dist){const t="Polygon"===e.type&&(n===o.length-1||0===n);i.dist=s,i.index=t?0:n,i.isFirstOrLastPolygonCoord=t}}return i}isValidDragWebMercator(t,e,i){switch(t){case 0:if(e<=0||i>=0)return!1;break;case 1:if(i>=0)return!1;break;case 2:if(e>=0||i>=0)return!1;break;case 3:if(e>=0)return!1;break;case 4:if(e>=0||i<=0)return!1;break;case 5:if(i<=0)return!1;break;case 6:if(e<=0||i<=0)return!1;break;case 7:if(e<=0)return!1}return!0}getSelectedFeatureDataWebMercator(){if(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<this.minimumScale&&(s=this.minimumScale),Le({coordinates:n,originX:e.x,originY:e.y,xScale:s,yScale:s}),n):null}oppositeFixedWebMercatorDrag(t){var e,i;const o=this.getSelectedFeatureDataWebMercator();if(!o)return null;const{boundingBox:n,updatedCoords:s,selectedCoordinate:r}=o,a=H(r[0],r[1]),d=this.getIndexesWebMercator(n,a),h=null!=(e=this.oppositeClosestBBoxIndex)?e:d.closestBBoxIndex;this.oppositeClosestBBoxIndex=h;const l=null!=(i=this.oppositeOrigin)?i:{x:n[d.oppositeBboxIndex][0],y:n[d.oppositeBboxIndex][1]};if(!Number.isFinite(l.x)||!Number.isFinite(l.y))return null;this.oppositeOrigin=l;const u=H(t.lng,t.lat);return this.scaleFixedWebMercator({closestBBoxIndex:h,updatedCoords:s,webMercatorCursor:u,webMercatorSelected:a,webMercatorOrigin:l})}oppositeWebMercatorDrag(t){var e,i;const o=this.getSelectedFeatureDataWebMercator();if(!o)return null;const{boundingBox:n,updatedCoords:s,selectedCoordinate:r}=o,a=H(r[0],r[1]),d=this.getIndexesWebMercator(n,a),h=null!=(e=this.oppositeClosestBBoxIndex)?e:d.closestBBoxIndex;this.oppositeClosestBBoxIndex=h;const l=null!=(i=this.oppositeOrigin)?i:{x:n[d.oppositeBboxIndex][0],y:n[d.oppositeBboxIndex][1]};if(!Number.isFinite(l.x)||!Number.isFinite(l.y))return null;this.oppositeOrigin=l;const u=H(t.lng,t.lat);return this.scaleWebMercator({closestBBoxIndex:h,updatedCoords:s,webMercatorCursor:u,webMercatorSelected:a,webMercatorOrigin:l})}scaleWebMercator({closestBBoxIndex:t,webMercatorOrigin:e,webMercatorSelected:i,webMercatorCursor:o,updatedCoords:n}){const s=e.x-o.x,r=e.y-o.y;if(!this.isValidDragWebMercator(t,s,r))return null;let a=1;0!==s&&1!==t&&5!==t&&(a=1-(e.x-i.x-s)/s);let d=1;return 0!==r&&3!==t&&7!==t&&(d=1-(e.y-i.y-r)/r),this.validateScale(a,d)?(a<0&&(a=this.minimumScale),d<0&&(d=this.minimumScale),this.performWebMercatorScale(n,e.x,e.y,a,d),n):null}getFeature(t){if(null===this.draggedCoordinate.id)return null;const e=this.readFeature.getGeometry(t);return"Polygon"!==e.type&&"LineString"!==e.type?null:{id:t,type:"Feature",geometry:e,properties:{}}}getNormalisedCoordinates(t){return"Polygon"===t.type?t.coordinates[0]:t.coordinates}validateScale(t,e){const i=Number.isFinite(t)&&t<Number.MAX_SAFE_INTEGER,o=Number.isFinite(e)&&e<Number.MAX_SAFE_INTEGER;return i&&o}performWebMercatorScale(t,e,i,o,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})}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])=>{t<e[0]&&(e[0]=t),i<e[1]&&(e[1]=i),t>e[2]&&(e[2]=t),i>e[3]&&(e[3]=i)});const[i,o,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<t.length;n++){const s=Ct({x:e.x,y:e.y},{x:t[n][0],y:t[n][1]});s<o&&(i=n,o=s)}if(void 0===i)throw new Error("No closest coordinate found");return{oppositeBboxIndex:this.boundingBoxMaps.opposite[i],closestBBoxIndex:i}}isDragging(){return null!==this.draggedCoordinate.id}startDragging(t,e){this.draggedCoordinate={id:t,index:e},this.centerOrigin=null,this.oppositeOrigin=null,this.oppositeClosestBBoxIndex=null}stopDragging(){this.draggedCoordinate={id:null,index:-1},this.centerOrigin=null,this.oppositeOrigin=null,this.oppositeClosestBBoxIndex=null}getDraggableIndex(t,e){const i=this.readFeature.getGeometry(e),o=this.getClosestCoordinate(t,i);return-1===o.index?-1:o.index}drag(t,e){if(null===this.draggedCoordinate.id)return!1;const i=this.getFeature(this.draggedCoordinate.id);if(!i)return!1;let o=null;if("center"===e?o=this.centerWebMercatorDrag(t):"opposite"===e?o=this.oppositeWebMercatorDrag(t):"center-fixed"===e?o=this.centerFixedWebMercatorDrag(t):"opposite-fixed"===e&&(o=this.oppositeFixedWebMercatorDrag(t)),!o)return!1;for(let t=0;t<o.length;t++){const e=o[t];if(e[0]=L(e[0],this.coordinatePrecision),e[1]=L(e[1],this.coordinatePrecision),!$(e,this.coordinatePrecision))return!1}const n=i.id;let r=null;if("Polygon"===i.geometry.type?r=this.mutateFeature.updatePolygon({featureId:n,coordinateMutations:{type:lt,coordinates:[o]},context:{updateType:s.Provisional}}):"LineString"===i.geometry.type&&(r=this.mutateFeature.updateLineString({featureId:n,coordinateMutations:{type:lt,coordinates:o},context:{updateType:s.Provisional}})),!r)return!1;const a=r.geometry.coordinates;return this.midPoints.updateAllInPlace({featureCoordinates:a}),this.selectionPoints.updateAllInPlace({featureCoordinates:a}),this.coordinatePoints.updateAllInPlace({featureId:n,featureCoordinates:a}),!0}}const Ve={deselect:E,delete:"Delete",rotate:["Control","r"],scale:["Control","s"]},ze={pointerOver:b,dragStart:b,dragEnd:b,insertMidpoint:D};class He extends w{getPointerOverFeatureCursor(){var t;return null!=(t=this.cursors.pointerOverFeature)?t:this.cursors.pointerOver}getPointerOverCoordinateCursor(){var t;return null!=(t=this.cursors.pointerOverCoordinate)?t:this.cursors.pointerOver}getPointerOverResizeHandleCursor(){var t;return null!=(t=this.cursors.pointerOverResizeHandle)?t:this.cursors.pointerOver}constructor(t){super(t,!0),this.mode="select",this.allowManualDeselection=!0,this.allowManualSelection=!0,this.dragEventThrottle=5,this.dragEventCount=0,this.selected=[],this.flags={},this.keyEvents=Ve,this.cursors=ze,this.validations={},this.dragTarget={type:"none"},this.selectionPoints=void 0,this.midPoints=void 0,this.coordinateSnap=void 0,this.featuresAtMouseEvent=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.dragFeature=void 0,this.dragCoordinate=void 0,this.rotateFeature=void 0,this.scaleFeature=void 0,this.dragCoordinateResizeFeature=void 0,this.coordinatePoints=void 0,this.lineSnap=void 0,this.mutateFeature=void 0,this.readFeature=void 0,this.updateOptions(t)}updateOptions(e){if(super.updateOptions(e),this.cursors=v(null==e?void 0:e.cursors)?t({},this.cursors,e.cursors):ze,P(null==e?void 0:e.keyEvents)?this.keyEvents={deselect:null,delete:null,rotate:null,scale:null}:v(null==e?void 0:e.keyEvents)&&(this.keyEvents=t({},this.keyEvents,e.keyEvents)),f(null==e?void 0:e.dragEventThrottle)&&(this.dragEventThrottle=e.dragEventThrottle),C(null==e?void 0:e.allowManualDeselection)&&(this.allowManualDeselection=e.allowManualDeselection),C(null==e?void 0:e.allowManualSelection)&&(this.allowManualSelection=e.allowManualSelection),v(null==e?void 0:e.flags)){this.flags=t({},this.flags,e.flags),this.validations={};for(const t in this.flags){const e=this.flags[t].feature;e&&e.validation&&(this.validations[t]=e.validation)}}}selectFeature(t){this.select(t,!1)}setSelecting(){if("started"!==this._state)throw new Error("Mode must be started to move to selecting state");this._state="selecting"}registerBehaviors(t){this.readFeature=new pt(t),this.mutateFeature=new ut(t,{validate:(t,e)=>{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);s<this.pointerDistance&&s<i&&(i=s,e=this.readFeature.getProperties(o))}),!e)return;const n=e.selectionPointFeatureId,r=e.index,a=this.readFeature.getProperties(n),d=this.flags[a.mode];if(!(d&&d.feature&&d.feature.coordinates&&d.feature.coordinates.deletable))return;const h=this.readFeature.getGeometry(n);let l;if("Polygon"===h.type){if(l=h.coordinates[0],l.length<=4)return}else{if("LineString"!==h.type)return;if(l=h.coordinates,l.length<=2)return}"Polygon"!==h.type||0!==r&&r!==l.length-1?l.splice(r,1):(l.shift(),l.pop(),l.push([l[0][0],l[0][1]]));let u=null;if("Polygon"===h.type?u=this.mutateFeature.updatePolygon({featureId:n,coordinateMutations:{type:lt,coordinates:[l]},context:{updateType:s.Commit}}):"LineString"===h.type&&(u=this.mutateFeature.updateLineString({featureId:n,coordinateMutations:{type:lt,coordinates:l},context:{updateType:s.Commit}})),!u)return;const c=u.geometry.coordinates;this.mutateFeature.deleteFeaturesIfPresent([...this.midPoints.ids,...this.selectionPoints.ids]),a.coordinatePointIds&&this.coordinatePoints.createOrUpdate({featureId:n,featureCoordinates:c}),this.selectionPoints.create({featureCoordinates:c,featureId:n}),d&&d.feature&&d.feature.coordinates&&d.feature.coordinates.midpoints&&this.midPoints.create({featureCoordinates:c,featureId:n}),this.onFinish(n,{action:o,mode:this.mode})}select(t,e=!0){if(this.selected[0]===t)return;const{mode:i}=this.readFeature.getProperties(t),o=this.flags[i];if(!o||!o.feature)return;const n=this.selected[0];if(n){if(n===t)return;this.deselect(n)}e&&this.setCursor(this.getPointerOverFeatureCursor()),this.selected=[t],this.mutateFeature.setSelected(t),this.onSelect(t);const{type:s,coordinates:r}=this.readFeature.getGeometry(t);"LineString"!==s&&"Polygon"!==s||r&&o&&o.feature.coordinates&&(this.selectionPoints.create({featureCoordinates:r,featureId:t}),o.feature.coordinates.midpoints&&this.midPoints.create({featureCoordinates:r,featureId:t}))}onLeftClick(t){const{clickedFeature:e}=this.featuresAtMouseEvent.find(t,this.selected.length>0),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)<n)return;return this.dragTarget={type:"coordinate",featureId:e,coordinateIndex:i},void this.setCursor(this.getPointerOverCoordinateCursor())}}if(n&&n.resizable){const i=this.dragCoordinateResizeFeature.getDraggableIndex(t,e);if(i>-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);r<a;){for(Ke(t,r,a),r++,a--;n(t[r],s)<0;)r++;for(;n(t[a],s)>0;)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<i;s++){const e=t.children[s];$e(n,t.leaf?o(e):e)}return n}function $e(t,e){return t.minX=Math.min(t.minX,e.minX),t.minY=Math.min(t.minY,e.minY),t.maxX=Math.max(t.maxX,e.maxX),t.maxY=Math.max(t.maxY,e.maxY),t}function qe(t,e){return t.minX-e.minX}function Ze(t,e){return t.minY-e.minY}function Je(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function Qe(t){return t.maxX-t.minX+(t.maxY-t.minY)}function ti(t,e){const i=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),n=Math.min(t.maxX,e.maxX),s=Math.min(t.maxY,e.maxY);return Math.max(0,n-i)*Math.max(0,s-o)}function ei(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function ii(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=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<e.children.length;s++){const r=e.children[s],a=e.leaf?o(r):r;ii(t,a)&&(e.leaf?i.push(r):ei(t,a)?this._all(r,i):n.push(r))}e=n.pop()}return i}collides(t){let e=this.data;if(ii(t,e)){const i=[];for(;e;){for(let o=0;o<e.children.length;o++){const n=e.children[o],s=e.leaf?this.toBBox(n):n;if(ii(t,s)){if(e.leaf||ei(t,s))return!0;i.push(n)}}e=i.pop()}}return!1}load(t){if(t.length<this._minEntries){for(let e=0;e<t.length;e++)this.insert(t[e]);return}let e=this._build(t.slice(),0,t.length-1,0);if(this.data.children.length)if(this.data.height===e.height)this._splitRoot(this.data,e);else{if(this.data.height<e.height){const t=this.data;this.data=e,e=t}this._insert(e,this.data.height-e.height-1,!0)}else this.data=e}insert(t){this._insert(t,this.data.height-1)}clear(){this.data=oi([])}remove(t){let e=this.data;const i=this.toBBox(t),o=[],n=[];let s,r,a=!1;for(;e||o.length;){if(e||(e=o.pop(),r=o[o.length-1],s=n.pop(),a=!0),e.leaf){const i=e.children.indexOf(t);-1!==i&&(e.children.splice(i,1),o.push(e),this._condense(o))}a||e.leaf||!ei(e,i)?r?(s++,e=r.children[s],a=!1):e=null:(o.push(e),n.push(s),s=0,r=e,e=e.children[0])}}toBBox(t){return t}compareMinX(t,e){return t.minX-e.minX}compareMinY(t,e){return t.minY-e.minY}_all(t,e){const i=[];for(;t;)t.leaf?e.push(...t.children):i.push(...t.children),t=i.pop();return e}_build(t,e,i,o){const n=i-e+1;let s,r=this._maxEntries;if(n<=r)return s=oi(t.slice(e,i+1)),Ye(s,this.toBBox),s;o||(o=Math.ceil(Math.log(n)/Math.log(r)),r=Math.ceil(n/Math.pow(r,o-1))),s=oi([]),s.leaf=!1,s.height=o;const a=Math.ceil(n/r),d=a*Math.ceil(Math.sqrt(r));ni(t,e,i,d,this.compareMinX);for(let n=e;n<=i;n+=d){const e=Math.min(n+d-1,i);ni(t,n,e,a,this.compareMinY);for(let i=n;i<=e;i+=a){const n=Math.min(i+a-1,e);s.children.push(this._build(t,i,n,o-1))}}return Ye(s,this.toBBox),s}_chooseSubtree(t,e,i,o){for(;o.push(e),!e.leaf&&o.length-1!==i;){let i,o=Infinity,r=Infinity;for(let a=0;a<e.children.length;a++){const d=e.children[a],h=Je(d),l=(n=t,s=d,(Math.max(s.maxX,n.maxX)-Math.min(s.minX,n.minX))*(Math.max(s.maxY,n.maxY)-Math.min(s.minY,n.minY))-h);l<r?(r=l,o=h<o?h:o,i=d):l===r&&h<o&&(o=h,i=d)}e=i||e.children[0]}var n,s;return e}_insert(t,e,i){const o=i?t:this.toBBox(t),n=[],s=this._chooseSubtree(o,this.data,e,n);for(s.children.push(t),$e(s,o);e>=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<n?(n=d,o=r,s=h<s?h:s):d===n&&h<s&&(s=h,o=r)}return o||i-e}_chooseSplitAxis(t,e,i){const o=t.leaf?this.compareMinX:qe,n=t.leaf?this.compareMinY:Ze;this._allDistMargin(t,e,i,o)<this._allDistMargin(t,e,i,n)&&t.children.sort(o)}_allDistMargin(t,e,i,o){t.children.sort(o);const n=this.toBBox,s=Xe(t,0,e,n),r=Xe(t,i-e,i,n);let a=Qe(s)+Qe(r);for(let o=e;o<i-e;o++){const e=t.children[o];$e(s,t.leaf?n(e):e),a+=Qe(s)}for(let o=i-e-1;o>=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<o.length;t++)i.push(o[t][1]),e.push(o[t][0]);const n=Math.min(...i),s=Math.max(...i);return{minX:Math.min(...e),minY:n,maxX:Math.max(...e),maxY:s}}insert(t){if(this.idToNode.get(String(t.id)))throw new Error("Feature already exists");const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}load(t){const e=[],i=new Set;t.forEach(t=>{const o=this.toBBox(t);if(this.setMaps(t,o),i.has(String(t.id)))throw new Error(`Duplicate feature ID found ${t.id}`);i.add(String(t.id)),e.push(o)}),this.tree.load(e)}update(t){this.remove(t.id);const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}remove(t){const e=this.idToNode.get(t);if(!e)throw new Error(`${t} not inserted into the spatial index`);this.tree.remove(e)}clear(){this.tree.clear()}search(t){return this.tree.search(this.toBBox(t)).map(t=>this.nodeToId.get(t))}collides(t){return this.tree.collides(this.toBBox(t))}}const 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.length;t++)i-=Math.abs(gi(e[t]))}return i}const ci=20294876564838.72,pi=Math.PI/180;function gi(t){const e=t.length;if(e<=2)return 0;let i=0,o=0;for(;o<e;)i+=(t[o+2>=e?(o+2)%e:o+2][0]*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)<e?{valid:!1,reason:yi}:{valid:!0},fi=(t,e)=>"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)<Ct(a,r),h=Ii(n,s,a),l=d?90-h:Ii(n,s,a)-90,u=Ct(s,a),c=Math.cos(W(l))*u,p=Kt(n,r)+("right"===function(t,e,i){const o=(i.x-e.x)*(t.y-e.y)-(i.y-e.y)*(t.x-e.x);return o>1e-10?"left":o<-1e-10?"right":"left"}(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)<h?r:s,u=Kt(d,s),c=Kt(d,r),p=Kt(d,a),g=Yt(c),y=Yt(p),m=Yt(u);if(this.notInSector({normalizedCursor:m,normalizedStart:g,normalizedEnd:y,direction:this.direction}))return;const f=this.getDeltaBearing(this.direction,g,y),C=this.arcPoints,P=("anticlockwise"===this.direction?1:-1)*f/C,v=Ct(d,l),I=[];for(let t=0;t<=C;t++){const i=Rt(d,v,g+t*P),{lng:o,lat:n}=G(i.x,i.y),s=[L(o,this.coordinatePrecision),L(n,this.coordinatePrecision)];(s[0]!==e[e.length-1][0]||s[1]!==e[e.length-1][1])&&I.unshift(s)}return e.push(...I),e.push(e[0]),e}getUpdatedLineStringCoordinates(t){if(void 0===this.currentInitialArcId||void 0===this.currentStartingPointId||this.currentCoordinate<2)return;const e=this.readFeature.getGeometry(this.currentInitialArcId).coordinates,i=this.readFeature.getGeometry(this.currentStartingPointId).coordinates,o=e[0],n=[t.lng,t.lat],s=H(o[0],o[1]),r=H(n[0],n[1]),a=H(i[0],i[1]),d=Ct(a,s);if(void 0===this.direction){const t=wi(a,s,r);this.direction=t?"clockwise":"anticlockwise"}const h=Kt(a,s),l=Kt(a,r);this.allowDirectionChange&&void 0!==this.previousEndBearing&&(this.direction=xi(this.direction,h,this.previousEndBearing,l)),this.previousEndBearing=l;const u=this.arcPoints,c=[o],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;for(let t=0;t<=u;t++){const e=Rt(a,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}getDeltaBearing(t,e,i){let o;return"anticlockwise"===t?(o=i-e,o<0&&(o+=360)):(o=e-i,o<0&&(o+=360)),o}notInSector({normalizedCursor:t,normalizedStart:e,normalizedEnd:i,direction:o}){return"clockwise"===o?e<=i?t>=e&&t<=i:t>=e||t<=i:e>=i?t<=e&&t>=i:t<=e||t>=i}}class _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._minPixelDragDistanceDrawing:"selecting"===o?n<this._minPixelDragDistanceSelecting:n<this._minPixelDragDistance,s)return;this._nextKeyUpIsContextMenu=!1,this._dragState="dragging",this._currentModeCallbacks.onDragStart(e,t=>{this.setDraggability.bind(this)(t)})}else"dragging"===this._dragState&&this._currentModeCallbacks.onDrag(e,t=>{this.setDraggability.bind(this)(t)})},register:t=>{this.getMapEventElement("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.pointerDistance?this.cursors.close:this.cursors.start),r<this.minDistance)return;const c=this.mutateFeature.updateLineString({featureId:this.currentId,coordinateMutations:[{type:at,index:-1,coordinate:[t.lng,t.lat]}],context:{updateType:s.Provisional}});c&&(this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:this.currentId,featureCoordinates:c.geometry.coordinates}),this.closingPoints.update(c.geometry.coordinates))}beginDrawing(t,e="click"){const{id:i,geometry:o}=this.mutateFeature.createLineString({coordinates:[[t.lng,t.lat],[t.lng,t.lat]],properties:{mode:this.mode,[d.CURRENTLY_DRAWING]:!0}});this.closingPoints.create(o.coordinates),this.currentId=i,this.showCoordinatePoints&&this.coordinatePoints.createOrUpdate({featureId:i,featureCoordinates:o.coordinates}),this.drawType=e,this.canClose=!0,"drawing"!==this.state&&this.setDrawing()}onMouseMove(t){this.moveDrawAllowed()&&"click"===this.drawType&&this.addCoordinate(t)}onClick(t){if(this.moveDrawAllowed()&&"drag"!==this.drawType&&("right"===t.button&&this.allowPointerEvent(this.pointerEvents.rightClick,t)||"left"===t.button&&this.allowPointerEvent(this.pointerEvents.leftClick,t)||t.isContextMenu&&this.allowPointerEvent(this.pointerEvents.contextMenu,t))){if(this.preventNewFeature)return;if(!1===this.canClose)return void this.beginDrawing(t);this.close()}}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel?this.cleanUp():t.key===this.keyEvents.finish&&!0===this.canClose&&this.close()}onDragStart(t,e){"drawing"!==this.state&&(this.preventNewFeature||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.addCoordinate(t)}onDragEnd(t,e){this.allowPointerEvent(this.pointerEvents.onDragEnd,t)&&this.dragDrawAllowed()&&"drag"===this.drawType&&(this.preventNewFeature=!0,setTimeout(()=>{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)<o}if("LineString"===u.geometry.type){const t=u.geometry.coordinates;for(let e=0;e<t.length-1;e++){const i=t[e],n=t[e+1];if(ke(p,c(i[0],i[1]),c(n[0],n[1]))<o)return!0}return!1}if(De([t,e],u.geometry.coordinates))return!0;if(null!=i&&i.includePolygonsWithinPointerDistance){const t=u.geometry.coordinates;for(const e of t)for(let t=0;t<e.length-1;t++){const i=e[t],n=e[t+1],s=c(i[0],i[1]),r=c(n[0],n[1]);if(ke(p,s,r)<o)return!0}}return!1}).map(o=>{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<n.length;t++){const e=n[t],i=Ct(c(e[0],e[1]),p);i<a&&(r=t,a=i,s=e)}return o.properties.closestCoordinateIndexToEvent=r,o.properties.closestCoordinatePixelDistanceToEvent=a,o.properties.closestCoordinateDistanceKmToEvent=T(s,[t,e]),o})}getSelectModeOrThrow(t=void 0){const e=this.getSelectMode({switchToSelectMode:!0,selectMode:t});if(!e)throw new Error("No select mode defined in instance");return e}getSelectMode({switchToSelectMode:t,selectMode:e}){this.checkEnabled();const i=this.getMode();if(0===this._instanceSelectModes.length)return null;if(void 0!==e&&!this._instanceSelectModes.includes(e))throw new Error(`No select mode with this name present: ${e}`);let o;return o=void 0!==e?e:this._instanceSelectModes.includes(i)?i:this._instanceSelectModes[0],t&&i!==o&&this.setMode(o),this._modes[o]}isGuidanceFeature(t){return Boolean(t.properties[a.MID_POINT]||t.properties[a.SELECTION_POINT]||t.properties[d.COORDINATE_POINT]||t.properties[d.SNAPPING_POINT])}setModeStyles(t,e){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._modes[t].styles=e}updateModeOptions(t,e){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._modes[t].updateOptions(e)}getSnapshot(){return this._store.copyAll()}getSnapshotFeature(t){if(this._store.has(t))return this._store.copy(t)}clear(){this.checkEnabled(),this._adapter.clear()}get enabled(){return this._enabled}set enabled(t){throw new Error("Enabled is read only")}getMode(){return this._mode.mode}getModeState(){return this._mode.state}setMode(t){if(this.checkEnabled(),!this._modes[t])throw new Error("No mode with this name present");this._mode.stop(),this._mode=this._modes[t],this._mode.start()}removeFeatures(t){this.checkEnabled();const e=[],i=[];let o;t.forEach(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
|