/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/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 i}from"../../../../core/quantity.js";import{watch as n,syncAndInitial as o}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{useGeodesicAreaMeasurement as h}from"../../../support/automaticAreaMeasurementUtils.js";import{MeasurementWorkerHandle as y}from"../../../support/MeasurementWorkerHandle.js";const _=i(1e5,"meters");async function j(e,t,r){if(!e?.rings.length||!e.rings[0].length)return null;await d();const s=f(e,t);return s?h(t)?w(s,r):v(s,r):null}async function w(e,t){await p();const r=g(e);if(!r)return null;const s=k(r),n=c(r,_.value,{unit:_.unit});if(!n)return null;const o=c(s,_.value,{unit:_.unit});if(!o)return null;const{area:a,length:u}=await t.geodeticArea(e,{unit:"square-meters",returnLength:!0});return{area:a,display:{fill:n,outline:o},mode:"geodesic",perimeter:u??i(0,"meters")}}async function v(e,t){const r=g(e);if(!r)return null;const s=k(r),{area:n,length:o}=await t.area2D(e,{unit:"square-meters",returnLength:!0});return{area:n,display:{fill:r,outline:s},mode:"euclidean",perimeter:o??i(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._debouncedGetResult=r(async e=>{this._result=await j(e,this.view.spatialReference,this._measurementWorker)}),this._measurementWorker=new y,this._measurementWorker.preloadGeodetic()}initialize(){this.addHandles(n(()=>this.measurementViewData.analysis.geometry,e=>{this._updatingHandles.addPromise(s(this._debouncedGetResult(e)))},o))}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};
