var P=Object.defineProperty;var R=(n,e,t)=>e in n?P(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var h=(n,e,t)=>R(n,typeof e!="symbol"?e+"":e,t);const o=class o{baseUrl;abortController=null;constructor(e="https://app.uselayers.com/api/storefront/v1"){this.baseUrl=e}getApiKey(){if(!window.Layers?.widgetManager)return console.error("Layers Widget Manager not found. Please ensure it's properly initialized."),null;const e=window.Layers.widgetManager.getApiKey();return e||(console.error("No API key found. Please set an API key using Layers.widgetManager.setApiKey()."),null)}cancelRequest(){this.abortController&&(this.abortController.abort(),this.abortController=null)}formatPrice(e){const t=typeof e=="string"?Number.parseFloat(e):e,r=window.Layers?.widgetManager?.config?.currencyFormat||{currency:"USD",locale:"en-US",style:"currency"};if(r.style==="currency"){const a={style:"currency",currency:r.currency};return r.currencyDisplay&&(a.currencyDisplay=r.currencyDisplay),new Intl.NumberFormat(r.locale,a).format(t)}else return new Intl.NumberFormat(r.locale,{style:"decimal",minimumFractionDigits:2,maximumFractionDigits:2}).format(t)}handleApiError(e){return e instanceof DOMException&&e.name==="AbortError"?{code:"REQUEST_ABORTED",message:"The request was aborted"}:e.code?e:e instanceof TypeError&&e.message.includes("fetch")?{code:"NETWORK_ERROR",message:"Network error. Please check your internet connection and try again."}:{code:"UNKNOWN_ERROR",message:e.message||"An unknown error occurred"}}extractPriceInfo(e){const t=this.formatPrice(e.price_range?.to||e.price||0);let r=e.price_range?.compare_at_price||e.compare_at_price;return r&&Number.parseFloat(r)>Number.parseFloat(t)?r=this.formatPrice(r):r=void 0,{price:t,compareAtPrice:r}}generateCacheKey(e,t){try{const r=t?this.sortObjectProperties(t):null;return`${e}:${JSON.stringify(r)}`}catch{return null}}sortObjectProperties(e){return Array.isArray(e)?e.map(t=>this.sortObjectProperties(t)):e!==null&&typeof e=="object"?Object.keys(e).sort().reduce((t,r)=>(t[r]=this.sortObjectProperties(e[r]),t),{}):e}getCachedResponse(e){return o.responseCache.get(e)}setCachedResponse(e,t){e&&t&&o.responseCache.set(e,t)}transformResponse(e){const t=(e.results||[]).map(r=>{const a=r.featured_media?r.featured_media.src:null,s=this.extractPriceInfo(r);return{id:r.id||"",title:r.title||"",price:s.price,compareAtPrice:s.compareAtPrice,imageUrl:a,handle:r.handle||"",attributionToken:e.attributionToken||""}});return{results:t,facets:e.facets||{},metadata:{totalResults:e.totalResults||t.length,processingTimeMs:e.processing_time_ms||0}}}};h(o,"responseCache",new Map);let y=o;const b=({src:n,size:e})=>{try{const t=new URL(n);return e&&t.searchParams.append("width",`${e}`),t.toString()}catch{return n}},C=[90,180,360,540,720,900,1080,1296,1512,1728,1944,2160,2376,2592,2808,3024],I=({src:n,maxWidth:e=3024,srcSetSizes:t=null})=>{const r=(t??C).filter(a=>a<=e);if(r.length!==0)return r.map(a=>`${b({src:n,size:a})} ${a}w`).join(", ")};function M(n){const e=n.split(",");return e.length<2?null:e[0].split(":")[1].split(";")[0]}async function _(n,e,t,r,a,s){if(r<10||a<10)return n.src;const c=document.createElement("canvas"),u=n.naturalWidth/s.width,p=n.naturalHeight/s.height,d=e-s.left,w=t-s.top,m=Math.max(0,d*u),f=Math.max(0,w*p),i=Math.min(n.naturalWidth-m,r*u),l=Math.min(n.naturalHeight-f,a*p);c.width=i,c.height=l;const g=c.getContext("2d",{alpha:!1,willReadFrequently:!0});if(!g)throw new Error("Could not get canvas context");return g.drawImage(n,m,f,i,l,0,0,i,l),c.toDataURL("image/jpeg")}async function A(n,e=.8){return new Promise((t,r)=>{n.toBlob(a=>{a?t(a):r(new Error("Failed to convert canvas to blob"))},"image/jpeg",e)})}export{y as B,b as a,M as b,A as c,I as g,_ as p};