(()=>{"use strict";var t={n:a=>{var e=a&&a.__esModule?()=>a.default:()=>a;return t.d(e,{a:e}),e},d:(a,e)=>{for(var i in e)t.o(e,i)&&!t.o(a,i)&&Object.defineProperty(a,i,{enumerable:!0,get:e[i]})},o:(t,a)=>Object.prototype.hasOwnProperty.call(t,a),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},a={};t.r(a),t.d(a,{default:()=>c});const e=window.jQuery;var i=t.n(e);const s=window.wcPPCP.utils,n=window.wp.apiFetch;var r=t.n(n);const d=window.wcPPCP.actions;var o=t.n(d);const c=new class{constructor(){this.actions=new(o()),this.data=(0,s.getSetting)("cart"),this.page=(0,s.getSetting)("generalData").page,this.processing=!1,i()(document.body).on("updated_wc_div",this.onCartUpdated.bind(this)),i()(document.body).on("updated_cart_totals",this.onCartUpdated.bind(this)),i()(document.body).on("updated_checkout",this.onUpdatedCheckout.bind(this)),i()(document.body).on("wc_fragments_refreshed wc_fragments_loaded",this.onCartFragmentsChanged.bind(this)),document.addEventListener("wc-ppcp:mini-cart-ready",({detail:t})=>{this.actions.doAction("mini_cart_ready",this)}),document.addEventListener("wc-ppcp:mini-cart-updated",this.onMiniCartUpdated.bind(this))}async onCartUpdated(t){let a;window?.wcPPCPCartData?.cart?(a=window.wcPPCPCartData,this.data={...this.data,...a.cart}):a=await this.refreshData(),(0,s.setSetting)("queryParams",this.data.queryParams),this.actions.doAction("cart_updated",a,this),window.wcPPCPCartData=null}onMiniCartUpdated({detail:t}){const a=t.cart||null;a&&a?.extensions?.wc_ppcp?.cart&&(this.data={...this.data,...a.extensions.wc_ppcp.cart},this.actions.doAction("mini_cart_updated",this))}on(t,a){this.actions.addAction(t,a)}trigger(t,...a){this.actions.doAction(t,...a)}applyFilters(t,...a){return this.actions.applyFilters(t,...a)}async onUpdatedCheckout(t,a){let e={};if(a?.fragments?.wc_ppcp_data){e=a.fragments.wc_ppcp_data;const{cart:t,queryParams:i=null}=e;this.data={...this.data,...t},i&&(0,s.setSetting)("queryParams",i)}this.actions.doAction("updated_checkout",e,a)}onCartFragmentsChanged(){setTimeout(()=>{window.wcPPCPMiniCartUpdate&&(this.data={...this.data,...wcPPCPMiniCartUpdate}),this.actions.doAction("fragments_changed",this)},250)}getData(){return(0,s.getSetting)("cart")}getCurrency(){return this.actions.applyFilters("cart_get_currency",this.data.currency,this)}getCountryCode(){return this.data.countryCode}needsShipping(){return this.actions.applyFilters("cart_needs_shipping",this.data?.needsShipping,this)}isEmpty(){return this.data?.isEmpty}isPaymentMethodAvailable(t){var a;return null!==(a=this.data?.availablePaymentMethods?.includes(t))&&void 0!==a&&a}async refreshData(t=null){if(!this.processing)try{this.processing=!0;const t=await r()({method:"POST",url:(0,s.getRestRoute)("cart/refresh"),data:{page:this.page}});return this.data={...this.data,...t.cart},this.data.queryParams=t.queryParams,t}catch(t){console.log(t)}finally{this.processing=!1}}async addToCart(t){return r()({method:"POST",url:(0,s.getRestRoute)("cart/item"),data:await this.actions.applyFiltersAsync("add_to_cart_data",t,this)}).then(t=>(t.cart&&(this.data={...this.data,...t.cart}),t)).catch(t=>{throw t})}removeCartItem(t){return r()({method:"DELETE",url:(0,s.getRestRoute)("cart/item"),data:{key:t}}).then(t=>{this.data={...this.data,...t.cart}}).catch(t=>{console.log("Error removing item from cart. ",t)})}async updateShipping(t){return r()({method:"POST",url:(0,s.getRestRoute)("cart/shipping"),data:t}).then(t=>(t.code||(this.data={...this.data,...t}),this.sanitizeResponse(t))).catch(t=>{throw t})}async updateBilling(t){return r()({method:"POST",url:(0,s.getRestRoute)("cart/billing"),data:t}).then(t=>(t.code||(this.data={...this.data,...t.cart}),t)).catch(t=>{throw t})}async createOrder(t){return r()({method:"POST",url:(0,s.getRestRoute)("cart/order"),data:await this.actions.applyFiltersAsync("create_order_data",t)}).then(t=>this.sanitizeResponse(t))}async doOrderPay(t,a={}){const e=(0,s.getSetting)("order");return r()({method:"POST",url:(0,s.getRestRoute)("order/pay"),data:{payment_method:t,...a,...e}}).then(t=>this.sanitizeResponse(t))}getTotal(t="string"){let a=this.data?.total||0;return"string"===t&&(a=a.toString()),this.actions.applyFilters("cart_total",a,this)}getTotalCents(){return this.actions.applyFilters("cart_total_cents",parseInt(this.data?.totalCents||0),this)}getLineItems(){return this.data.lineItems||[]}getShippingOptions(){return this.data.shippingOptions||[]}getSelectedShippingMethod(){return this.data.selectedShippingMethod||""}sanitizeResponse(t){return"string"==typeof t?t=t.replace(/<[^>]*>/g,""):Array.isArray(t)&&(t=this.sanitizeResponse(t[0])),t}};(this.wcPPCP=this.wcPPCP||{}).cart=a})();
//# sourceMappingURL=cart.js.map