var p=Object.defineProperty;var m=(s,t,e)=>t in s?p(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var u=(s,t,e)=>m(s,typeof t!="symbol"?t+"":t,e);import{f as g,r as S}from"./_favorites-service.BaRMmbSg.min.js";import{i as b,y as w}from"./_scheduler.DOZJZeMb.min.js";import{p as T}from"./_product-card-batch-loader.Btc9rgsa.min.js";const C=({src:s,size:t})=>{try{const e=new URL(s);return t&&e.searchParams.append("width",`${t}`),e.toString()}catch{return s}},v=[90,180,360,540,720,900,1080,1296,1512,1728,1944,2160,2376,2592,2808,3024],y=({src:s,maxWidth:t=3024,srcSetSizes:e=null})=>{const i=(e??v).filter(r=>r<=t);if(i.length!==0)return i.map(r=>`${C({src:s,size:r})} ${r}w`).join(", ")},d=["content-visibility-auto","contain-intrinsic-size-[auto_25.62rem]"],c=[...d,"flex","flex-col","will-change-contents"],f=document.querySelector("#product-card-skeleton-contents-template").content;class l extends HTMLElement{intersectionObserver;productHandle;productTitle;loaded=!1;loading=!1;disconnectedCallback(){this.intersectionObserver&&this.intersectionObserver.disconnect()}connectedCallback(){if(this.loaded||this.loading)return;this.childElementCount===0&&this.appendChild(f.cloneNode(!0)),this.classList.length===0&&this.classList.add(...c.filter(i=>!d.includes(i)));const t=this.getAttribute("handle");t&&(this.productHandle=t,this.updateLinks());const e=this.getAttribute("title");e&&(this.productTitle=e,this.updateTitle()),this.productHandle&&this.init(this.productHandle)}attributeChangedCallback(t,e,i){e!==i&&(t==="handle"&&typeof i=="string"?(this.productHandle=i,this.updateLinks()):t==="title"&&typeof i=="string"&&(this.productTitle=i,this.updateTitle()))}init(t,e=!1){this.intersectionObserver&&this.intersectionObserver.disconnect(),!(this.loading||this.loaded)&&(e?this.loadCard(t):(this.intersectionObserver=new IntersectionObserver((i,r)=>{i.forEach(o=>{o.isIntersecting&&(r.unobserve(this),this.loadCard(t))})},{rootMargin:`0px 0px ${b()?100:200}px 0px`,threshold:.01}),this.intersectionObserver.observe(this)))}async loadCard(t){this.loading||this.loaded||(this.loading=!0,T.addRequest(t).then(async e=>{if(e){const i=this.querySelector("product-card-skeleton-image-placeholder"),r=this.querySelector("img");!i&&!r?.hasAttribute("error")&&e.querySelector("img")?.replaceWith(r),requestAnimationFrame(()=>{this.replaceWith(e)})}else console.log("Failed to load product card",t),this.remove(),g.forEach(i=>{i.productHandle===t&&S(i)});this.loading=!1,this.loaded=!0,await w()}))}updateLinks(){this.productHandle&&this.querySelectorAll("a").forEach(t=>{t.href=`/products/${this.productHandle}`})}updateTitle(){const t=this.querySelector("product-card-skeleton-title");t&&this.title&&(t.textContent=this.title)}updateImage(t,e){const i=t.split("/").pop(),r=`${window.location.protocol}//${window.location.hostname}/cdn/shop/files/${i}`,o=y({src:r,srcSetSizes:[352,728]}),a=`${r}&width=728`,h=this.querySelector("product-card-skeleton-image-placeholder");if(h&&e){const n=new Image(728,1081);n.onload=()=>{setTimeout(()=>{h.replaceWith(n)},0)},n.onerror=()=>{n.setAttribute("error","true")},n.alt=i?.split(".")[0],n.sizes="50vw, (min-width: 1024px) 20vw",n.classList.add("product-image","absolute","inset-0","w-full","h-full","object-cover"),n.loading="eager",n.fetchPriority="high",o&&(n.srcset=o),n.src=a}}}u(l,"observedAttributes",["handle","title"]);function E(s,t,e,i,r=!1){const o=s??new l;return r?o.classList.add(...c.filter(a=>!d.includes(a))):o.classList.add(...c),s||o.replaceChildren(f.cloneNode(!0)),e&&o.setAttribute("handle",e),t&&o.setAttribute("title",t),i&&o.updateImage(i,r),e&&o.init(e,r),o}customElements.get("product-card-skeleton")||customElements.define("product-card-skeleton",l);window.RainbowShops=window.RainbowShops||{};window.RainbowShops.initProductCard=(s,t=void 0,e=void 0)=>E(void 0,t,s,e);export{l as P,E as i};