import{a as T}from"./chunk-OLA3HACK.js";import{b as R}from"./chunk-VR6HZEGV.js";import{a as M}from"./chunk-O7DGIWOA.js";import{a as j}from"./chunk-IU3NNIJ5.js";import{a as F}from"./chunk-42JUHPQJ.js";import{a as $}from"./chunk-T7ECN7SR.js";import{a as I}from"./chunk-6KDDFUPR.js";import{a as A}from"./chunk-FPSGQXCN.js";import{c as O,e as U}from"./chunk-X7PPDKMC.js";import{L as l,S as y,Td as P,U as g,V as E,aa as L,da as d,de as f,ee as x,f as c,ka as q,p as k,q as h,qa as s,r as w,u as C,v as S,w as b}from"./chunk-H4PAM23B.js";import{e as _}from"./chunk-YEWEO3AL.js";var oe=(()=>{let r=class r{constructor(){this.esolveShippingService=s(x),this.esolveCartService=s(P),this.authService=s(A),this.cartService=s(j),this.locationService=s(F),this.config=s(I),this.paymentService=s(R),this.analyticsService=s(O),this.transactionLock=s(T),this.accountService=s($),this.shippingService=s(M),this.$session=this.authService.onValidSession(),this.required_modules=this.config.required,this.checkoutStart=new c,this.checkoutEnd=new c,this.paymentStart=new c,this.shippingCostLoad=new c,this.shipping_costs_streams_map=new Map}onCheckoutStart(){return this.checkoutStart.asObservable()}onCheckoutEnd(){return this.checkoutEnd.asObservable()}onPaymentStart(){return this.paymentStart.asObservable()}onShippingCostLoad(){return this.shippingCostLoad.asObservable()}getShippingCosts(e="order"){this.shipping_costs_streams_map.has(e)||this.storeShippingCostsStream(e);let t=this.shipping_costs_streams_map.get(e);return t||w(()=>new Error("No stream found"))}checkout(e,t="",i,o=!1){return _(this,null,function*(){let p=this.accountService.addresses_id,V=this.locationService.location_id,v=this.shippingService.shipping_id,u=this.paymentService.payment_id;if((e==="order"?this.required_modules.address:this.required_modules.address_on_quote)&&p===0)throw new Error("Please select address");if(this.required_modules.shipping&&v===0)throw new Error("Please select shipping");if(this.required_modules.payment&&u===0&&e==="order")throw new Error("Please select payment");let n=new U;this.checkoutStart.next(),this.transactionLock.start(e);let z=yield this.shippingService.getCheckoutTimeSlot(),N=this.esolveCartService.checkout(e,p,v,u,V,t,z,o).pipe(g(a=>(n.transaction=a,a.id>0&&this.transactionLock.setId(a.id),!n.transaction.process_payment||i===!1?h(n):(this.paymentStart.next(),this.paymentService.setPayment(a.id,u,i).pipe(S(B=>(n.payment=B,n)),l(()=>h(n)))))),g(a=>(this.checkoutEnd.next(),k(this.analyticsService.triggerPurchase(a)))),E(()=>this.cartService.refreshCart()));return C(N)})}parseCheckoutErrors(e){if(!e)return"Unknown error has occurred";if(typeof e=="string")return e;if(e.error_code){let t="";switch(e.error_code){case"tracking_failed":t="Tracking error. Please refresh the application.";break;case"checkout_error":t="Unknown error has occurred.";break;case"break":t="Fatal error occurred on the server.";break;case"busy":t="A previous checkout request has not yet completed, please wait.";break;default:break}if(t!=="")return t}return e.message&&e.message!==""?e.message:"Unknown error has occurred."}storeShippingCostsStream(e="order"){let t=this.getShippingCostsStream(e);this.shipping_costs_streams_map.set(e,t)}getShippingCostsStream(e){let t=b([this.accountService.onAddressSelect(),this.shippingService.onShippingSelect(),this.locationService.onLocationSelect()]);return this.$session.pipe(d(()=>this.cartService.onCartRefresh()),d(()=>t.pipe(S(([i,o,p])=>({addresses_id:i,shipping_id:o,location_id:p})),y((i,o)=>i.addresses_id===o.addresses_id&&i.shipping_id===o.shipping_id&&i.location_id===o.location_id),d(i=>i.addresses_id===0||i.shipping_id===0?h(new f):(this.shippingCostLoad.next(),this.esolveShippingService.getShippingCost(i.shipping_id,i.addresses_id,i.location_id,e).pipe(l(()=>h(new f))))))),L(1))}};r.\u0275fac=function(t){return new(t||r)},r.\u0275prov=q({token:r,factory:r.\u0275fac,providedIn:"root"});let m=r;return m})();export{oe as a};
