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