/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../chunks/_rollupPluginBabelHelpers","../chunks/tslib.es6","../request","../core/Loadable","../core/Logger","../core/maybe","../core/MultiOriginJSONSupport","../core/promiseUtils","../core/urlUtils","../core/accessorSupport/decorators/property","../core/arrayUtils","../core/has","../core/accessorSupport/ensureType","../core/accessorSupport/decorators/subclass","../geometry/Extent","../geometry/SpatialReference","../layers/support/arcgisLayerUrl"],(function(e,r,t,o,n,a,i,l,s,u,c,p,d,y,f,h,_){"use strict";const v=n.getLogger("esri.networks.Network");let g=function(r){function o(e){var t;return(t=r.call(this,e)||this).id=null,t.title=null,t.layerUrl=null,t.dataElement=null,t.fullExtent=null,t.spatialReference=null,t.type=null,t.sourceJSON=null,t}e._inheritsLoose(o,r);var n=o.prototype;return n.initialize=function(){this.when().catch((e=>{var r,t;l.isAbortError(e)||v.error("#load()",`Failed to load layer (title: '${null!=(r=this.title)?r:"no title"}', id: '${null!=(t=this.id)?t:"no id"}')`,{error:e})}))},n.load=function(){var r=e._asyncToGenerator((function*(e){return this.addResolvingPromise(this._fetchDataElement(this.featureServiceUrl,this.layerId.toString(),e)),this.addResolvingPromise(this._fetchLayerMetaData(this.layerUrl,e)),this}));function t(e){return r.apply(this,arguments)}return t}(),n.getLayerIdBySourceId=function(e){if(this.dataElement){const r=this.dataElement.domainNetworks;for(const t of r){for(const r of t.edgeSources?t.edgeSources:[])if(r.sourceId===e)return r.layerId;for(const r of t.junctionSources?t.junctionSources:[])if(r.sourceId===e)return r.layerId}return null}return null},n._fetchLayerMetaData=function(){var r=e._asyncToGenerator((function*(e,r){const o=yield t(e,{responseType:"json",query:{f:"json"},...r});this.sourceJSON=o.data,this.read(o.data,{origin:"service"})}));function o(e,t){return r.apply(this,arguments)}return o}(),n._fetchDataElement=function(){var r=e._asyncToGenerator((function*(e,r,o){if(this.dataElement)return;const n=yield t(`${e}/queryDataElements`,{responseType:"json",query:{layers:JSON.stringify([r]),f:"json"},...o}).then((e=>{var r;return null==(r=e.data.layerDataElements)?void 0:r[0]}));n&&this.read(n,{origin:"service"})}));function o(e,t,o){return r.apply(this,arguments)}return o}(),e._createClass(o,[{key:"datasetName",get:function(){var e;return null==(e=this.dataElement)?void 0:e.name}},{key:"owner",get:function(){var e;return null==(e=this.dataElement)?void 0:e.userIdentity}},{key:"schemaGeneration",get:function(){var e;return null==(e=this.dataElement)?void 0:e.schemaGeneration}},{key:"parsedUrl",get:function(){const e=this.layerUrl;return e?s.urlToObject(e):null}},{key:"featureServiceUrl",get:function(){const e=this.parsedUrl&&_.parse(this.parsedUrl.path);return a.isSome(e)?e.url.path:null}},{key:"networkServiceUrl",get:function(){return this.featureServiceUrl?this.featureServiceUrl.replace(/\/FeatureServer/i,"/UtilityNetworkServer"):null}},{key:"layerId",get:function(){const e=this.parsedUrl&&_.parse(this.parsedUrl.path);return a.isSome(e)?e.sublayer:null}}]),o}(i.MultiOriginJSONMixin(o));r.__decorate([u.property({type:String,nonNullable:!0,json:{origins:{"web-map":{read:!0,write:{isRequired:!0}}},read:!1}})],g.prototype,"id",void 0),r.__decorate([u.property({type:String,nonNullable:!0,json:{origins:{"web-map":{read:!0,write:{isRequired:!0}}},read:!1}})],g.prototype,"title",void 0),r.__decorate([u.property({type:String,nonNullable:!0,json:{origins:{"web-map":{read:{source:"url"},write:{target:"url",isRequired:!0}}},read:!1}})],g.prototype,"layerUrl",void 0),r.__decorate([u.property({type:Object,json:{origins:{service:{read:!0}},read:!1}})],g.prototype,"dataElement",void 0),r.__decorate([u.property({type:f,json:{origins:{service:{read:{source:"extent"}}},read:!1}})],g.prototype,"fullExtent",void 0),r.__decorate([u.property({type:h,json:{origins:{service:{read:{source:"extent.spatialReference"}}},read:!1}})],g.prototype,"spatialReference",void 0),r.__decorate([u.property({type:["utility","trace"],readOnly:!0,json:{read:!1,write:!1}})],g.prototype,"type",void 0),r.__decorate([u.property({readOnly:!0})],g.prototype,"datasetName",null),r.__decorate([u.property({readOnly:!0})],g.prototype,"owner",null),r.__decorate([u.property({readOnly:!0})],g.prototype,"schemaGeneration",null),r.__decorate([u.property({readOnly:!0})],g.prototype,"parsedUrl",null),r.__decorate([u.property({readOnly:!0})],g.prototype,"featureServiceUrl",null),r.__decorate([u.property({readOnly:!0})],g.prototype,"networkServiceUrl",null),r.__decorate([u.property({readOnly:!0})],g.prototype,"layerId",null),r.__decorate([u.property()],g.prototype,"sourceJSON",void 0),g=r.__decorate([y.subclass("esri.networks.Network")],g);return g}));
