import { createHash } from 'node:crypto';

function getGravatarUrl(email: string): string {
  const hash = createHash('md5').update(email.trim().toLowerCase()).digest('hex');
  return `https://www.gravatar.com/avatar/${hash}?d=identicon`;
}

export function resolveAvatarUrl(input: {
  email: string;
  image?: string | null;
}): string {
  if (input.image) {
    return input.image;
  }

  return getGravatarUrl(input.email);
}
