---
import type { CollectionEntry } from "astro:content";
import clsx from "clsx";
import type { Lang } from "@i18n";
import { getUsernameForLang } from "@utils/get_username_for_lang";
import { isUserSelf } from "@utils/is_user_self";

type Props = {
  lang: Lang;
  user: CollectionEntry<"users">;
  class?: string;
  rel?: string;
};

const { user, lang, class: className, rel } = Astro.props;
const username = getUsernameForLang(user, lang);
const link = user.data.preferredLink ? user.data.links[user.data.preferredLink]!.link : null;
const isSelf = isUserSelf(user);
---

{
  link ? (
    <a
      rel={clsx(rel, isSelf ? "me" : "nofollow")}
      href={link}
      class:list={[className, "h-card u-url p-name text-link underline"]}
      target="_blank"
    >
      {username}
    </a>
  ) : (
    <span class:list={[className, "h-card p-name"]}>{username}</span>
  )
}
