import type { Member, Role } from "../domain/types";

type TFn = (key: string) => string;

/** 角色標籤（可接 i18n） */
export function roleLabel(role: Role | string, t?: TFn): string {
  if (role === "admin") return t ? t("role.admin") : "管理員";
  if (role === "member") return t ? t("role.member") : "一般會員";
  return String(role);
}

/**
 * 主要顯示角色：有 admin 就顯示管理員，否則一般會員。
 * 回傳列表供多角色展示。
 */
export function memberRoleSummary(
  member: Pick<Member, "roles"> | null | undefined,
  t?: TFn,
): {
  primary: "admin" | "member";
  primaryLabel: string;
  labels: string[];
  isAdmin: boolean;
} {
  const roles = member?.roles?.length ? member.roles : (["member"] as Role[]);
  const isAdmin = roles.includes("admin");
  const primary = isAdmin ? "admin" : "member";
  const labels = [...new Set(roles.map((r) => roleLabel(r, t)))];
  return {
    primary,
    primaryLabel: isAdmin
      ? t
        ? t("role.admin")
        : "管理員"
      : t
        ? t("role.member")
        : "一般會員",
    labels,
    isAdmin,
  };
}
