/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../chunks/_rollupPluginBabelHelpers","../../kernel","../../request","../../core/maybe","../../core/promiseUtils","../FeatureLayer","../../portal/Portal","../../portal/PortalItem"],(function(t,r,e,n,o,i,a,s,l){"use strict";let u=function(){function t(t,r,e,n){this.parsedUrl=t,this.portalItem=r,this.apiKey=e,this.signal=n,this.rootDocument=null;const o=this.parsedUrl.path.match(/^(.*)\/SceneServer\/layers\/([\d]*)\/?$/i);o&&(this.urlParts={root:o[1],layerId:parseInt(o[2],10)})}var u=t.prototype;return u.fetch=function(){var t=r._asyncToGenerator((function*(){var t;if(!this.urlParts)return null;const r=null!=(t=this.portalItem)?t:yield this._portalItemFromServiceItemId();if(o.isNone(r))return this._loadFromUrl();const e=yield this._findAndLoadRelatedPortalItem(r);return o.isNone(e)?null:this._loadFeatureLayerFromPortalItem(e)}));function e(){return t.apply(this,arguments)}return e}(),u.fetchPortalItem=function(){var t=r._asyncToGenerator((function*(){var t;if(!this.urlParts)return null;const r=null!=(t=this.portalItem)?t:yield this._portalItemFromServiceItemId();return o.isNone(r)?null:this._findAndLoadRelatedPortalItem(r)}));function e(){return t.apply(this,arguments)}return e}(),u._fetchRootDocument=function(){var t=r._asyncToGenerator((function*(){if(o.isSome(this.rootDocument))return this.rootDocument;if(o.isNone(this.urlParts))return this.rootDocument={},{};const t={query:{f:"json",token:this.apiKey},responseType:"json",signal:this.signal},r=`${this.urlParts.root}/SceneServer`;try{const e=yield n(r,t);this.rootDocument=e.data}catch{this.rootDocument={}}return this.rootDocument}));function e(){return t.apply(this,arguments)}return e}(),u._fetchServiceOwningPortalUrl=function(){var t=r._asyncToGenerator((function*(){var t;const r=null==(t=e.id)?void 0:t.findServerInfo(this.parsedUrl.path);if(null!=r&&r.owningSystemUrl)return r.owningSystemUrl;const o=this.parsedUrl.path.replace(/(.*\/rest)\/.*/i,"$1")+"/info";try{const t=(yield n(o,{query:{f:"json"},responseType:"json",signal:this.signal})).data.owningSystemUrl;if(t)return t}catch(a){i.throwIfAbortError(a)}return null}));function o(){return t.apply(this,arguments)}return o}(),u._findAndLoadRelatedPortalItem=function(){var t=r._asyncToGenerator((function*(t){try{return(yield t.fetchRelatedItems({relationshipType:"Service2Service",direction:"reverse"},{signal:this.signal})).find((t=>"Feature Service"===t.type))||null}catch(r){return i.throwIfAbortError(r),null}}));function e(r){return t.apply(this,arguments)}return e}(),u._loadFeatureLayerFromPortalItem=function(){var t=r._asyncToGenerator((function*(t){yield t.load({signal:this.signal});const r=yield this._findMatchingAssociatedSublayerUrl(t.url);return new a({url:r,portalItem:t}).load({signal:this.signal})}));function e(r){return t.apply(this,arguments)}return e}(),u._loadFromUrl=function(){var t=r._asyncToGenerator((function*(){const t=yield this._findMatchingAssociatedSublayerUrl(`${this.urlParts.root}/FeatureServer`);return new a({url:t}).load({signal:this.signal})}));function e(){return t.apply(this,arguments)}return e}(),u._findMatchingAssociatedSublayerUrl=function(){var t=r._asyncToGenerator((function*(t){const r=t.replace(/^(.*FeatureServer)(\/[\d]*\/?)?$/i,"$1"),e={query:{f:"json"},responseType:"json",authMode:"no-prompt",signal:this.signal},o=this.urlParts.layerId,i=this._fetchRootDocument(),a=n(r,e),[s,l]=yield Promise.all([a,i]),u=l&&l.layers,c=s.data&&s.data.layers;if(!Array.isArray(c))throw new Error("expected layers array");if(Array.isArray(u))for(let n=0;n<Math.min(u.length,c.length);n++){if(u[n].id===o)return`${r}/${c[n].id}`}else if(o<c.length)return`${r}/${c[o].id}`;throw new Error("could not find matching associated sublayer")}));function e(r){return t.apply(this,arguments)}return e}(),u._portalItemFromServiceItemId=function(){var t=r._asyncToGenerator((function*(){const t=(yield this._fetchRootDocument()).serviceItemId;if(!t)return null;const r=new l({id:t,apiKey:this.apiKey}),e=yield this._fetchServiceOwningPortalUrl();o.isSome(e)&&(r.portal=new s({url:e}));try{return r.load({signal:this.signal})}catch(n){return i.throwIfAbortError(n),null}}));function e(){return t.apply(this,arguments)}return e}(),t}();t.FetchAssociatedFeatureLayer=u,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
