interface ResolveNavigationUserInfoOptions<T> {
  cachedUserInfo: null | T;
  fetchUserInfo: () => Promise<T>;
  getAccessToken: () => null | string;
}

/**
 * 解析路由守卫所需的用户信息，并识别已被拦截器清理的失效登录态。
 */
async function resolveNavigationUserInfo<T>(
  options: ResolveNavigationUserInfoOptions<T>,
): Promise<null | T> {
  if (options.cachedUserInfo) {
    return options.cachedUserInfo;
  }
  try {
    return await options.fetchUserInfo();
  } catch (error) {
    if (!options.getAccessToken()) {
      return null;
    }
    throw error;
  }
}

export { resolveNavigationUserInfo };
