/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{isDevEnvironment as t,adjustStaticAGOUrl as e}from"../../core/devEnvironmentUtils.js";import r from"../../core/Error.js";import{urlToObject as o,removeFile as l}from"../../core/urlUtils.js";import n from"../../portal/Portal.js";import{f as s}from"../../chunks/persistableUrlUtils.js";import{fromJSON as m}from"./jsonUtils.js";import a from"./StyleOrigin.js";import{fetchStyle as i,Style2DUrlTemplate as u,requestJSON as c,makeCIMSymbolRef as p,symbolUrlFromStyleItem as f}from"./styleUtils.js";import{Thumbnail as y}from"./Thumbnail.js";import{isSymbol3D as b}from"./typeUtils.js";import{defaultAcceptedFormats as d}from"./webStyleAcceptedFormats.js";function h(t,e,o){const l=t.name;return null==l?Promise.reject(new r("symbolstyleutils:style-symbol-reference-name-missing","Missing name in style symbol reference")):"Esri2DPointSymbolsStyle"===t.styleName?U(l,e,o):i(t,e,o).then(t=>j(t,l,e,f,o))}function g(t,e){return e.items.find(e=>e.name===t)}async function j(i,u,f,h,j){const U=null!=f?.portal?f.portal:n.getDefault(),w={portal:U,url:o(i.baseUrl)},N=g(u,i.data);if(!N)throw new r("symbolstyleutils:symbol-name-not-found",`The symbol name '${u}' could not be found`,{symbolName:u});const S=j?.acceptedFormats??d,D=h(N,S);if(!D)throw new r("symbolstyleutils:symbol-reference-no-accepted-format",`The symbol name '${u}' does not have an accepted format (one of ${S})`,{symbolName:u});const{url:$,format:v}=D;let E=s($,w),O=N.thumbnail?.href??null;const P=N.thumbnail?.imageData;t()&&(E=e(E)??"",O=e(O));const T={portal:U,url:o(l(E)),origin:"portal-item"};return c(E,j).then(t=>{const e="cim"===v?p(t.data):t.data,r=m(e,T);if(r&&b(r)){if(O){const t=s(O,w);r.thumbnail=new y({url:t})}else P&&(r.thumbnail=new y({url:`data:image/png;base64,${P}`}));i.styleUrl?r.styleOrigin=new a({portal:f.portal,styleUrl:i.styleUrl,name:u}):i.styleName&&(r.styleOrigin=new a({portal:f.portal,styleName:i.styleName,name:u}))}return r})}function U(t,e,r){const s=u.replaceAll(/\{SymbolName\}/gi,t),a=null!=e.portal?e.portal:n.getDefault();return c(s,r).then(t=>{const e=p(t.data);return m(e,{portal:a,url:o(l(s)),origin:"portal-item"})})}export{j as fetchSymbolFromStyle,g as getStyleItemFromStyle,h as resolveWebStyleSymbol};
