import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import type { UserInfo } from '@/api/types/auth';

/** 用户状态 */
interface UserState {
  token: string | null;
  userInfo: UserInfo | null;
  setToken: (token: string) => void;
  setUserInfo: (info: UserInfo) => void;
  logout: () => void;
}

/** 用户存储 */
export const useUserStore = create<UserState>()(
  persist(
    set => ({
      token: null,
      userInfo: null,
      setToken: token => set({ token }),
      setUserInfo: info => set({ userInfo: info }),
      logout: () => set({ token: null, userInfo: null }),
    }),
    {
      name: 'user-store',
      partialize: state => ({ token: state.token }),
    },
  ),
);
