import type { CollectionEntry } from "astro:content";
import { DEFAULT_LANG, type Lang } from "@i18n";

export function getUsernameForLang(user: CollectionEntry<"users">, lang: Lang): string {
  const { name } = user.data;
  if (typeof name === "object") {
    if (name[lang]) {
      return name[lang];
    }
    throw new Error(`No "${lang}" translation for user "${user.id}"`);
  }
  if (lang !== DEFAULT_LANG) {
    console.warn(`WARNING: Name "${name}" for user "${user.id}" isn't translated to "${lang}"; using default...`);
  }
  return name;
}
