import{R as f,c as i,s as g,a4 as h,a5 as d,Q as n,b as p,a6 as P,a as y,P as w}from"./BCLrUOBx.js";const q=()=>({cartCount:0,cartProducts:[]}),k={emptyCartProduct(){this.cartProducts=[],this.cartCount=0},subtractCartProductCount(t){this.cartProducts=this.cartProducts.filter(e=>parseInt(e.selected)!==(t==null?void 0:t.status)),this.cartCount=parseInt(this.cartCount)-parseInt(t.qty)},setCartCount(t){this.cartCount=t},setCartProducts(t){this.cartProducts=t,this.cartCount=t.reduce((e,r)=>e+parseInt(r.quantity),0)},cartProductAction(t){const e=this.cartProducts.findIndex(r=>r.id===t.id);e===-1?this.cartProducts.push(t):(this.cartProducts[e].quantity=0,this.cartProducts[e].quantity=t.quantity)},deleteCart(t){const e=this.cartProducts.findIndex(r=>r.id===t.id);this.cartProducts.splice(e,1),this.cartCount-=parseInt(t.quantity)},async getCartByUser({lang:t}){try{const{token:e}=i(),{getUserToken:r}=y(),s=await h.getRequest(`${d.api.cartByUser}?${w({user_token:await r()})}`,e,t);(s==null?void 0:s.status)===200?this.setCartProducts(s.data):n({statusCode:s==null?void 0:s.status,message:s==null?void 0:s.message})}catch(e){n({statusCode:400,message:e.message})}},async buyNow({payload:t,lang:e}){try{const{token:r}=i(),s=await h.postRequest(d.api.buyNow,t,r);return(s==null?void 0:s.status)===200&&await this.getCartByUser({lang:e,params:t}),s}catch(r){n({statusCode:400,message:r.message})}},async cartAction({payload:t,lang:e}){var r;try{const{setToastMessage:s,setToastError:c}=p(),m=i(),{token:o}=g(m),u=t.apiVal,C=t.storeVal,a=await h.postRequest(d.api.cartAction,u,o.value);if((a==null?void 0:a.status)===200){const{t:l}=P().$i18n;s(l("cart.productAdded")),t.isBundle&&await this.getCartByUser({lang:e,params:t})}else(a==null?void 0:a.status)===201?c((r=a==null?void 0:a.data)==null?void 0:r.form[0]):n({statusCode:a==null?void 0:a.status,message:a==null?void 0:a.message})}catch(s){n({statusCode:400,message:s.message})}},async cartDelete({payload:t,lang:e}){try{const r=i(),{token:s}=g(r),c=await h.deleteRequest(`${d.api.cartDelete}/${t.id}`,s.value);(c==null?void 0:c.status)===200?await this.getCartByUser({params:t,lang:e}):n({statusCode:c==null?void 0:c.status,message:c==null?void 0:c.message})}catch(r){n({statusCode:400,message:r.message})}},async cartChanged({payload:t,lang:e}){var r;try{const{setToastError:s}=p(),c=i(),{token:m}=g(c),o={checked:t.checked,unchecked:[],isBundle:!1};this.cartProducts.forEach((C,a)=>{o.checked.indexOf(parseInt(C.id))===-1?(o.unchecked.push(C.id),this.cartProducts[a].selected=2):this.cartProducts[a].selected=1,!o.isBundle&&C.bundle_deal&&(o.isBundle=!0)});const u=await h.postRequest(d.api.cartChanged,o,m.value);(u==null?void 0:u.status)!==200?s((r=u==null?void 0:u.data)==null?void 0:r.form[0]):o.isBundle&&await this.getCartByUser({lang:e})}catch(s){n({statusCode:400,message:s.message})}},async updateCartShipping(t){try{const e=i(),{token:r}=g(e),s=await h.postRequest(d.api.updateCartShipping,t,r.value);return(s==null?void 0:s.status)===200&&(this.cartProducts=s.data),s}catch(e){n({statusCode:400,message:e.message})}}},B=f("cart",{state:q,actions:k});export{B as u};
