/**
 * @see https://umijs.org/docs/max/access#access
 * 动态权限控制（精简版，仅保留登录态相关）
 */

// 扩展 CurrentUser 类型以包含 roles
interface CurrentUserWithRoles extends API.CurrentUser {
  roles?: string[];
}

export default function access(
  initialState: { currentUser?: CurrentUserWithRoles } | undefined,
) {
  const { currentUser } = initialState ?? {};
  const userRoles = currentUser?.roles || [];
  const isAdmin = userRoles.includes('admin') || currentUser?.access === 'admin';

  return {
    // 保持向后兼容的管理员判断
    canAdmin: isAdmin,
    userRoles,
  };
}
