/*
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 r}from"tslib";import t from"../../core/Accessor.js";import{deprecatedModule as e}from"../../core/deprecate.js";import o from"../../core/Error.js";import i from"../../core/Logger.js";import{watch as s}from"../../core/reactiveUtils.js";import{property as a,subclass as l}from"../../core/accessorSupport/decorators.js";import{UpdatingHandles as n}from"../../core/support/UpdatingHandles.js";import d from"../../layers/FeatureLayer.js";import{getOwningPortalUrl as c}from"../../layers/support/layerUtils.js";import u from"../../portal/Portal.js";import{hasUserTypeExtension as h}from"../../portal/support/utils.js";e(i.getLogger("esri.widgets.UtilityNetworkTraceAnalysis.UtilityNetworkTraceAnalysisViewModel"),"UtilityNetworkTraceAnalysisViewModel",{replacement:"Use esri/networks/UtilityNetworkTraceAnalysis instead",version:"5.0",warnOnce:!0});let w=class extends t{constructor(r){super(r),this._updatingHandlesLoad=new n,this._updatingHandlesExecute=new n}initialize(){this.addHandles([s(()=>[this.utilityNetwork,this.view],()=>{this._setUp()},{initial:!0})])}destroy(){this._updatingHandlesLoad.destroy(),this._updatingHandlesExecute.destroy()}get executionError(){return this._executionError}get loadError(){return this._loadError}get state(){return this._updatingHandlesLoad.updating?"loading":this.loadError?"disabled":this._updatingHandlesExecute.updating?"executing":"ready"}get utilityNetwork(){return this._get("utilityNetwork")}set utilityNetwork(r){this._get("utilityNetwork")!==r&&this._set("utilityNetwork",r)}get view(){return this._get("view")}set view(r){this._get("view")!==r&&this._set("view",r)}async executeNamedTraceConfiguration(r){return this._updatingHandlesExecute.addPromise(this._executeNamedTraceConfiguration(r))}async executeTraceConfiguration(r){return this._updatingHandlesExecute.addPromise(this._executeTraceConfiguration(r))}async _executeNamedTraceConfiguration(r){if("disabled"===this.state){const r="trace-analysis-view-model:load-error";throw this._logError(r,this.loadError),new o(r,this.loadError??"")}this._executionError=null;return await this.utilityNetwork.trace(r).catch(r=>{throw this._executionError="trace-error",this._logError("trace-analysis-view-model:execution-error",this.executionError),r})}async _executeTraceConfiguration(r){if("disabled"===this.state){const r="trace-analysis-view-model:load-error";throw this._logError(r,this.loadError),new o(r,this.loadError??"")}this._executionError=null;return await this.utilityNetwork.trace(r).catch(r=>{throw this._executionError="trace-error",this._logError("trace-analysis-view-model:execution-error",this.executionError),r})}_logError(r,t){i.getLogger(this).error(new o(r,t??""))}async _setUp(){const{utilityNetwork:r,view:t}=this;this._updatingHandlesLoad.addPromise((async()=>{this._loadError=null;const e="trace-analysis-view-model:load-error";if(!t)throw this._loadError="no-view",this._logError(e,this.loadError),new o(e,this.loadError??"");if("2d"!==t.type)throw this._loadError="sceneView-not-supported",this._logError(e,this._loadError),new o(e,this.loadError??"");if("utility"!==r?.type)throw this._loadError="no-utility-network",this._logError(e,this.loadError),new o(e,this.loadError??"");r.loaded||await r.load().catch(r=>{throw this._loadError="no-utility-network",this._logError(e,this.loadError),r}),await this._checkUserTypeExtension(r)})())}async _checkUserTypeExtension(r){const t=new d({url:r.networkSystemLayers.dirtyAreasLayerUrl});await t.load();const e=t.version;if(Number(e)<=11.1){const t=await c(r.layerUrl),e=new u({url:t});await e.load();const i=e.user?.username??"";if(!await h(e,i,"utilityNetwork")){const r="trace-analysis-view-model:load-error";throw this._loadError="no-user-type-extension",this._logError(r,this.loadError),new o(r,this.loadError??"")}}}};r([a()],w.prototype,"_executionError",void 0),r([a()],w.prototype,"_loadError",void 0),r([a()],w.prototype,"executionError",null),r([a()],w.prototype,"loadError",null),r([a({readOnly:!0})],w.prototype,"state",null),r([a()],w.prototype,"utilityNetwork",null),r([a()],w.prototype,"view",null),w=r([l("esri.widgets.UtilityNetworkTraceAnalysis.UtilityNetworkTraceAnalysisViewModel")],w);const y=w;export{y as default};
