/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../../../core/Accessor.js";import{multiplyOpacity as i}from"../../../../core/colorUtils.js";import{makeHandle as s}from"../../../../core/handleUtils.js";import{ignoreAbortErrors as a,isAborted as r}from"../../../../core/promiseUtils.js";import{watch as o,initial as n}from"../../../../core/reactiveUtils.js";import{property as l,subclass as h}from"../../../../core/accessorSupport/decorators.js";import{UpdatingHandles as c}from"../../../../core/support/UpdatingHandles.js";import p from"../../../../symbols/SimpleFillSymbol.js";import m from"../../../../symbols/SimpleLineSymbol.js";import{ExclusiveOperationManager as d}from"../../../analysis/ExclusiveOperationManager.js";import u from"../../../../widgets/Sketch/SketchViewModel.js";function y(e){return"polygon"===e?.type}let w=class extends t{constructor(e){super(e),this._invisibleAreaSymbol=new p({color:null,outline:null}),this._operationManager=new d,this._updatingHandles=new c}initialize(){this._sketchViewModel=new u({defaultCreateOptions:{mode:"click"},defaultUpdateOptions:{enableMoveAllGraphics:!1,enableRotation:!1,enableScaling:!1,enableZ:!1,highlightOptions:{enabled:!1},multipleSelectionEnabled:!1,reshapeOptions:{edgeOperation:"none",shapeOperation:"none"},toggleToolOnClick:!1,tool:"reshape"},updateOnGraphicClick:!1,cancelUpdateOnClickAway:!1,view:this.view}),this.addHandles([o(()=>({interactive:this.measurementViewData.interactive,visible:this.measurementViewData.visible}),({interactive:e,visible:t})=>{t&&e&&this._analysis.geometry?"place"!==this._operationManager.currentOperationType&&a(this.reshape()):this._operationManager.stop()}),o(()=>this.measurementViewData.analysis.geometry,e=>{e||(this._operationManager.stop(),this._sketchViewModel?.cancel(),this.measurementViewData.sketchGraphicsLayer?.removeAll())}),o(()=>this.measurementViewData.snappingOptions,e=>{this._sketchViewModel&&(this._sketchViewModel.snappingOptions=e)},n),o(()=>this.view.effectiveTheme.accentColor,e=>{this._areaSymbol=new p({color:i(e,.3),outline:new m({color:e,width:2})})},n)])}destroy(){this._areaSymbol?.destroy(),this._invisibleAreaSymbol.destroy(),this._operationManager.destroy(),this._sketchViewModel?.destroy(),this._updatingHandles.destroy()}get state(){if(!this.measurementViewData.visible||!this._operationManager.currentOperationType)return"idle";switch(this._operationManager.currentOperationType){case"place":return"placing";case"reshape":return"reshaping"}}get updating(){return!this._sketchViewModel||this._sketchViewModel.updating||this._updatingHandles.updating}async place(e){return this._operationManager.start("place",async t=>{if(!this._sketchViewModel||!this.measurementViewData.sketchGraphicsLayer)return;const i=this._analysis.clone(),o=e=>{const i="state"in e?e.state:"active",s="graphic"in e?e.graphic:e.graphics?.at(0);switch(i){case"start":case"active":{const e=s?.geometry;"polygon"===e?.type&&(this._analysis.geometry=e);break}case"cancel":case"complete":t.stop(),this.reshape()}},n=this._sketchViewModel.on(["create","undo","redo"],o);t.handles.push(n,s(()=>{n.remove(),this._sketchViewModel?.cancel(),this.measurementViewData.sketchGraphicsLayer?.removeAll();const s=this._analysis;return s.valid?r(e)||i.equals(s)?t.reject():void t.resolve({}):(s.clear(),t.reject())})),this._sketchViewModel.polygonSymbol=this._invisibleAreaSymbol,this._sketchViewModel.layer=this.measurementViewData.sketchGraphicsLayer,this._updatingHandles.consumePromise(a(this._sketchViewModel.create("polygon")))},e)}async reshape(e){return this._operationManager.start("reshape",async t=>{if(!this._sketchViewModel)return;const i=()=>this._areaSymbol&&this._sketchViewModel&&this.measurementViewData.manipulatorGraphic&&this.measurementViewData.manipulatorGraphicsLayer?(this._sketchViewModel.polygonSymbol=this._areaSymbol,this._sketchViewModel.layer=this.measurementViewData.manipulatorGraphicsLayer,this._updatingHandles.addPromise(a(this._sketchViewModel.update(this.measurementViewData.manipulatorGraphic,{tool:"reshape"})))):Promise.resolve(),o=this._sketchViewModel.on("update",e=>{const s=e.graphics[0]?.geometry;this._analysis.geometry=y(s)?s:null,"complete"===e.state&&(this.measurementViewData.interactive?i():t.stop())});t.handles.push(o,s(()=>{if(o.remove(),this._sketchViewModel?.cancel(),r(e))return t.reject();t.resolve()})),await i()},e)}get _analysis(){return this.measurementViewData.analysis}};e([l()],w.prototype,"_sketchViewModel",void 0),e([l()],w.prototype,"measurementViewData",void 0),e([l()],w.prototype,"state",null),e([l()],w.prototype,"updating",null),e([l()],w.prototype,"view",void 0),w=e([h("esri.views.2d.analysis.AreaMeasurement.AreaMeasurementTool")],w);export{w as AreaMeasurementTool};
