/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../request.js";import r from"../../core/Error.js";import has from"../../core/has.js";import{throwIfAbortError as t}from"../../core/promiseUtils.js";import{removeFile as o,normalize as s}from"../../core/urlUtils.js";import n from"../../portal/Portal.js";import l from"../../portal/PortalQueryParams.js";import{cachedStyles as a}from"./styleCache.js";async function f(e,r){try{return{data:(await p(e,r)).data,baseUrl:o(e),styleUrl:e}}catch(s){return t(s),null}}function u(e,r,t){const o=null!=r.portal?r.portal:n.getDefault();let s;const l=`${o.url} - ${o.user?.username} - ${e}`,f=a.get(l);if(f)return f;const u=c(e,o,t).then(e=>(s=e,e.fetchData())).then(r=>({data:r,baseUrl:s.itemUrl??"",styleName:e}));return a.set(l,u),u}function c(e,t,o){return t.load(o).then(()=>{const r=new l({disableExtraQuery:!0,query:`owner:${d} AND type:${w} AND typekeywords:"${e}"`});return t.queryItems(r,o)}).then(({results:t})=>{let s=null;const n=e.toLowerCase();if(t&&Array.isArray(t))for(const e of t){const r=e.typeKeywords?.some(e=>e.toLowerCase()===n);if(r&&e.type===w&&e.owner===d){s=e;break}}if(!s)throw new r("symbolstyleutils:style-not-found",`The style '${e}' could not be found`,{styleName:e});return s.load(o)})}function i(e,t,o){return null!=e?.styleUrl?f(e.styleUrl,o):null!=e?.styleName?u(e.styleName,t,o):Promise.reject(new r("symbolstyleutils:style-url-and-name-missing","Either styleUrl or styleName is required to resolve a style"))}function m(e){return null===e||"CIMSymbolReference"===e.type?e:{type:"CIMSymbolReference",symbol:e}}function y(e,r){for(const t of r)switch(t){case"cim":if(e.cimRef)return{format:t,url:encodeURI(e.cimRef)};break;case"web-gltf-basisu":{const r=b(e,"gltf_basisu");if(r)return{format:t,url:r};break}case"web-gltf":{const r=b(e,"gltf");if(r)return{format:t,url:r};break}case"web":{const r=b(e,"gltf");if(r)return{format:"web-gltf",url:r};if(e.webRef)return{format:t,url:encodeURI(e.webRef)};break}}}function b(e,r){if(!has("enable-feature:force-wosr"))return e.formatInfos?.find(e=>e.type===r)?.href}function p(r,t){const o={responseType:"json",query:{f:"json"},...t};return e(s(r),o)}const d="esri_en",w="Style",h="https://cdn.arcgis.com/sharing/rest/content/items/220936cc6ed342c9937abd8f180e7d1e/resources/styles/cim/{SymbolName}.json?f=json";export{h as Style2DUrlTemplate,i as fetchStyle,m as makeCIMSymbolRef,p as requestJSON,y as symbolUrlFromStyleItem};
