import{j as a}from"./jsx-runtime.D_zvdyIk.js";import{r as t}from"./index.DIfZGIpv.js";const d=t.createContext({user:null,loading:!0,login:async()=>!1,logout:()=>{},token:null});function k(){return t.useContext(d)}function A({children:r}){const[s,n]=t.useState(null),[m,i]=t.useState(null),[f,u]=t.useState(!0);t.useEffect(()=>{const o=localStorage.getItem("admin_token");o?(i(o),fetch("/api/admin/auth/session",{headers:{Authorization:`Bearer ${o}`}}).then(e=>e.ok?e.json():null).then(e=>{e?.authenticated?n(e.user):localStorage.removeItem("admin_token")}).catch(()=>localStorage.removeItem("admin_token")).finally(()=>u(!1))):u(!1)},[]);const h=async(o,e)=>{const c=await fetch("/api/admin/auth/login",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:o,password:e})});if(!c.ok)return!1;const l=await c.json();return i(l.token),n(l.user),localStorage.setItem("admin_token",l.token),!0},g=()=>{n(null),i(null),localStorage.removeItem("admin_token"),document.cookie="admin_token=; Path=/; Max-Age=0"};return a.jsx(d.Provider,{value:{user:s,loading:f,login:h,logout:g,token:m},children:r})}function j({children:r}){const{user:s,loading:n}=k();return n?a.jsx("div",{className:"flex items-center justify-center min-h-screen bg-[#0A0A0A]",children:a.jsx("div",{className:"w-8 h-8 border-2 border-[#00FF6A] border-t-transparent rounded-full animate-spin"})}):s?a.jsx(a.Fragment,{children:r}):(window.location.href="/admin/login",null)}export{A,j as a,k as u};
