/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as t}from"tslib";import r 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 u}from"../layers/support/layerUtils.js";import d from"../portal/Portal.js";import{hasUserTypeExtension as c}from"../portal/support/utils.js";let h=class extends r{constructor(t){super(t),this._updatingHandlesLoad=new n,this._updatingHandlesExecute=new n}initialize(){this.addHandles([i(()=>[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 e(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 e(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 e(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 o=e.utilityNetworks;o?.length?(t&&o.includes(t)||(this.utilityNetwork=o.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 l({url:t.networkSystemLayers.dirtyAreasLayerUrl});await r.load();const e=r.version;if(Number(e)<=11.1){const r=await u(t.layerUrl),e=new d({url:r});await e.load();const o=e.user?.username??"";if(!await c(e,o,"utilityNetwork")){const t="trace-analysis-view-model:load-error";this._loadError="no-user-type-extension",this._logError(t,this.loadError)}}}};t([s()],h.prototype,"_executionError",void 0),t([s()],h.prototype,"_loadError",void 0),t([s()],h.prototype,"executionError",null),t([s()],h.prototype,"loadError",null),t([s({readOnly:!0})],h.prototype,"state",null),t([s()],h.prototype,"utilityNetwork",null),t([s()],h.prototype,"view",null),h=t([a("esri.networks.UtilityNetworkTraceAnalysis")],h);const y=h;export{y as default};
