import{a1 as h,A as m,p as c}from"./2JCroH3R.js";const a="auth_token",u=10080*60;function k(t){const o=document.cookie.match(new RegExp("(^| )"+t+"=([^;]+)"));return o?decodeURIComponent(o[2]):null}function d(t,o,n){document.cookie=`${t}=${encodeURIComponent(o)}; path=/; max-age=${n}; SameSite=Lax`}function g(t){document.cookie=`${t}=; path=/; max-age=0`}const p=h("user",{state:()=>({token:null,user:null}),getters:{isLoggedIn:t=>!!t.token,isAdmin:t=>!!t.user?.is_admin},actions:{initFromCookie(){const t=k(a);t&&!this.token&&(this.token=t)},async login(t,o){const s=c().public.apiBase,i=await $fetch(`${s}/api/user/login`,{method:"POST",body:{username:t,password:o}}),e=i&&i.code!==void 0?i.data:i;return this.token=e.token,d(a,e.token,u),this.user={id:e.user_id,username:e.username,nickname:e.nickname,region_id:e.region_id,points:e.points,contribution_count:e.contribution_count,is_admin:e.is_admin},this.user},async register(t,o,n){const i=c().public.apiBase,e=await $fetch(`${i}/api/user/register`,{method:"POST",body:{username:t,password:o,phone:n}}),r=e&&e.code!==void 0?e.data:e;return this.token=r.token,d(a,r.token,u),await this.fetchUser(),this.user},async fetchUser(){if(!this.token)return this.user=null,null;try{const o=c().public.apiBase,n=await $fetch(`${o}/api/user/me`,{method:"GET",headers:{Authorization:`Bearer ${this.token}`}}),s=n&&n.code!==void 0?n.data:n;return this.user=s,s}catch{return this.logout(),null}},logout(){this.token=null,this.user=null,g(a),m("/login")}}});export{p as u};