if(!customElements.get("add-cart-button")){class AddCartButton extends HTMLElement{_marks={loading:"loading"};_shopify_features_script=document.querySelector("script[id='shopify-features']");_shopify_features_json=JSON.parse(this._shopify_features_script.innerHTML);_checkout_headers={Accept:"application/json","Content-Type":"application/json",authorization:"Basic "+btoa(this._shopify_features_json.accessToken)};get _variantId(){return this.getAttribute("data-variant-id")}get _quantity(){return this.getAttribute("data-quantity")??1}constructor(){super()}connectedCallback(){this.elements={loadingSpinner:this.querySelector(".loading__spinner")},this.addEventListener("click",e=>{this.startLoading(),this.addToCart().finally(()=>{this.finishLoading()})}),this.addEventListener("keydown",e=>{(e.key==="Enter"||e.key===" ")&&(e.preventDefault(),this.click())})}onCartUpdate(){fetch(`${routes.cart_url}?section_id=cart-drawer`).then(response=>response.text()).then(responseText=>{const html=new DOMParser().parseFromString(responseText,"text/html"),targetElement=document.querySelector("cart-drawer"),selectors=[".cart-count-bubble",".drawer__header",".sider-cart-top-count",".drawer__inner-empty","cart-drawer-items",".cart-drawer__footer"];for(const selector of selectors){const targetElement2=document.querySelector(selector),sourceElement=html.querySelector(selector);targetElement2&&sourceElement&&targetElement2.replaceWith(sourceElement)}targetElement.classList.add("active"),targetElement.classList.remove("is-empty")}).catch(e=>{console.error(e)}),fetch(`${routes.cart_url}.js`).then(response=>{if(!response.ok)throw new Error("Network response was not ok");return response.json()}).then(cartData=>{const cartItemCount=cartData.item_count;let cartCountBubble=document.querySelector("#cart-icon-bubble .cart-count-bubble");cartCountBubble?(cartCountBubble.querySelector('span[aria-hidden="true"]').textContent=cartItemCount,cartCountBubble.querySelector(".visually-hidden").textContent=`${cartItemCount} item`):(cartCountBubble=document.createElement("div"),cartCountBubble.className="cart-count-bubble",cartCountBubble.innerHTML=`${cartItemCount}${cartItemCount} item`,document.querySelector("#cart-icon-bubble").appendChild(cartCountBubble))}).catch(error=>{console.error("There has been a problem with your fetch operation:",error)}),window.location.href.indexOf("/cart")>0&&fetch(`${routes.cart_url}`).then(response=>response.text()).then(responseText=>{const html=new DOMParser().parseFromString(responseText,"text/html"),selectors=[".cart-count-bubble",".cart-items-js-contents",".subtotal-content"];for(const selector of selectors){const targetElement=document.querySelector(selector),sourceElement=html.querySelector(selector);targetElement&&sourceElement&&targetElement.replaceWith(sourceElement)}}).catch(e=>{console.error(e)}),window.cart_proxy.isActive=!1}async addToCart(){let data_json={items:this._variantId.split(",").map(id=>({form_type:"product",utf8:"\u2713",id:parseInt(id,10),quantity:this._quantity}))},data=JSON.stringify(data_json);return fetch("/cart/add.json",{method:"POST",headers:this._checkout_headers,body:data}).then(response=>{if(response.ok)return response.json();throw response.status===422&&alert("Inventory shortage!"),new Error("\u8BF7\u6C42\u5931\u8D25")}).then(response=>this.updateCart(response)).catch(err=>{console.error(err)})}async updateCart(response){window.cart_proxy.isActive=!0,this.onCartUpdate()}startLoading(){this.setAttribute("aria-disabled",!0),this.classList.add(this._marks.loading),this.elements.loadingSpinner?.classList.remove("hidden")}finishLoading(){this.removeAttribute("aria-disabled"),this.classList.remove(this._marks.loading),this.elements.loadingSpinner?.classList.add("hidden")}}customElements.define("add-cart-button",AddCartButton)}
//# sourceMappingURL=/cdn/shop/t/106/assets/component-add-cart-button.js.map?v=122672772192156278871740390148