import{a as l}from"./vendor.d12b5734.js";import{u as n,v as m}from"./main.465728e1.js";const u=e=>{const t=p(),a=n();if(!e.response)a.showNotification({type:"error",message:"Please check your internet connection or wait until servers are back online."});else if(e.response.data&&(e.response.statusText==="Unauthorized"||e.response.data===" Unauthorized.")){const s=e.response.data.message?e.response.data.message:"Unauthorized";i(s),t.logout()}else if(e.response.data.errors){const s=JSON.parse(JSON.stringify(e.response.data.errors));for(const o in s)d(s[o][0])}else e.response.data.error?d(e.response.data.error):d(e.response.data.message)},d=e=>{switch(e){case"These credentials do not match our records.":i("errors.login_invalid_credentials");break;case"The email has already been taken.":i("validation.email_already_taken");break;case"invalid_credentials":i("errors.invalid_credentials");break;case"Email could not be sent to this email address.":i("errors.email_could_not_be_sent");break;case"not_allowed":i("errors.not_allowed");break;default:i(e,!1);break}},i=(e,t=!0)=>{const{global:a}=window.i18n;n().showNotification({type:"error",message:t?a.t(e):e})},{defineStore:f}=window.pinia,{global:r}=window.i18n,p=f({id:"customerAuth",state:()=>({loginData:{email:"",password:"",device_name:"xyz",company:""}}),actions:{login(e){const t=n(!0);return new Promise((a,s)=>{l.get("/sanctum/csrf-cookie").then(o=>{o&&l.post(`/${e.company}/customer/login`,e).then(c=>{t.showNotification({type:"success",message:r.tm("general.login_successfully")}),a(c),setTimeout(()=>{this.loginData.email="",this.loginData.password=""},1e3)}).catch(c=>{u(c),s(c)})})})},forgotPassword(e){const t=n(!0);return new Promise((a,s)=>{l.post(`/api/v1/${e.company}/customer/auth/password/email`,e).then(o=>{o.data&&t.showNotification({type:"success",message:r.tm("general.send_mail_successfully")}),a(o)}).catch(o=>{o.response&&o.response.status===403?t.showNotification({type:"error",message:r.tm("errors.email_could_not_be_sent")}):u(o),s(o)})})},resetPassword(e,t){return new Promise((a,s)=>{l.post(`/api/v1/${t}/customer/auth/reset/password`,e).then(o=>{o.data&&n(!0).showNotification({type:"success",message:r.tm("login.password_reset_successfully")}),a(o)}).catch(o=>{o.response&&o.response.status===403&&notificationStore.showNotification({type:"error",message:r.tm("validation.email_incorrect")}),s(o)})})},logout(e){return new Promise((t,a)=>{l.post(`/${e}/customer/logout`).then(s=>{n().showNotification({type:"success",message:r.tm("general.logged_out_successfully")}),m.push({name:"customer.login"}),t(s)}).catch(s=>{u(s),a(s)})})}}});export{u as h,p as u};
