(function(){"use strict";var CACHE_PREFIX="siblings_v3_",MAX_CACHE_ENTRIES=50,cachedKeys=[];function cacheKey(styleRef){return CACHE_PREFIX+styleRef}function getFromCache(styleRef){try{var raw=sessionStorage.getItem(cacheKey(styleRef));if(raw)return JSON.parse(raw)}catch{}return null}function saveToCache(styleRef,products){try{pruneCache(),sessionStorage.setItem(cacheKey(styleRef),JSON.stringify(products)),cachedKeys.push(cacheKey(styleRef))}catch{}}function pruneCache(){try{for(;cachedKeys.length>=MAX_CACHE_ENTRIES;)sessionStorage.removeItem(cachedKeys.shift())}catch{}}var pendingFetches={};function filterSiblings(products,styleRef){var erpPrefix=styleRef+"-",styleRefLower=styleRef.toLowerCase();return products.filter(function(p){return p.tags&&p.tags.length?p.tags.some(function(t){return t.indexOf(erpPrefix)===0}):p.handle.indexOf(styleRefLower)!==-1})}var USE_STOREFRONT_API=!!(window.STOREFRONT_ACCESS_TOKEN&&window.STOREFRONT_ACCESS_TOKEN.length>10&&window.STOREFRONT_DOMAIN);function normalizeFromStorefront(node){var colorHex=null,colorName=null;if(node.options)for(var i=0;i0&&(colorName=opt.optionValues[0].name,opt.optionValues[0].swatch&&opt.optionValues[0].swatch.color&&(colorHex=opt.optionValues[0].swatch.color));break}}return{handle:node.handle,title:node.title,tags:node.tags||[],featured_image:(function(){var edges=node.images&&node.images.edges,img=edges&&edges[1]||edges&&edges[0];return img?{url:img.node.url}:null})(),variants:node.variants&&node.variants.edges?node.variants.edges.map(function(e){return{title:e.node.title}}):[],colorHex,colorName,available:node.availableForSale!==!1}}function fetchViaSfAPI(styleRef){var query='{ products(first: 20, query: "'+styleRef+'") { edges { node { handle title tags availableForSale images(first: 2) { edges { node { url } } } options { name optionValues { name swatch { color } } } variants(first: 1) { edges { node { title } } } } }} }';return fetch("https://"+window.STOREFRONT_DOMAIN+"/api/2024-07/graphql.json",{method:"POST",headers:{"Content-Type":"application/json","X-Shopify-Storefront-Access-Token":window.STOREFRONT_ACCESS_TOKEN},body:JSON.stringify({query})}).then(function(r){if(!r.ok)throw new Error("HTTP "+r.status);return r.json()}).then(function(json){if(json.errors)throw new Error(json.errors[0].message);var edges=json.data&&json.data.products&&json.data.products.edges||[];return filterSiblings(edges.map(function(e){return normalizeFromStorefront(e.node)}),styleRef)})}function fetchViaPredictive(styleRef){var params=["q="+encodeURIComponent(styleRef),"resources[type]=product","resources[limit]=20","resources[options][fields]=tag","resources[options][unavailable_products]=hide"].join("&");return fetch("/search/suggest.json?"+params).then(function(r){if(!r.ok)throw new Error("HTTP "+r.status);return r.json()}).then(function(json){var results=json.resources&&json.resources.results&&json.resources.results.products||[];return filterSiblings(results,styleRef)})}function fetchSiblings(styleRef){var cached=getFromCache(styleRef);if(cached)return Promise.resolve(cached);if(pendingFetches[styleRef])return pendingFetches[styleRef];var primaryFetch=USE_STOREFRONT_API?fetchViaSfAPI(styleRef):fetchViaPredictive(styleRef),promise=primaryFetch.catch(function(){return USE_STOREFRONT_API?fetchViaPredictive(styleRef):null}).then(function(products){return products&&saveToCache(styleRef,products),delete pendingFetches[styleRef],products}).catch(function(){return delete pendingFetches[styleRef],null});return pendingFetches[styleRef]=promise,promise}function getColorName(product){if(product.colorName)return product.colorName;if(product.variants&&product.variants.length>0){var variantTitle=product.variants[0].title;if(variantTitle&&variantTitle!=="Default Title"){var parts=variantTitle.split(" / ");return parts[parts.length-1]}}return product.title}function renderSwatches(container,siblings,currentHandle){if(container.querySelectorAll(".color-siblings__skeleton").forEach(function(el){el.remove()}),!(!siblings||siblings.length===0)){var swatchStyle=container.dataset.swatchStyle||"image",otherSiblings=siblings.filter(function(s){return s.handle!==currentHandle&&s.available!==!1});if(otherSiblings.length!==0){var fragment=document.createDocumentFragment();otherSiblings.forEach(function(sibling){var colorName=getColorName(sibling),link=document.createElement("a");if(link.href="/products/"+sibling.handle,link.className="color-siblings__swatch",link.setAttribute("aria-label",colorName),link.setAttribute("title",colorName),link.dataset.handle=sibling.handle,swatchStyle==="color"){var hex=sibling.colorHex||null,dot=document.createElement("span");dot.className=hex?"color-siblings__swatch-dot":"color-siblings__swatch-fallback",hex&&(dot.style.backgroundColor=hex),link.appendChild(dot)}else{var imgObj=sibling.featured_image||sibling.image,imgUrl=imgObj&&imgObj.url;if(imgUrl){var media=document.createElement("span");media.className="color-siblings__swatch-media";var img=document.createElement("img");img.src=imgUrl+(imgUrl.indexOf("?")!==-1?"&":"?")+"width=88",img.alt=colorName,img.loading="lazy",img.decoding="async",img.className="color-siblings__swatch-img",media.appendChild(img),link.appendChild(media)}else{var fallback=document.createElement("span");fallback.className="color-siblings__swatch-fallback",link.appendChild(fallback)}}fragment.appendChild(link)}),container.appendChild(fragment)}}}function showSkeletons(container){for(var fragment=document.createDocumentFragment(),i=0;i<3;i++){var skeleton=document.createElement("span");skeleton.className="color-siblings__swatch color-siblings__skeleton",skeleton.setAttribute("aria-hidden","true");var inner=document.createElement("span");inner.className="color-siblings__swatch-fallback color-siblings__swatch-fallback--skeleton",skeleton.appendChild(inner),fragment.appendChild(skeleton)}container.appendChild(fragment)}function getBreadcrumbSectionId(){var el=document.querySelector(".breadcrumb-section[data-section-id]");return el?el.dataset.sectionId:null}function rerunScripts(container){container.querySelectorAll("script").forEach(function(oldScript){var newScript=document.createElement("script");Array.from(oldScript.attributes).forEach(function(attr){newScript.setAttribute(attr.name,attr.value)}),newScript.textContent=oldScript.textContent,oldScript.parentNode.replaceChild(newScript,oldScript)})}function findCardParent(el){for(var node=el.parentElement;node&&node!==document.body;){var tag=node.tagName,cls=typeof node.className=="string"?node.className:"";if(tag==="LI"||tag==="ARTICLE"||tag==="PRODUCT-CARD"||cls.indexOf("product-card")!==-1||cls.indexOf("product-item")!==-1||cls.indexOf("grid-item")!==-1||cls.indexOf("card--product")!==-1)return node;node=node.parentElement}return el.parentElement}var sectionCache={};function prefetchSection(handle,sectionsParam){if(sectionCache[handle])return sectionCache[handle];var url="/products/"+handle+"?sections="+sectionsParam,promise=fetch(url).then(function(r){if(!r.ok)throw new Error("HTTP "+r.status);return r.json()}).catch(function(){return delete sectionCache[handle],null});return sectionCache[handle]=promise,promise}function initPDP(){var container=document.querySelector('[data-style-ref][data-mode="pdp"]');if(container){var styleRef=container.dataset.styleRef,currentHandle=container.dataset.currentHandle,sectionId=container.dataset.sectionId;showSkeletons(container),fetchSiblings(styleRef).then(function(siblings){if(!siblings){container.querySelectorAll(".color-siblings__skeleton").forEach(function(el){el.remove()});return}renderSwatches(container,siblings,currentHandle)});var breadcrumbSectionId=getBreadcrumbSectionId(),sectionsParam=breadcrumbSectionId?sectionId+","+breadcrumbSectionId:sectionId;container.addEventListener("mouseenter",function(e){var swatch=e.target.closest(".color-siblings__swatch");if(swatch&&swatch.getAttribute("aria-current")!=="true"&§ionId){var handle=swatch.dataset.handle;handle&&prefetchSection(handle,sectionsParam)}},!0),container.addEventListener("click",function(e){var swatch=e.target.closest(".color-siblings__swatch");if(swatch){if(swatch.getAttribute("aria-current")==="true"){e.preventDefault();return}if(sectionId){e.preventDefault();var handle=swatch.dataset.handle;container.classList.add("color-siblings--loading");var dataPromise=prefetchSection(handle,sectionsParam);dataPromise.then(function(data){if(!data){window.location.href="/products/"+handle;return}var wrapper=document.getElementById("shopify-section-"+sectionId);if(wrapper&&data[sectionId]){wrapper.innerHTML=data[sectionId],rerunScripts(wrapper),history.pushState({},"","/products/"+handle);var titleEl=wrapper.querySelector("[data-product-title]");if(titleEl&&(document.title=titleEl.dataset.productTitle),breadcrumbSectionId&&data[breadcrumbSectionId]){var breadcrumbWrapper=document.getElementById("shopify-section-"+breadcrumbSectionId);breadcrumbWrapper&&(breadcrumbWrapper.innerHTML=data[breadcrumbSectionId],breadcrumbWrapper.dispatchEvent(new CustomEvent("shopify:section:load",{bubbles:!0})))}initPDP()}else window.location.href="/products/"+handle}).catch(function(){window.location.href="/products/"+handle})}}})}}var CARD_SELECTOR='[data-style-ref][data-mode="card"]';function initPLP(){var inFlight=typeof WeakSet=="function"?new WeakSet:{has:function(){return!1},add:function(){},delete:function(){}};function loadSiblings(container){if(!(container.dataset.loaded||inFlight.has(container))){inFlight.add(container);var styleRef=container.dataset.styleRef,currentHandle=container.dataset.currentHandle;fetchSiblings(styleRef).then(function(siblings){inFlight.delete(container),siblings&&(renderSwatches(container,siblings,currentHandle),container.dataset.loaded="true")}).catch(function(){inFlight.delete(container)})}}var observer=null;function observeCards(){observer||typeof IntersectionObserver!="function"||(observer=new IntersectionObserver(function(entries){entries.forEach(function(entry){entry.isIntersecting&&(observer.unobserve(entry.target),loadSiblings(entry.target))})},{rootMargin:"200px 0px"}),bound.forEach(function(container){container.dataset.loaded||observer.observe(container)}))}var bound=[],seen=typeof WeakSet=="function"?new WeakSet:{has:function(){return!1},add:function(){}};function bindContainer(container){if(!seen.has(container)){seen.add(container),bound.push(container);var card=findCardParent(container),debounceTimer=null;card.addEventListener("mouseenter",function(){container.dataset.loaded||(debounceTimer=setTimeout(function(){loadSiblings(container)},250))}),card.addEventListener("mouseleave",function(){debounceTimer&&(clearTimeout(debounceTimer),debounceTimer=null)}),observer&&!container.dataset.loaded&&observer.observe(container)}}function bindAllIn(root){!root||root.nodeType!==1||(root.matches&&root.matches(CARD_SELECTOR)&&bindContainer(root),root.querySelectorAll&&Array.prototype.forEach.call(root.querySelectorAll(CARD_SELECTOR),bindContainer))}var pendingSwatches=new WeakMap;if(document.addEventListener("click",async function(e){var swatch=e.target.closest&&e.target.closest(".color-siblings__swatch");if(swatch){var container=swatch.closest(CARD_SELECTOR);if(container){var card=container.closest("product-card, li, article"),component=card&&card.querySelector("quick-add-custom-component"),slot=!component&&card&&card.querySelector("[data-gco-quick-add-lazy]"),lazy=window.GCOQuickAddLazy;if(!(!component&&!(slot&&lazy&&typeof lazy.fill=="function"))&&!(component&&typeof component.selectSibling!="function")){var overlayColors=component&&component.querySelector("[data-siblings-colors]");if(!(overlayColors&&overlayColors.offsetParent!==null)){var handle=swatch.dataset.handle;if(handle){e.preventDefault();var intent={};if(pendingSwatches.set(card,intent),!component){if(component=await lazy.fill(slot),pendingSwatches.get(card)!==intent||card.isConnected===!1)return;if(!component){window.location.assign(swatch.href);return}if(card.querySelector("quick-add-custom-component")!==component||typeof component.selectSibling!="function")return}if(Array.prototype.forEach.call(container.querySelectorAll(".color-siblings__swatch"),function(other){other.removeAttribute("aria-current"),other.classList.remove("color-siblings__swatch--active")}),swatch.setAttribute("aria-current","true"),swatch.classList.add("color-siblings__swatch--active"),handle===container.dataset.currentHandle){component.restoreOriginal();return}component.selectSibling(handle)}}}}}}),bindAllIn(document.body),typeof MutationObserver=="function"&&new MutationObserver(function(mutations){mutations.forEach(function(mutation){Array.prototype.forEach.call(mutation.addedNodes,bindAllIn)})}).observe(document.body,{childList:!0,subtree:!0}),typeof window.matchMedia=="function"){var hoverless=window.matchMedia("(hover: none)");hoverless.matches&&observeCards(),typeof hoverless.addEventListener=="function"&&hoverless.addEventListener("change",function(event){event.matches&&observeCards()})}}var hydrations=new WeakMap;function hydrate(container){var handle=container.dataset.currentHandle,ref=container.dataset.styleRef,previous=hydrations.get(container);if(previous&&previous.handle===handle&&previous.ref===ref)return previous.promise;var state={handle,ref};return hydrations.set(container,state),container.setAttribute("aria-busy","true"),state.promise=fetchSiblings(ref).then(function(siblings){return!container.isConnected||hydrations.get(container)!==state||container.dataset.currentHandle!==handle||container.dataset.styleRef!==ref?!1:(container.querySelectorAll('.color-siblings__swatch:not([aria-current="true"])').forEach(function(el){el.remove()}),renderSwatches(container,siblings,handle),siblings!==null)}).catch(function(){return!1}).finally(function(){hydrations.get(container)===state&&(container.removeAttribute("aria-busy"),hydrations.delete(container))}),state.promise}window.ColorSiblings={fetch:fetchSiblings,hydrate,getColorName},typeof document.dispatchEvent=="function"&&document.dispatchEvent(new CustomEvent("gco:color-siblings-ready")),document.readyState==="loading"?document.addEventListener("DOMContentLoaded",function(){initPDP(),initPLP()}):(initPDP(),initPLP())})(); //# sourceMappingURL=/cdn/shop/t/35/assets/gco-color-siblings.js.map?v=39047579473121641121793372365