import{p as w,a2 as l,A as $,s as C}from"./2JCroH3R.js";import{u as f}from"./CbPNayBJ.js";import{u as v}from"./DrJDeGiv.js";function g(s){if(s&&typeof s=="object"&&"code"in s){const t=s;if(t.code!==0)throw l({statusCode:t.code,message:t.message||"Request failed"});return t.data??s}return s}function _(){const t=w().public.apiBase;function i(u){const o=document.cookie.match(new RegExp("(^| )"+u+"=([^;]+)"));return o?decodeURIComponent(o[2]):null}function r(u){const o={"Content-Type":"application/json",...u},a=i("auth_token");return a&&(o.Authorization=`Bearer ${a}`),o}async function d(u,o){const a=o?"?"+new URLSearchParams(Object.entries(o).filter(([,c])=>c!=null).map(([c,k])=>[c,String(k)])).toString():"",{data:e,error:n}=await f(`${t}${u}${a}`,{method:"GET",headers:r()},"$XyJlI8LBpU");if(n.value)throw l({statusCode:n.value.statusCode||500,message:n.value.message||"Request failed"});return g(e.value)}async function m(u,o){const{data:a,error:e}=await f(`${t}${u}`,{method:"POST",headers:r(),body:o},"$cdjleazKTg");if(e.value)throw l({statusCode:e.value.statusCode||500,message:e.value.message||"Request failed"});return g(a.value)}async function p(u,o){const{data:a,error:e}=await f(`${t}${u}`,{method:"PUT",headers:r(),body:o},"$TVPTgRlWCT");if(e.value)throw l({statusCode:e.value.statusCode||500,message:e.value.message||"Request failed"});return g(a.value)}async function h(u){const{data:o,error:a}=await f(`${t}${u}`,{method:"DELETE",headers:r()},"$OLMZiXqsek");if(a.value)throw l({statusCode:a.value.statusCode||500,message:a.value.message||"Request failed"});return g(o.value)}return{get:d,post:m,put:p,del:h}}function T(){const s=_(),t=v("auth_user",()=>null),i=v("auth_token",()=>null),r=C(()=>!!i.value);function d(e){i.value=e;{const c=window.location.protocol==="https:"?"; Secure":"";document.cookie=`auth_token=${encodeURIComponent(e)}; path=/; max-age=${10080*60}; SameSite=Lax${c}`,localStorage.setItem("auth_token",e)}}function m(){i.value=null,t.value=null,document.cookie="auth_token=; path=/; max-age=0",localStorage.removeItem("auth_token")}async function p(e){const n=await s.post("/api/user/login",{username:e.username,password:e.password});return d(n.token),t.value={id:n.user_id,username:n.username,nickname:n.nickname,region_id:n.region_id,points:n.points,contribution_count:n.contribution_count},t.value}async function h(e){const n=await s.post("/api/user/register",{username:e.username,password:e.password,phone:e.phone,invite_code:e.invite_code||void 0});return d(n.token),await o(),t.value}function u(){m(),$("/login")}async function o(){if(!i.value)return t.value=null,null;try{const e=await s.get("/api/user/me");return t.value=e,e}catch{return m(),null}}function a(){if(!i.value){const e=localStorage.getItem("auth_token");if(e){i.value=e;return}const n=document.cookie.match(/(^| )auth_token=([^;]+)/);n&&(i.value=decodeURIComponent(n[2]))}}return{user:t,token:i,isLoggedIn:r,login:p,register:h,logout:u,fetchUser:o,initAuth:a}}export{_ as a,T as u};