import { Message } from '@arco-design/web-react';
import { logout } from '@/api/user';
import { clearToken } from '@/utils/auth';

interface UseUserReturnType {
  logoutHooks: () => Promise<void>;
}

export default function useUser(): UseUserReturnType {

  const logoutHooks = async () => {
    try {
      // 调用后端登出接口
      await logout();
    } catch (error) {
      // 即使后端登出失败，也要清除本地状态
      console.error('Logout error:', error);
    } finally {
      // 清除本地存储的用户状态
      localStorage.removeItem('userStatus');
      clearToken()

      // 显示成功消息
      Message.success('退出成功');

      // 重定向到登录页面
      const { protocol, host } = window.location;
      window.location.href = `${protocol}//${host}/login`;
    }
  };

  return {
    logoutHooks
  };
}