/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../../../core/Accessor.js";import{debounce as r,ignoreAbortErrors as s}from"../../../../core/promiseUtils.js";import{createLength as n}from"../../../../core/quantity.js";import{watch as o,syncAndInitial as i}from"../../../../core/reactiveUtils.js";import{property as a,subclass as u}from"../../../../core/accessorSupport/decorators.js";import{UpdatingHandles as l}from"../../../../core/support/UpdatingHandles.js";import m from"../../../../geometry/Polyline.js";import{l as p,e as c}from"../../../../chunks/geodeticDensifyOperator.js";import{load as d,execute as f}from"../../../../geometry/operators/projectOperator.js";import{e as g}from"../../../../chunks/simplifyOperator.js";import{GeometryWorkerHandle as h}from"../../../support/GeometryWorkerHandle.js";import{autoMeasurementMode as y}from"../../../support/measurementWorkerUtils.js";const j=n(1e5,"meters");async function w(e,t,r){if(!e?.rings.length||!e.rings[0].length)return null;await d();const s=f(e,t);return s?0===y(t)?_(s,r):v(s,r):null}async function _(e,t){await p();const r=g(e);if(!r)return null;const s=k(r),o=c(r,j.value,{unit:j.unit});if(!o)return null;const i=c(s,j.value,{unit:j.unit});if(!i)return null;const{area:a,length:u}=await t.geodeticArea(e,{unit:"square-meters",returnLength:!0});return{area:a,display:{fill:o,outline:i},mode:"geodesic",perimeter:u??n(0,"meters")}}async function v(e,t){const r=g(e);if(!r)return null;const s=k(r),{area:o,length:i}=await t.area2D(e,{unit:"square-meters",returnLength:!0});return{area:o,display:{fill:r,outline:s},mode:"euclidean",perimeter:i??n(0,"meters")}}function k(e){return new m({spatialReference:e.spatialReference,paths:e.rings})}let H=class extends t{constructor(e){super(e),this._result=null,this._updatingHandles=new l,this._measurementWorker=new h({preloadGeodetic:!0}),this._debouncedGetResult=r(async e=>{this._result=await w(e,this.view.spatialReference,this._measurementWorker)})}initialize(){this.addHandles(o(()=>this.measurementViewData.analysis.geometry,e=>{this._updatingHandles.consumePromise(s(this._debouncedGetResult(e)))},i))}destroy(){this._updatingHandles.destroy(),this._measurementWorker.destroy()}get result(){return this._result}get updating(){return this._updatingHandles.updating}};e([a()],H.prototype,"_result",void 0),e([a()],H.prototype,"measurementViewData",void 0),e([a()],H.prototype,"result",null),e([a()],H.prototype,"updating",null),e([a()],H.prototype,"view",void 0),H=e([u("esri.views.2d.analysis.AreaMeasurement.AreaMeasurementController")],H);export{H as AreaMeasurementController};
