import{DialogComponent,DialogOpenEvent,DialogCloseEvent}from"@theme/dialog";import{StandardEvents}from"@shopify/events";import{isMobileBreakpoint,requestIdleCallback}from"@theme/utilities";let cartItemsScriptsPromise=null;function ensureCartItemsScripts(){if(window.Theme?.template?.name!=="cart")return cartItemsScriptsPromise||(cartItemsScriptsPromise=Promise.all([import("@theme/gco-cart-items"),import("@theme/component-cart-quantity-selector")]),cartItemsScriptsPromise)}class CartDrawerComponent extends DialogComponent{#summaryThreshold=.5;#historyAbortController=null;get#dialog(){return this.refs.dialog??null}connectedCallback(){super.connectedCallback(),document.addEventListener(StandardEvents.cartLinesUpdate,this.#handleCartLinesUpdate),this.addEventListener("click",this.#onTabClick),this.addEventListener(DialogOpenEvent.eventName,this.#updateStickyState),this.addEventListener(DialogOpenEvent.eventName,this.#handleHistoryOpen),this.addEventListener(DialogCloseEvent.eventName,this.#handleHistoryClose),history.state?.cartDrawerOpen&&history.replaceState(null,""),requestIdleCallback(()=>ensureCartItemsScripts(),{timeout:2e3})}disconnectedCallback(){super.disconnectedCallback(),document.removeEventListener(StandardEvents.cartLinesUpdate,this.#handleCartLinesUpdate),this.removeEventListener("click",this.#onTabClick),this.removeEventListener(DialogOpenEvent.eventName,this.#updateStickyState),this.removeEventListener(DialogOpenEvent.eventName,this.#handleHistoryOpen),this.removeEventListener(DialogCloseEvent.eventName,this.#handleHistoryClose),this.#historyAbortController?.abort()}#onTabClick=event=>{const tabButton=event.target.closest(".cart-drawer__tab");if(!(tabButton instanceof HTMLElement))return;const tab=tabButton.dataset.tab;tab&&this.#switchTab(tab)};#switchTab=tab=>{const tabs=this.querySelectorAll(".cart-drawer__tab"),contents=this.querySelectorAll(".cart-drawer__content");tabs.forEach(btn=>{btn.classList.toggle("active",btn.dataset.tab===tab)}),contents.forEach(content=>{const isActive=content.dataset.tabContent===tab;content.classList.toggle("active",isActive),content.style.display=isActive?"flex":"none"})};#handleHistoryOpen=()=>{isMobileBreakpoint()&&(history.state?.cartDrawerOpen||history.pushState({cartDrawerOpen:!0},""),this.#historyAbortController=new AbortController,window.addEventListener("popstate",this.#handlePopState,{signal:this.#historyAbortController.signal}))};#handleHistoryClose=()=>{this.#historyAbortController?.abort(),history.state?.cartDrawerOpen&&history.back()};#handlePopState=async()=>{this.refs.dialog?.open&&(this.refs.dialog.style.setProperty("--dialog-drawer-closing-animation","none"),await this.closeDialog(),this.refs.dialog.style.removeProperty("--dialog-drawer-closing-animation"))};#handleCartLinesUpdate=event=>{const shouldAutoOpen=this.hasAttribute("auto-open")&&event.action==="add"&&!this.#dialog?.open,sourceModal=event.target instanceof Element?event.target.closest("dialog:modal"):null,openDrawer=async()=>{await ensureCartItemsScripts(),this.#switchTab("cart"),this.showDialog()};shouldAutoOpen&&!sourceModal&&!this.#isCartEmpty()&&openDrawer(),event.promise?.then(({detail,cart})=>{const settle=()=>requestAnimationFrame(()=>this.#updateStickyState());if(this.#announceCartCount(cart?.totalQuantity),!shouldAutoOpen||detail?.didError){settle();return}const openAndSettle=()=>{this.#dialog?.open||openDrawer(),settle()};sourceModal?.open?sourceModal.addEventListener("close",openAndSettle,{once:!0}):openAndSettle()}).catch(error=>{error?.name!=="AbortError"&&console.warn("[cart-drawer] Event promise rejected:",error)})};#announceCartCount(cartCount){const liveRegion=this.refs.liveRegion;!this.refs.dialog?.open||!liveRegion||cartCount===void 0||(liveRegion.textContent=`${Theme.translations.cart_count}: ${cartCount}`)}async open(){await ensureCartItemsScripts(),this.#switchTab("cart"),this.showDialog(),customElements.whenDefined("shopify-payment-terms").then(()=>{document.querySelector("shopify-payment-terms")?.shadowRoot?.querySelector("#shopify-installments-cta")?.addEventListener("click",this.closeDialog,{once:!0})})}close(){this.closeDialog()}#isCartEmpty(){return!!this.querySelector(".cart-drawer--empty")}#updateStickyState(){const dialog=this.#dialog;if(!dialog)return;const content=dialog.querySelector(".cart-drawer__content"),summary=dialog.querySelector(".cart-drawer__summary");if(!content||!summary){dialog.setAttribute("cart-summary-sticky","false");return}const drawerHeight=dialog.getBoundingClientRect().height,ratio=summary.getBoundingClientRect().height/drawerHeight;dialog.setAttribute("cart-summary-sticky",ratio>this.#summaryThreshold?"false":"true")}}customElements.get("cart-drawer-component")||customElements.define("cart-drawer-component",CartDrawerComponent); //# sourceMappingURL=/cdn/shop/t/35/assets/gco-cart-drawer.js.map?v=54412405358747473741792594883