1import{i as t}from"./bluetti.apiInit.BeRcSX7g.min.js";import"./bluetti.modulepreload-polyfill.YP0FEG5d.min.js";import{B as e}from"./bluetti.baseApi.D5nYpQJ4.min.js";import{B as i}from"./bluetti.utils.BeiRElZ6.min.js";import"./bluetti.constants.Cyvr9pHy.min.js";import"./bluetti.Customer.Ckm4_sV5.min.js";const n=new class extends e{constructor(){super()}async getActivityDetail(){return this.get("/activityapi/buygift/detail")}async getGifts(t){const{items:e}=t,n=e.map(t=>({vId:t.variant_id,qty:t.quantity})),[a,s]=await i(this.post("/activityapi/buygift/gifts",{lineItems:n}));if(a)throw a;const r=(null==s?void 0:s.data)||{};return{gifts:(r.gifts||[]).map(t=>({variant_id:t.gVId,quantity:t.qty})),products:(r.lineItems||[]).map(t=>({variant_id:t.vId,quantity:t.qty}))}}};class a extends HTMLElement{constructor(){super(),this.isProcessing=!1,this.isRefreshing=!1,this.activityInfo=null,this.activityEnabled=!1,this.debounceTimer=null}async connectedCallback(){await t(),await this.checkActivityStatus(),this.activityEnabled&&(this.setupEventListeners(),this.interceptCartRemove(),await this.handleCartChange(),await this.checkAndRemoveOutOfStockGifts()),document.dispatchEvent(new CustomEvent("cart:changed-start")),document.dispatchEvent(new CustomEvent("cart:changed-end"))}disconnectedCallback(){this.removeEventListeners(),this.debounceTimer&&clearTimeout(this.debounceTimer)}async checkActivityStatus(){var t;const[e,a]=await i(n.getActivityDetail());e||(this.activityInfo=null==a?void 0:a.data,this.activityEnabled=1===(null==(t=this.activityInfo)?void 0:t.status),this.activityEnabled||await this.removeAllBuyGifts())}setupEventListeners(){this.boundHandleCartChange=this.handleCartChangeDebounced.bind(this),document.addEventListener("cart:updated",this.boundHandleCartChange),this.boundHandleCartOpen=this.handleCartOpen.bind(this);const t=document.getElementById("cart-offcanvas-nav");t&&t.addEventListener("show",this.boundHandleCartOpen);document.getElementById("main-cart-items")&&(this.boundHandleVisibilityChange=this.handleVisibilityChange.bind(this),document.addEventListener("visibilitychange",this.boundHandleVisibilityChange))}removeEventListeners(){if(this.boundHandleCartChange&&document.removeEventListener("cart:updated",this.boundHandleCartChange),this.boundHandleCartOpen){const t=document.getElementById("cart-offcanvas-nav");t&&t.removeEventListener("show",this.boundHandleCartOpen)}this.boundHandleVisibilityChange&&document.removeEventListener("visibilitychange",this.boundHandleVisibilityChange)}async handleCartOpen(){this.showGiftLoading();try{await this.checkActivityStatus(),this.activityEnabled&&await this.handleCartChange(),await this.checkAndRemoveOutOfStockGifts()}finally{this.hideGiftLoading(),document.dispatchEvent(new CustomEvent("cart:changed-end"))}}async handleVisibilityChange(){if("visible"===document.visibilityState){this.showGiftLoading();try{await this.checkActivityStatus(),this.activityEnabled&&await this.handleCartChange(),await this.checkAndRemoveOutOfStockGifts()}finally{this.hideGiftLoading(),document.dispatchEvent(new CustomEvent("cart:changed-end"))}}}handleCartChangeDebounced(){this.activityEnabled&&this.showGiftLoading(),document.dispatchEvent(new CustomEvent("cart:changed-start")),this.debounceTimer&&clearTimeout(this.debounceTimer),this.debounceTimer=setTimeout(()=>{this.handleCartChange()},300)}async handleCartChange(){if(this.isProcessing||this.isRefreshing)return;if(!this.activityEnabled)return void this.hideGiftLoading();this.isProcessing=!0;let t=!1;try{const e=await this.fetchCart(),i=this.extractNonGiftItems(e);if(0===i.length)return await this.clearAllGifts(e),void(t=!0);t=await this.syncGifts(e,i)}catch(e){}finally{this.isProcessing=!1,t||this.hideGiftLoading(),document.dispatchEvent(new CustomEvent("cart:changed-end"))}}async fetchCart(){const t=await fetch("/cart.js");if(!t.ok)throw new Error("Failed to fetch cart");return t.json()}extractNonGiftItems(t){return t.items.filter(t=>{var e;return"1"!==(null==(e=t.properties)?void 0:e._gift_product)}).map(t=>({variant_id:t.variant_id,quantity:t.quantity}))}async fetchGifts(t){const[e,a]=await i(n.getGifts({items:t}));if(e)throw new Error(`Failed to fetch gifts: ${e.message}`);return(null==a?void 0:a.gifts)||[]}async syncGifts(t,e){const i=await this.fetchGifts(e),n=this.getCurrentGifts(t),a=new Map;
1n.forEach(t=>{a.set(t.variant_id,t)});let s=!1;for(const o of n){const t=i.find(t=>t.variant_id===o.variant_id);t?t.quantity!==o.quantity&&(await this.changeGiftQuantity(o.key,t.quantity),s=!0):(await this.changeGiftQuantity(o.key,0),s=!0)}for(const o of i)if(!a.has(o.variant_id)&&o.quantity>0)try{await this.addGift(o.variant_id,o.quantity),s=!0}catch(r){}return s&&await this.refreshCartUI(!0),s}async clearAllGifts(t){const e=this.getCurrentGifts(t);if(0!==e.length){this.showGiftLoading();for(const t of e)await this.changeGiftQuantity(t.key,0);await this.refreshCartUI(!0)}}async removeAllBuyGifts(){try{const t=(await this.fetchCart()).items.filter(t=>{var e;return"1"===(null==(e=t.properties)?void 0:e._gift_product)});if(0===t.length)return;this.showGiftLoading();for(const e of t)await this.changeGiftQuantity(e.key,0);await this.refreshCartUI(!0)}catch(t){this.hideGiftLoading()}}async checkAndRemoveOutOfStockGifts(){try{const t=await this.fetchCart(),e=this.getCurrentGifts(t);if(0===e.length)return;const i=[];for(const n of e){await this.checkVariantInventory(n.variant_id)||i.push(n)}if(i.length>0){for(const t of i)await this.changeGiftQuantity(t.key,0);await this.refreshCartUI(!1)}}catch(t){this.hideGiftLoading()}}getCurrentGifts(t){return t.items.filter(t=>{var e;return"1"===(null==(e=t.properties)?void 0:e._gift_product)})}async changeGiftQuantity(t,e){const i=await fetch("/cart/change.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({id:t,quantity:e})});if(!i.ok)throw new Error(`Failed to change gift quantity: ${i.statusText}`);return i.json()}async addGift(t,e){var i,n,a;if(!(await this.checkVariantInventory(t)))throw new Error("Gift variant is out of stock");const s=await fetch("/cart/add.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({id:t,quantity:e,properties:{_gift_product:"1",_gift_product_tag:(null==(i=this.activityInfo)?void 0:i.tag)||"",_gift_product_icon:(null==(n=this.activityInfo)?void 0:n.giftIcon)||""}})});if(!s.ok)throw new Error(`Failed to add gift: ${s.statusText}`);const r=await s.json();return null==(a=window.GA4Ecommerce)||a.pushAddToCartEvent(r),r}async checkVariantInventory(t){try{const e=await fetch(`/variants/${t}.js`);if(!e.ok)return!1;return!!(await e.json()).available}catch(e){return!1}}async getCart(){return this.fetchCart()}async removeFromCart(t){return this.changeGiftQuantity(t,0)}async refreshCart(){return this.refreshCartUI()}interceptCartRemove(){const t=(e,i=0)=>{if(!e||e._buyGiftIntercepted)return void(null==e||e._buyGiftIntercepted);if("function"!=typeof e.updateQuantity)return"HEADER-CART-DROPDOWN"===e.tagName&&i<3?void setTimeout(()=>{t(e,i+1)},500*(i+1)):void 0;const n=e.updateQuantity.bind(e);e._buyGiftIntercepted=!0,e.updateQuantity=async(t,e,i)=>{if(0===Number(e))try{const s=await this.getCart();if(!s||!s.items||!Array.isArray(s.items))return n(t,e,i);const r=s.items.filter(t=>t.properties&&"1"===t.properties._gift_product),o=s.items.filter(t=>!t.properties||"1"!==t.properties._gift_product);if(1===r.length&&1===o.length){const e=s.items.find((e,i)=>i+1===parseInt(t));if(e&&(!e.properties||"1"!==e.properties._gift_product)){this.showGiftLoading();const t=r[0],i=e.key;e.variant_id;try{await this.removeFromCart(t.key);await new Promise(t=>setTimeout(t,500)),await this.refreshCart();const e=await this.getCart();return e.items.find(t=>t.key===i)?(await this.removeFromCart(i),await new Promise(t=>setTimeout(t,300)),void(await this.refreshCart())):(await new Promise(t=>setTimeout(t,300)),void(await this.refreshCart()))}catch(a){this.hideGiftLoading()}}}}catch(a){}return n(t,e,i)}},e=document.querySelector("cart-items");e&&t(e,0);const i=document.querySelector("header-cart-dropdown");i&&t(i,0);new MutationObserver(e=>{e.forEach(e=>{e.addedNodes.forEach(e=>{var i,n;if(1===e.nodeType){"CART-ITEMS"!==e.tagName&&"HEADER-CART-DROPDOWN"!==e.tagName||t(e,0);const a=null==(i=e.querySelector)?void 0:i.call(e,"cart-items"),s=null==(n=e.querySelector)?void 0:n.call(e,"header-cart-dropdown");a&&t(a,0),s&&t(s,0)}})})}).observe(document.body,{childList:!0,subtree:!0})}getSectionsToRender(){const t=this.getAttribute("data-section-id")||"header-cart-dropdown";return document.getElementById("main-cart-items")?[{id:"main-cart-items",section:t,selector:".js-contents"},{id:"cart-icon-bubble",section:"cart-badge",selector:".tm-cart-badge"}]:[{id:"cart-offcanvas-nav",section:t,selector:"header-cart-dropdown"},{id:"cart-icon-bubble",section:"cart-badge",selector:".tm-cart-badge"}]}async refreshCartUI(t=!0){const e=this.getSectionsToRender();this.showGiftLoading();const i=await fetch("/cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({sections:e.map(t=>t.section).join(","),sections_url:window.location.pathname})});if(!i.ok)throw new Error("Failed to refresh cart UI");const n=await i.json();this.isRefreshing=!0,this.renderContents(n,e,t),setTimeout(()=>{this.isRefreshing=!1},800),document.dispatchEvent(new CustomEvent("cart:refreshed"))}
1renderContents(t,e,i=!0){e.forEach(e=>{var i,n;const a=(null==(i=document.getElementById(e.id))?void 0:i.querySelector(e.selector))||document.getElementById(e.id);a&&(null==(n=t.sections)?void 0:n[e.section])&&(a.innerHTML=this.getSectionInnerHTML(t.sections[e.section],e.selector))}),i||this.showGiftLoading();try{affirm.ui.refresh()}catch(n){}if(this.activityEnabled){const t=document.querySelector("gift-progress-indicator");t&&t.remove()}else{const t=document.querySelector(".gift-container");t&&t.remove()}i&&this.hideGiftLoading()}getSectionInnerHTML(t,e){const i=(new DOMParser).parseFromString(t,"text/html").querySelector(e);return i?i.innerHTML:""}showGiftLoading(){const t=document.getElementById("gift-loading-overlay");t&&t.classList.add("active")}hideGiftLoading(){const t=document.getElementById("gift-loading-overlay");t&&t.classList.remove("active")}}customElements.define("cart-gift-manager",a);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.