/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as e}from"tslib";import{ignoreAbortErrors as t}from"../core/promiseUtils.js";import{watch as i}from"../core/reactiveUtils.js";import{property as s,subclass as r}from"../core/accessorSupport/decorators.js";import a from"./Widget.js";import n from"./Measurement/MeasurementViewModel.js";import{messageBundle as o,tsx as l}from"./support/widget.js";const d={base:"esri-measurement"};function u(e){return"esri.widgets.AreaMeasurement2D"===e?.declaredClass||"esri.widgets.AreaMeasurement3D"===e?.declaredClass}let c=class extends a{constructor(e,t){super(e,t),this._widgets=new Map,this.activeWidget=null,this.messages=null,this.viewModel=new n}initialize(){this.activeWidget&&this.viewModel.set("activeViewModel",this.activeWidget.viewModel),this.view&&this.activeTool&&this._getActiveWidget().then(e=>{this._set("activeWidget",e)}),this.addHandles([i(()=>[this.view,this.activeTool],async()=>{this._set("activeWidget",await this._getActiveWidget())}),i(()=>this.activeWidget,(e,t)=>{this.viewModel.set("activeViewModel",e?e.viewModel:null),t&&(t.visible=!1)}),i(()=>[this.areaUnit,this.linearUnit,this.uiStrings],()=>this._updateSubWidgetProperties())])}destroy(){this._destroyWidgets()}get activeTool(){return this.viewModel.activeTool}set activeTool(e){this.viewModel.activeTool=e}get areaUnit(){return this.viewModel.areaUnit}set areaUnit(e){this.viewModel.areaUnit=e}get icon(){return"measure"}set icon(e){this._overrideIfSome("icon",e)}get label(){return this.messages?.widgetLabel??""}set label(e){this._overrideIfSome("label",e)}get linearUnit(){return this.viewModel.linearUnit}set linearUnit(e){this.viewModel.linearUnit=e}get view(){return this.viewModel.view}set view(e){this.viewModel.view=e}render(){const{activeWidget:e}=this,t=e&&!e.destroyed?e.render():null;return l("div",{class:d.base},t)}clear(){this.activeTool=null,this._destroyWidgets()}startMeasurement(){const{activeViewModel:e}=this.viewModel;e&&t(e.start())}async _createWidget(e){const{areaUnit:t,linearUnit:i,view:s}=this;if(!s)return null;switch(e){case"area":{const{type:i}=s;switch(i){case"2d":return new(0,(await import("./AreaMeasurement2D.js")).default)({view:s,unit:t,uiStrings:this._createUIStringsForWidget(e)});case"3d":return new(0,(await import("./AreaMeasurement3D.js")).default)({view:s,unit:t,uiStrings:this._createUIStringsForWidget(e)});default:return null}}case"distance":{const{type:t}=s;if("2d"===t){return new(0,(await import("./DistanceMeasurement2D.js")).default)({view:s,unit:i,uiStrings:this._createUIStringsForWidget(e)})}return null}case"direct-line":{const{type:t}=s;if("3d"===t){return new(0,(await import("./DirectLineMeasurement3D.js")).default)({view:s,unit:i,uiStrings:this._createUIStringsForWidget(e)})}return null}default:return null}}_destroyWidgets(){this._widgets.forEach(e=>e.destroy()),this._widgets.clear()}async _getActiveWidget(){const{activeTool:e,view:t}=this;if(!t||!e)return null;let i=null;if(this._widgets.has(e))i=this._widgets.get(e),i.visible=!0;else{if(i=await this._createWidget(e),!i)return null;await i.viewModel.start(),this._widgets.set(e,i)}return i}_createUIStringsForWidget(e){if(!this.uiStrings)return;const t=e+"-"+this.view?.type;return this.uiStrings[t]}_updateSubWidgetProperties(){this._widgets.forEach((e,t)=>{const{areaUnit:i,linearUnit:s}=this;e.unit=u(e)?i:s,e.uiStrings=this._createUIStringsForWidget(t)})}};e([s()],c.prototype,"activeTool",null),e([s({readOnly:!0})],c.prototype,"activeWidget",void 0),e([s()],c.prototype,"areaUnit",null),e([s()],c.prototype,"icon",null),e([s()],c.prototype,"label",null),e([s()],c.prototype,"linearUnit",null),e([s(),o("esri/widgets/Measurement/t9n/Measurement")],c.prototype,"messages",void 0),e([s()],c.prototype,"uiStrings",void 0),e([s()],c.prototype,"view",null),e([s({type:n})],c.prototype,"viewModel",void 0),c=e([r("esri.widgets.Measurement")],c);const g=c;export{g as default};
