/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as r}from"tslib";import t from"../core/Accessor.js";import e from"../core/Error.js";import o from"../core/Logger.js";import{watch as i}from"../core/reactiveUtils.js";import{property as s,subclass as a}from"../core/accessorSupport/decorators.js";import{UpdatingHandles as n}from"../core/support/UpdatingHandles.js";import l from"../layers/FeatureLayer.js";import{getOwningPortalUrl as d}from"../layers/support/layerUtils.js";import u from"../portal/Portal.js";import{hasUserTypeExtension as c}from"../portal/support/utils.js";let h=class extends t{constructor(r){super(r),this._updatingHandlesLoad=new n,this._updatingHandlesExecute=new n}initialize(){this.addHandles([i(()=>[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 e(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 e(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){o.getLogger(this).error(new e(r,t??""))}async _setUp(){const{utilityNetwork:r,view:t}=this;this._updatingHandlesLoad.addPromise((async()=>{this._loadError=null;const o="trace-analysis-view-model:load-error";if(!t)throw this._loadError="no-view",this._logError(o,this.loadError),new e(o,this.loadError??"");if("2d"!==t.type)throw this._loadError="sceneView-not-supported",this._logError(o,this._loadError),new e(o,this.loadError??"");if("utility"!==r?.type)throw this._loadError="no-utility-network",this._logError(o,this.loadError),new e(o,this.loadError??"");r.loaded||await r.load().catch(r=>{throw this._loadError="no-utility-network",this._logError(o,this.loadError),r}),await this._checkUserTypeExtension(r)})())}async _checkUserTypeExtension(r){const t=new l({url:r.networkSystemLayers.dirtyAreasLayerUrl});await t.load();const o=t.version;if(Number(o)<=11.1){const t=await d(r.layerUrl),o=new u({url:t});await o.load();const i=o.user?.username??"";if(!await c(o,i,"utilityNetwork")){const r="trace-analysis-view-model:load-error";throw this._loadError="no-user-type-extension",this._logError(r,this.loadError),new e(r,this.loadError??"")}}}};r([s()],h.prototype,"_executionError",void 0),r([s()],h.prototype,"_loadError",void 0),r([s()],h.prototype,"executionError",null),r([s()],h.prototype,"loadError",null),r([s({readOnly:!0})],h.prototype,"state",null),r([s()],h.prototype,"utilityNetwork",null),r([s()],h.prototype,"view",null),h=r([a("esri.networks.UtilityNetworkTraceAnalysis")],h);const w=h;export{w as default};
