var CommerceJS=function(s){"use strict";var F=Object.defineProperty;var V=(s,i,l)=>i in s?F(s,i,{enumerable:!0,configurable:!0,writable:!0,value:l}):s[i]=l;var h=(s,i,l)=>V(s,typeof i!="symbol"?i+"":i,l);class i{constructor(n){h(this,"logPrefix");h(this,"timers",{});this.logPrefix=`[CommerceJS - ${n}]`}log(...n){console.log("LOG",this.logPrefix,...n)}info(...n){console.info("INFO",this.logPrefix,...n)}warn(...n){console.warn("WARNING",this.logPrefix,...n)}error(...n){console.error("ERROR",this.logPrefix,...n)}debug(...n){console.debug("DEBUG",this.logPrefix,...n)}startTimer(n){this.timers[n]=Date.now()}stopTimer(n){const t=this.timers[n];if(t){const a=Date.now()-t;delete this.timers[n];const H=Math.floor(a/6e4),j=Math.floor(a%6e4/1e3),x=`${n} completed in ${String(H).padStart(2,"0")}m:${String(j).padStart(2,"0")}s`;this.log(x)}else this.warn(`Timer: ${n} does not exist`)}}const l=()=>{const e=document.querySelector('meta[property="og:url"]');return encodeURIComponent((e==null?void 0:e.getAttribute("content"))??window.location.href.replace(/(^\w+:|^)\/\//,"").replace(/\/$/,""))},C=()=>({url:l()}),S=()=>document.title;var E={VITE_AI_ORCHESTRATION_BASE_URL:"https://shopping-gallery.prd-commerce.sovrnservices.com",VITE_COMMERCEJS_DIST_BASE_URL:"https://d35m0nfeeqvaj5.cloudfront.net",BASE_URL:"./",MODE:"prd",DEV:!1,PROD:!0,SSR:!1};const{VITE_AI_ORCHESTRATION_BASE_URL:T,VITE_COMMERCEJS_DIST_BASE_URL:v}=E,f={aiOrchestrationBaseUrl:T,commerceJsDistBaseUrl:v},D=()=>({headers:{"Content-Type":"application/json"}}),g=(e,n,t)=>fetch(f.aiOrchestrationBaseUrl+n,{method:e,...D(),...t}),U={get:(e,n)=>g("GET",e,n),patch:(e,n)=>g("PATCH",e,n),post:(e,n)=>g("POST",e,n),put:(e,n)=>g("PUT",e,n),delete:(e,n)=>g("DELETE",e,n)},P=(e,n="")=>{if(!e.ok)throw new Error(n||e.statusText);return e.json()},A={shoppingGalleryContainerClassName:"sovrn-commerce-ai-shopping-gallery"},R=new i("configuration"),m={get:()=>{const e=window.CommerceJSConfig;if(!e)throw new Error("CommerceJSConfig is not defined");if(!e.apiKey)throw new Error("CommerceJSConfig.apiKey is not defined");return{...e,shoppingGalleryConfig:{...A,...e.shoppingGalleryConfig}}},set:e=>{if(!e.apiKey)throw new Error("CommerceJSConfig.apiKey is not defined");if(window.CommerceJSConfig){R.warn("CommerceJSConfig is already defined.");return}window.CommerceJSConfig=e}},p=()=>{var o;const n=(o=m.get().shoppingGalleryConfig)==null?void 0:o.shoppingGalleryContainerClassName;if(!n)throw new Error("Shopping gallery container class name not found");const t=document.getElementsByClassName(n);if(!t.length)throw new Error("No shopping gallery containers found");return t},_=async()=>{for(;!document.body;)await new Promise(e=>setTimeout(e,100));return p(),Promise.resolve()},G=async()=>{for(;!document.body;)await new Promise(e=>setTimeout(e,100));return document.body.innerHTML.replace(/)<[^<]*)*<\/script>/gi,"")},$=async e=>{const n=new URLSearchParams,{apiKey:t}=m.get();n.append("apiKey",t),n.append("pageUrl",l());const o=`/ai-orchestration/products?${n.toString()}`;return U.post(o,{body:JSON.stringify(e)}).then(a=>P(a))},B=()=>{const{commerceJsDistBaseUrl:e}=f,n=document.createElement("link");n.rel="stylesheet",n.href=`${e}/commerce-js.css`,document.head.appendChild(n)},N=e=>e.clientWidth||0;var c=(e=>(e.USD="USD",e.AUD="AUD",e.GBP="GBP",e.CAD="CAD",e.EUR="EUR",e.CHF="CHF",e.DKK="DKK",e))(c||{});const O=new i("htmlBuilder"),u=4,y=4,b={buildShoppingGalleries:function(e){if(e.length===0)throw new Error("No products to display");if(e.lengthy&&O.warn(`Only the first ${y} products will be displayed`);const n=p(),o=[...e].splice(0,4).map(this.productTemplate);for(let a=0;a${e.join("")}`)},resizeHandler:function(){w(),Array.from(p()).forEach(e=>{e.firstChild.className=d(e)})},productTemplate:function(e){const n=e.name,t=e.deepLink,o=k(e.currency,e.salePrice);if(!t)throw new Error(`No link provided for the product: ${n}`);return` `}},I=e=>e.replace(/>\s+<"),k=(e,n)=>new Intl.NumberFormat(J(e),{style:"currency",currency:e}).format(n),J=e=>({[c.GBP]:"en-GB",[c.AUD]:"au-AU",[c.USD]:"en-US",[c.CAD]:"en-CA",[c.EUR]:"it-IT",[c.CHF]:"de-CH",[c.DKK]:"dk-DK"})[e],d=(e,n="sovrn-commerce-ai-shopping-gallery-container")=>{const t=N(e);let o=n;return t<800&&t>400?o+=" container--medium":t<=400?o+=" container--small":o+=" container--large",o},w=()=>{Array.from(p()).forEach(e=>{e.className=d(e,"sovrn-commerce-ai-shopping-gallery")})},r=new i("generateShoppingGallery"),K=()=>{(async()=>{r.startTimer("run"),r.info("initiating shopping gallery generation");try{r.info("verifying shopping gallery container present"),await _(),r.info("shopping gallery container present"),w(),r.info("injecting gallery stylesheet"),B(),r.info("gallery stylesheet injected"),r.info("gathering content");const e=await L();r.info("content gathered"),r.info("analyzing content");const n=await $(e);r.info("building shopping galleries"),b.buildShoppingGalleries(n),r.info("shopping galleries built and injected")}catch(e){r.error("error generating shopping gallery",e)}finally{r.stopTimer("run")}})()},L=async()=>{const e=S(),n=C(),t=await G();return{meta:n,title:e,content:t}},M=e=>{var t;const n=e??window.CommerceJSApiKey??(window.vglnk&&typeof window.vglnk!="function"?(t=window.vglnk)==null?void 0:t.key:void 0);if(!n)throw new Error("Sovrn Commerce API key is not defined");m.set({apiKey:n}),K()};return s.init=M,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"}),s}({});