import{t as e}from"./http-BrugN6o-.js";import{t}from"./notification-BM55Quw3.js";import{Nn as n}from"./main-CVDkKXbu.js";var r=e=>{let n=c(),r=t();if(!e.response)r.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.`))a(e.response.data.message?e.response.data.message:`Unauthorized`),n.logout();else if(e.response.data.errors){let t=JSON.parse(JSON.stringify(e.response.data.errors));for(let e in t)i(t[e][0])}else e.response.data.error?i(e.response.data.error):i(e.response.data.message)},i=e=>{switch(e){case`These credentials do not match our records.`:a(`errors.login_invalid_credentials`);break;case`The email has already been taken.`:a(`validation.email_already_taken`);break;case`invalid_credentials`:a(`errors.invalid_credentials`);break;case`Email could not be sent to this email address.`:a(`errors.email_could_not_be_sent`);break;case`not_allowed`:a(`errors.not_allowed`);break;default:a(e,!1);break}},a=(e,n=!0)=>{let{global:r}=window.i18n;t().showNotification({type:`error`,message:n?r.t(e):e})},{defineStore:o}=window.pinia,{global:s}=window.i18n,c=o(`customerAuth`,{state:()=>({loginData:{email:``,password:``,device_name:`xyz`,company:``}}),actions:{login(n){let i=t(!0);return new Promise((t,a)=>{e.get(`/sanctum/csrf-cookie`).then(o=>{o&&e.post(`/${n.company}/customer/login`,n).then(e=>{i.showNotification({type:`success`,message:s.tm(`general.login_successfully`)}),t(e),setTimeout(()=>{this.loginData.email=``,this.loginData.password=``},1e3)}).catch(e=>{r(e),a(e)})})})},forgotPassword(n){let i=t(!0);return new Promise((t,a)=>{e.post(`/api/v1/${n.company}/customer/auth/password/email`,n).then(e=>{e.data&&i.showNotification({type:`success`,message:s.tm(`general.send_mail_successfully`)}),t(e)}).catch(e=>{e.response&&e.response.status===403?i.showNotification({type:`error`,message:s.tm(`errors.email_could_not_be_sent`)}):r(e),a(e)})})},resetPassword(n,r){return new Promise((i,a)=>{e.post(`/api/v1/${r}/customer/auth/reset/password`,n).then(e=>{e.data&&t(!0).showNotification({type:`success`,message:s.tm(`login.password_reset_successfully`)}),i(e)}).catch(e=>{e.response&&e.response.status===403&&notificationStore.showNotification({type:`error`,message:s.tm(`validation.email_incorrect`)}),a(e)})})},logout(i){return new Promise((a,o)=>{e.post(`/${i}/customer/logout`).then(e=>{t().showNotification({type:`success`,message:s.tm(`general.logged_out_successfully`)}),n.push({name:`customer.login`}),a(e)}).catch(e=>{r(e),o(e)})})}}});export{r as n,c as t};