${style}
${color}
import{Segment}from"@dna/analytics";import{RespondToViewport,WithProfiler}from"@dna/mixins";import*as paths from"@dna/paths";import{storefrontClient}from"@dna/storefront";import styles from"@dna/styles";import{addToCart,getCategoryFromTags,getLineItemProperties,minorToMajorUnit,renderPrice,splitProductName}from"@dna/utils";import{LitElement,classMap,createRef,css,html,nothing,ref,repeat,when}from"@lit";import{Task,TaskStatus}from"@lit/task";class DNAMiniCartUpsell extends WithProfiler(RespondToViewport(LitElement)){static properties={items:{type:Array,state:!0},title:{type:String}};constructor(){super(),this.items=[],this.addedItems=[],this.filteredItems=[],this.addingProduct=null,this.hasUpsell=!1,this.upsellProducts=[],this.$carousel=createRef(),document.addEventListener("dna:cart:open",this._handleOpenCart)}updated(changedProperties){if(super.updated(changedProperties),changedProperties.has("items")&&(this.addedItems=this.addedItems.filter(addedItem=>this.items.find(item=>item.product_id===addedItem)),this.filteredItems=this.items.filter(item=>!this.addedItems.includes(item.product_id)),this.addingProduct=null),changedProperties.has("filteredItems")||changedProperties.has("items")){const latestItem=this.filteredItems?.[0],isOldItem=latestItem&&this.latestItem&&latestItem?.id===this.latestItem?.id;(!latestItem||!isOldItem)&&(this.latestItem=latestItem,this._recommendationsTask.run()),this.requestUpdate()}}_handleOpenCart=()=>{window.setTimeout(()=>{this.$carousel.value.initializeSlides()})};_showNavigation(count){return count>1&&!this.isMobile}get productID(){return this.latestItem?.product_id}get cartItemsIds(){return this.items.map(item=>item.product_id)}_recommendationsTask=new Task(this,{task:async([productID])=>{if(!productID)return;const data=await(await fetch(`/recommendations/products.json?product_id=${productID}&intent=complementary`)).json();return data?.products?this.hasUpsell=!0:this.hasUpsell=!1,this.upsellProducts=data?.products||[],this.upsellProducts},args:()=>[this.productID]});get filteredProducts(){return this.upsellProducts.filter(product=>!this.cartItemsIds.includes(product.id)&&!this.addedItems.includes(product.id)&&product.variants?.length<=1)}async _getProperties(item,variantID){const product=await storefrontClient.getProductByHandle(item.handle),variant=product.variants.find(variant2=>variant2.id===variantID);return getLineItemProperties(product,variant)}_handleAddToCart=async product=>{const variant_id=product?.variants?.[0]?.id,properties=await this._getProperties(product,variant_id);this.addingProduct=product.id,this.addedItems.push(product.id);const productCard=this.shadowRoot.querySelector(`[data-product-id="${product.id}"]`);productCard&&productCard.classList.add("is-adding"),addToCart({id:variant_id,quantity:1,properties}),Segment.protect(()=>{const{style,color}=splitProductName(product.title);Segment.track(Segment.CONSTANTS.EVENTS.CART_ADDON_CTA_CLICKED,{category:style,name:color,price:Number(minorToMajorUnit(product.price)),product_category:product.tags?getCategoryFromTags(product.tags):void 0,product_id:product.id,quantity:1,shopify_product_id:product.variants[0].id})})};_handleProductClick=product=>{const{style,color}=splitProductName(product.title);Segment.protect(()=>{Segment.track(Segment.CONSTANTS.EVENTS.CART_ADDON_CLICKED,{category:style,name:color,price:Number(minorToMajorUnit(product.price)),product_category:product.tags?getCategoryFromTags(product.tags):void 0,product_id:product.id,quantity:1,shopify_product_id:product.variants[0].id})})};_getProductUrl(product){return`${Shopify.routes.root}products/${product.handle}`}_renderProduct(product){const{style,color}=splitProductName(product.title),showCompareAtPrice=product?.compare_at_price&&product.compare_at_price>0&&product.compare_at_price>product.price;return html`
${renderPrice(product.compare_at_price)}
${renderPrice(product.price)}