(function(document2,window2){let giftboxCheckStorage=localStorage.getItem("giftboxStorage");giftboxCheckStorage=giftboxCheckStorage?JSON.parse(giftboxCheckStorage):{};let giftable=giftboxCheckStorage.isCheck?giftboxCheckStorage.isCheck:!1;const gtmHandlerCartItem=async id=>await fetch(`${window2.themeVariables.routes.cartUrl}.js`).then(async response=>(await response.json()).items.find(item=>item.id===Number(id))),addToCart=(product,qtd)=>{let formData={items:[{id:Number(product),quantity:Number(qtd)}]};fetch(window2.Shopify.routes.root+"cart/add.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(formData)}).then(async()=>{const gtmHandlerItem=await gtmHandlerCartItem(product);document2.documentElement.dispatchEvent(new CustomEvent("cart:refresh",{bubbles:!0})),window2.gtmHandler.enhancedEcommerce.cart.add({brand:gtmHandlerItem.vendor,category:gtmHandlerItem.product_type,id:gtmHandlerItem.id,name:gtmHandlerItem.product_type,price:gtmHandlerItem.price,quantity:gtmHandlerItem.quantity,variant:gtmHandlerItem.variant_title})}).catch(error=>{console.error("Error",error)})},removeFromCart=product=>{let formData={updates:{[Number(product)]:0}};fetch(window2.Shopify.routes.root+"cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(formData)}).then(()=>{document2.documentElement.dispatchEvent(new CustomEvent("cart:refresh",{bubbles:!0})),setTimeout(()=>{document2.documentElement.querySelectorAll(".header__cart-count.bubble-count")?.forEach(item=>{item.textContent="0"})},500)}).catch(error=>{console.error("Error",error)})},handleGTMGiftboxChecked=(input,state)=>input.setAttribute("data-gtm-state",state),handleGiftboxMiniCartCheckBox=event=>{const input=event.target,giftableNote=document2.querySelector(".checkbox-giftable-note"),giftableContainer=document2.querySelector(".mini-cart__giftbox"),productId=input.getAttribute("data-giftboxId"),productIventory=input.getAttribute("data-giftboxIventory");input.checked?(input.checked=!0,giftboxCheckStorage.isCheck=!0,giftableNote.classList.add("checkbox-giftable-note--checked"),giftboxCheckStorage.giftboxId=productId,productIventory>0?addToCart(productId,"1"):giftableContainer&&(giftableContainer.style.display="flex"),localStorage.setItem("giftboxStorage",JSON.stringify(giftboxCheckStorage))):(input.checked=!1,giftboxCheckStorage.isCheck=!1,giftableNote.classList.remove("checkbox-giftable-note--checked"),productIventory>0?(removeFromCart(productId),giftboxCheckStorage.giftboxId=""):giftableContainer&&(giftableContainer.style.display="none"),localStorage.setItem("giftboxStorage",JSON.stringify(giftboxCheckStorage))),handleGTMGiftboxChecked(input,input.checked),giftable=input.checked};document2.addEventListener("mini-cart:toggle",()=>{const giftboxActive=document2.querySelector(".mini-cart__info-container")?.getAttribute("data-giftboxActive"),input=document2.getElementById("customer-mini-cart[giftable]"),giftableNote=document2.querySelector(".checkbox-giftable-note"),giftableContainer=document2.querySelector(".mini-cart__giftbox"),productIventory=input?.getAttribute("data-giftboxIventory"),productId=input?.getAttribute("data-giftboxId"),hasGiftbox=document2.querySelector(`[data-productid="${productId}"]`);if(hasGiftbox&&checkGiftboxInTheCart(hasGiftbox),giftboxActive=="true"&&(giftboxCheckStorage.isCheck==!0||giftboxCheckStorage.isCheck=="true"?(input.checked=!0,giftableNote.classList.add("checkbox-giftable-note--checked"),productIventory<=0&&(giftableContainer.style.display="flex")):(input.checked=!1,giftableNote.classList.remove("checkbox-giftable-note--checked"),productIventory<=0&&(giftableContainer.style.display="none")),giftboxCheckStorage.giftboxId!=productId&&giftboxCheckStorage.giftboxId!=""&&(giftboxCheckStorage.giftboxId!=""&&removeFromCart(giftboxCheckStorage.giftboxId),giftboxCheckStorage.giftboxId="",giftboxCheckStorage.isCheck=!1,localStorage.setItem("giftboxStorage",JSON.stringify(giftboxCheckStorage))),giftboxCheckStorage.giftboxId==""&&(input.checked=!1,giftableNote.classList.remove("checkbox-giftable-note--checked"),giftboxCheckStorage.isCheck=!1,localStorage.setItem("giftboxStorage",JSON.stringify(giftboxCheckStorage)))),giftboxActive=="false"&&(giftboxCheckStorage.isCheck==!0||giftboxCheckStorage.isCheck=="true")){let giftboxId=giftboxCheckStorage.giftboxId;localStorage.removeItem("giftboxStorage"),handleCheckboxStorage(giftboxId)}input&&(handleGTMGiftboxChecked(input,input.checked),giftable=input.checked,input.removeEventListener("click",handleGiftboxMiniCartCheckBox),input.addEventListener("click",handleGiftboxMiniCartCheckBox))}),document2.addEventListener("mini-cart:rerender",()=>{const input=document2.getElementById("customer-mini-cart[giftable]"),giftableNote=document2.querySelector(".checkbox-giftable-note"),giftableContainer=document2.querySelector(".mini-cart__giftbox"),productId=input?.getAttribute("data-giftboxId"),hasGiftbox=document2.querySelector(`[data-productid="${productId}"]`),productIventory=input?.getAttribute("data-giftboxIventory");input&&(!hasGiftbox&&productIventory>0&&handleRemoveGiftboxOnMiniCart(input,giftableNote),hasGiftbox&&checkGiftboxInTheCart(hasGiftbox),giftable?(giftableNote.classList.add("checkbox-giftable-note--checked"),giftboxCheckStorage.isCheck=!0,localStorage.setItem("giftboxStorage",JSON.stringify(giftboxCheckStorage)),productIventory<=0&&(giftableContainer.style.display="flex")):(giftboxCheckStorage.isCheck=!1,localStorage.setItem("giftboxStorage",JSON.stringify(giftboxCheckStorage)),giftableNote.classList.remove("checkbox-giftable-note--checked"),productIventory<=0&&(giftableContainer.style.display="none")),input.checked=giftable,handleGTMGiftboxChecked(input,input.checked),input.removeEventListener("click",handleGiftboxMiniCartCheckBox),input.addEventListener("click",handleGiftboxMiniCartCheckBox))});const handleRemoveGiftboxOnMiniCart=(input,giftableNote)=>{input&&(input.checked=!1,giftableNote.classList.remove("checkbox-giftable-note--checked"),giftboxCheckStorage.isCheck=!1,giftboxCheckStorage.giftboxId="",localStorage.setItem("giftboxStorage",JSON.stringify(giftboxCheckStorage)),handleGTMGiftboxChecked(input,input.checked),giftable=input.checked,input.removeEventListener("click",handleGiftboxMiniCartCheckBox),input.addEventListener("click",handleGiftboxMiniCartCheckBox))},handleCheckboxStorage=async giftboxId=>{removeFromCart(giftboxId)},checkGiftboxInTheCart=hasGiftbox=>{(giftboxCheckStorage.isCheck==!1||giftboxCheckStorage.isCheck=="false")&&hasGiftbox.classList.remove("line-item__giftable"),(giftboxCheckStorage.isCheck==!0||giftboxCheckStorage.isCheck=="true")&&hasGiftbox.classList.add("line-item__giftable"),removeGiftableIfDontHaveOthersProducts(giftboxCheckStorage.giftboxId)},removeGiftableIfDontHaveOthersProducts=async giftableId=>await fetch(`${window2.themeVariables.routes.cartUrl}.js`).then(async response=>{const cartContent=await response.json();cartContent.items.find(item=>item.id==Number(giftableId))&&cartContent.item_count==1&&removeFromCart(giftableId)})})(document,window); //# sourceMappingURL=/cdn/shop/t/115/assets/ob-checkbox-giftable.js.map?v=184198927971507802481718113197