/*! Copyright (c) Safe As Milk. All rights reserved. */import{formatMoney}from"utils";class VariantSelects extends HTMLElement{#addToCartText;connectedCallback(){this.productAvailable=this.getVariantData().some(variant=>variant.available),this.uniqueId=this.dataset.card||this.dataset.section,this.#addToCartText=this.dataset.addToCartText||window.theme.localize("ADD_TO_CART"),this.addEventListener("change",this.onVariantChange)}disconnectedCallback(){this.removeEventListener("change",this.onVariantChange)}onVariantChange(){this.updateOptions(),this.updateMasterId(),this.updatePickupAvailability(),this.removeErrorMessage(),this.currentVariant?(this.updateURL(),this.updateVariantInput(),this.renderProductInfo(),this.updateLabelsBlock()):(this.toggleAddButton(!0,""),this.setUnavailable()),this.dispatchEvent(new CustomEvent("on:variant:change",{detail:{variant:this.currentVariant}}))}updateOptions(){this.options=Array.from(this.querySelectorAll("select"),select=>select.value)}updateMasterId(){this.currentVariant=this.getVariantData().find(variant=>!variant.options.map((option,index)=>this.options[index]===option).includes(!1))}updateURL(){!this.currentVariant||this.dataset.updateUrl==="false"||window.history.replaceState({},"",`${this.dataset.url}?variant=${this.currentVariant.id}`)}updateVariantInput(){document.querySelectorAll(`#product-form-${this.uniqueId}, #product-form-installment-${this.uniqueId}`).forEach(productForm=>{const input=productForm.querySelector('input[name="id"]');input.value=this.currentVariant.id,input.dispatchEvent(new Event("change",{bubbles:!0}))});const productSingle=this.closest("product-single");productSingle&&(productSingle.variant=this.currentVariant)}updatePickupAvailability(){const pickupAvailability=document.querySelector("pickup-availability");pickupAvailability&&pickupAvailability.fetchAvailability(this.currentVariant.id)}removeErrorMessage(){const section=this.closest("section");section&§ion.querySelector(".qty-error")?.remove()}renderProductInfo(){const priceContainer=document.getElementById(`price-${this.uniqueId}`);if(priceContainer){const variantPrice=`
${window.theme.localize("REGULAR_PRICE")}
${formatMoney(this.currentVariant.price,window.theme.money_product_price_format)}
${this.currentVariant.compare_at_price>this.currentVariant.price?`
${window.theme.localize("SALE_PRICE")}
${formatMoney(this.currentVariant.compare_at_price,window.theme.money_format)}
`:""}
`;priceContainer.innerHTML=variantPrice}const unitPriceContainer=document.getElementById(`unit-price-${this.uniqueId}`);if(unitPriceContainer){const unitPrice=this.currentVariant.unit_price?`
${window.theme.localize("UNIT_PRICE_LABEL")}
${formatMoney(this.currentVariant.unit_price,window.theme.money_format)}
/
${window.theme.localize("UNIT_PRICE_SEPARATOR")}
${this.productAvailable&&this.currentVariant.unit_price_measurement?`
${this.currentVariant.unit_price_measurement.reference_value!==1?this.currentVariant.unit_price_measurement.reference_value:""}
${this.currentVariant.unit_price_measurement.reference_unit}
`:""}
`:"";unitPriceContainer.innerHTML=unitPrice}const labelContainer=document.getElementById(`label-${this.uniqueId}`);if(labelContainer)if(this.currentVariant.compare_at_price>this.currentVariant.price){const labelType=labelContainer.dataset.type;let text;switch(labelType){case"currency":{const discountInCurrency=formatMoney(Math.round(this.currentVariant.compare_at_price-this.currentVariant.price),window.theme.money_format);text=window.theme.localize("DISCOUNT_CURRENCY",discountInCurrency);break}case"percentage":{const discountInPercentage=Math.round((this.currentVariant.compare_at_price-this.currentVariant.price)*100/this.currentVariant.compare_at_price);text=window.theme.localize("DISCOUNT_PERCENTAGE",discountInPercentage);break}case"text":default:text=window.theme.localize("DISCOUNT_TEXT")}const label=`
`;labelContainer.innerHTML=label}else labelContainer.innerHTML="";const inventoryNoticeContainer=document.getElementById(`inventory-notice-${this.uniqueId}`);if(inventoryNoticeContainer){const inventoryNoticeWrapperContainer=inventoryNoticeContainer.querySelector(".stock-note"),inventoryNoticeTextContainer=inventoryNoticeContainer.querySelector(".stock-note__text"),inventoryShowQty=inventoryNoticeContainer.dataset.showQty,inventoryLimit=Number(inventoryNoticeContainer.dataset.inventoryLimit),currentVariantQuantity=Number(document.getElementById(`quantity-${this.uniqueId}-${this.currentVariant.id}`).dataset.qty),soldClass="stock-note--sold",inStockClass="stock-note--in-stock",lowStockClass="stock-note--low",continueSelling=this.currentVariant.available&¤tVariantQuantity<=0,shopifyManaged=this.currentVariant.inventory_management==="shopify";this.currentVariant.available?shopifyManaged&&inventoryShowQty==="true"?(inventoryNoticeWrapperContainer.classList.remove(soldClass,lowStockClass),inventoryNoticeWrapperContainer.classList.add(inStockClass),!continueSelling&¤tVariantQuantity>0?currentVariantQuantity<=inventoryLimit?(inventoryNoticeWrapperContainer.classList.add(lowStockClass),inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_NUMBER_LOW_STOCK_HTML",currentVariantQuantity)):inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_NUMBER_IN_STOCK_HTML",currentVariantQuantity):continueSelling?inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_CONTINUE_SELLING"):inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_IN_STOCK")):shopifyManaged&&inventoryShowQty==="false"?(inventoryNoticeWrapperContainer.classList.remove(soldClass,lowStockClass),inventoryNoticeWrapperContainer.classList.add(inStockClass),!continueSelling&¤tVariantQuantity>0?currentVariantQuantity<=inventoryLimit?(inventoryNoticeWrapperContainer.classList.add(lowStockClass),inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_LOW_STOCK")):inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_IN_STOCK"):continueSelling?inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_CONTINUE_SELLING"):inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_IN_STOCK")):(inventoryNoticeWrapperContainer.classList.remove(soldClass,lowStockClass),inventoryNoticeWrapperContainer.classList.add(inStockClass),inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_IN_STOCK")):(inventoryNoticeWrapperContainer.classList.remove(inStockClass,lowStockClass),inventoryNoticeWrapperContainer.classList.add(soldClass),inventoryNoticeTextContainer.innerHTML=window.theme.localize("QTY_NOTICE_SOLD_OUT"))}const skuContainer=document.getElementById(`sku-${this.uniqueId}`);if(skuContainer){const sku=this.currentVariant.sku?`
${skuContainer.dataset.skuLabel?`${skuContainer.dataset.skuLabel} `:""}${this.currentVariant.sku}
`:"";skuContainer.innerHTML=sku}this.toggleAddButton(!this.currentVariant.available,window.theme.localize("SOLD_OUT"))}updateLabelsBlock(){const soldOutLabel=document.getElementById(`label-${this.uniqueId}-sold_out`),discountLabel=document.getElementById(`label-${this.uniqueId}-discount`),newLabel=document.getElementById(`label-${this.uniqueId}-new`),customLabel1=document.getElementById(`label-${this.uniqueId}-custom_1`),customLabel2=document.getElementById(`label-${this.uniqueId}-custom_2`);if(soldOutLabel&&(this.currentVariant.available?soldOutLabel.setAttribute("hidden",""):soldOutLabel.removeAttribute("hidden")),discountLabel)if(this.currentVariant.compare_at_price>this.currentVariant.price){const textElement=discountLabel.querySelector(".label__text");let text="";const discountType=discountLabel.dataset.type,hideIfSoldOut=discountLabel.dataset.hide==="true"&&!this.currentVariant.available;switch(discountType){case"currency":text=window.theme.localize("DISCOUNT_CURRENCY",formatMoney(Math.round(this.currentVariant.compare_at_price-this.currentVariant.price),window.theme.money_format));break;case"percentage":text=window.theme.localize("DISCOUNT_PERCENTAGE",Math.round((this.currentVariant.compare_at_price-this.currentVariant.price)*100/this.currentVariant.compare_at_price));break;case"text":default:text=window.theme.localize("DISCOUNT_TEXT")}text&&!hideIfSoldOut?(textElement.textContent=text,discountLabel.removeAttribute("hidden")):discountLabel.setAttribute("hidden","")}else discountLabel.setAttribute("hidden","");[newLabel,customLabel1,customLabel2].forEach(label=>{label&&label.dataset.hide==="true"&&!this.currentVariant.available?label.setAttribute("hidden",""):label&&label.removeAttribute("hidden")})}toggleAddButton(disable=!0,text=""){const productForm=document.getElementById(`product-form-${this.uniqueId}`);if(!productForm)return;const buttonsContainer=productForm.querySelector(".js-product-buttons"),addButton=productForm.querySelector('[name="add"]'),addButtonText=productForm.querySelector('[name="add"] staged-action-text');addButton&&(disable?(addButton.setAttribute("disabled","disabled"),text&&(addButtonText.textContent=text),buttonsContainer.classList.add("is-disabled")):(addButton.removeAttribute("disabled"),addButtonText.textContent=this.#addToCartText,buttonsContainer.classList.remove("is-disabled")))}setUnavailable(){const productForm=document.getElementById(`product-form-${this.uniqueId}`),addButton=productForm.querySelector('[name="add"]'),addButtonText=productForm.querySelector('[name="add"] > span'),price=document.getElementById(`price-${this.uniqueId}`);addButton&&(addButtonText.textContent=window.theme.localize("UNAVAILABLE"),price&&price.classList.add("visibility-hidden"))}getVariantData(){return this.variantData=this.variantData||JSON.parse(this.querySelector('[type="application/json"]').textContent),this.variantData}}customElements.define("variant-selects",VariantSelects);class VariantRadios extends VariantSelects{updateOptions(){const fieldsets=Array.from(this.querySelectorAll("fieldset"));this.options=fieldsets.map(fieldset=>Array.from(fieldset.querySelectorAll("input")).find(radio=>radio.checked).value)}}customElements.define("variant-radios",VariantRadios);class VariantSwatches extends HTMLElement{connectedCallback(){this.currentSelection=this.querySelector("input:checked").value;const form=document.getElementById(this.dataset.formId);form?form.addEventListener("change",()=>{this.updateLabel()}):this.addEventListener("change",()=>{this.updateLabel()})}updateLabel(){const currentSelection=this.querySelector("input:checked").value;if(currentSelection!==this.currentSelection){const labelOptionElement=this.querySelector(".js-option-title");labelOptionElement.innerHTML=currentSelection,this.currentSelection=currentSelection}}}customElements.define("variant-swatches",VariantSwatches);class VariantMixedInputs extends VariantSelects{updateOptions(){this.options=Array.from(this.querySelectorAll("input:checked, select"),element=>element.value)}}customElements.define("variant-mixed-inputs",VariantMixedInputs);
//# sourceMappingURL=/cdn/shop/t/100/assets/variant-select-components.js.map?v=162452138268678519961751575325