/*
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","../../config","../../request","../../core/promiseUtils","../../core/urlUtils","../../views/2d/engine/vectorTiles/style/VectorTileSource"],(function(e,r,t,l,o,s,n){"use strict";const i=t.defaults&&t.defaults.io.corsEnabledServers;function u(e,r){return a.apply(this,arguments)}function a(){return(a=r._asyncToGenerator((function*(e,r){const t={source:null,sourceBase:null,sourceUrl:null,validatedSource:null,style:null,styleBase:null,styleUrl:null,sourceNameToSource:{},primarySourceName:"",spriteFormat:"png"},[l,o]="string"==typeof e?[e,null]:[null,e.jsonUrl];yield f(t,"esri",e,o,r);const s={layerDefinition:t.validatedSource,url:l,serviceUrl:t.sourceUrl,style:t.style,styleUrl:t.styleUrl,spriteUrl:t.style.sprite&&p(t.styleBase,t.style.sprite),spriteFormat:t.spriteFormat,glyphsUrl:t.style.glyphs&&p(t.styleBase,t.style.glyphs),sourceNameToSource:t.sourceNameToSource,primarySourceName:t.primarySourceName};return c(s.spriteUrl),c(s.glyphsUrl),s}))).apply(this,arguments)}function c(e){if(!e)return;const r=s.getOrigin(e);i&&!i.includes(r)&&i.push(r)}function p(...e){let r;for(let t=0;t<e.length;++t)if(s.isProtocolRelative(e[t])){if(r){const l=r.split("://")[0];r=l+":"+e[t].trim()}}else r=s.isAbsolute(e[t])?e[t]:s.join(r,e[t]);return s.removeTrailingSlash(r)}function f(e,r,t,l,o){return y.apply(this,arguments)}function y(){return(y=r._asyncToGenerator((function*(e,r,t,n,i){let u,a,p;if(o.throwIfAborted(i),"string"==typeof t){const e=s.normalize(t);c(e),p=yield l(e,{...i,responseType:"json",query:{f:"json",...null==i?void 0:i.query}}),p.ssl&&(u&&(u=u.replace(/^http:/i,"https:")),a&&(a=a.replace(/^http:/i,"https:"))),u=e,a=e}else p={data:t},u=t.jsonUrl||null,a=n;const f=p.data;if(m(f))return e.styleUrl=u||null,h(e,f,a,i);if(d(f))return e.sourceUrl?U(e,f,a,!1,r,i):(e.sourceUrl=u||null,U(e,f,a,!0,r,i));throw new Error("You must specify the URL or the JSON for a service or for a style.")}))).apply(this,arguments)}function m(e){return!!e.sources}function d(e){return!m(e)}function h(e,r,t,l){return S.apply(this,arguments)}function S(){return(S=r._asyncToGenerator((function*(e,r,t,l){const o=t?s.removeFile(t):s.getAppBaseUrl();e.styleBase=o,e.style=r,e.styleUrl&&c(e.styleUrl),r["sprite-format"]&&"webp"===r["sprite-format"].toLowerCase()&&(e.spriteFormat="webp");const n=[];if(r.sources&&r.sources.esri){const t=r.sources.esri;t.url?yield f(e,"esri",p(o,t.url),void 0,l):n.push(f(e,"esri",t,o,l))}for(const s of Object.keys(r.sources))"esri"!==s&&"vector"===r.sources[s].type&&(r.sources[s].url?n.push(f(e,s,p(o,r.sources[s].url),void 0,l)):r.sources[s].tiles&&n.push(f(e,s,r.sources[s],o,l)));yield Promise.all(n)}))).apply(this,arguments)}function U(e,r,t,l,o,s){return g.apply(this,arguments)}function g(){return(g=r._asyncToGenerator((function*(e,r,t,l,o,i){const u=t?s.removeTrailingSlash(t)+"/":s.getAppBaseUrl(),a=v(r,u),y=new n(o,s.addQueryParameters(u,null==i?void 0:i.query),a);if(!l&&e.primarySourceName in e.sourceNameToSource){const r=e.sourceNameToSource[e.primarySourceName];if(!r.isCompatibleWith(y))return;null!=y.fullExtent&&(null!=r.fullExtent?r.fullExtent.union(y.fullExtent):r.fullExtent=y.fullExtent.clone()),r.tileInfo.lods.length<y.tileInfo.lods.length&&(r.tileInfo=y.tileInfo)}if(l?(e.sourceBase=u,e.source=r,e.validatedSource=a,e.primarySourceName=o,e.sourceUrl&&c(e.sourceUrl)):c(u),e.sourceNameToSource[o]=y,!e.style){if(null==r.defaultStyles)throw new Error;return"string"==typeof r.defaultStyles?f(e,"",p(u,r.defaultStyles,"root.json"),void 0,i):f(e,"",r.defaultStyles,p(u,"root.json"),i)}}))).apply(this,arguments)}function v(e,r){if(e.hasOwnProperty("tileInfo"))return e;const t={xmin:-20037507.067161843,ymin:-20037507.067161843,xmax:20037507.067161843,ymax:20037507.067161843,spatialReference:{wkid:102100}},l=512;let o=78271.51696400007,s=295828763.7957775;const n=[],i=e.hasOwnProperty("minzoom")?+e.minzoom:0,u=e.hasOwnProperty("maxzoom")?+e.maxzoom:22;for(let a=0;a<=u;a++)a>=i&&n.push({level:a,scale:s,resolution:o}),o/=2,s/=2;for(const a of e.tiles)c(p(r,a));return{capabilities:"TilesOnly",initialExtent:t,fullExtent:t,minScale:0,maxScale:0,tiles:e.tiles,tileInfo:{rows:l,cols:l,dpi:96,format:"pbf",origin:{x:-20037508.342787,y:20037508.342787},lods:n,spatialReference:{wkid:102100}}}}e.loadMetadata=u,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
