import type { UserRole } from "@/lib/types";

const ROLE_PRIORITY: UserRole[] = ["owner", "leader", "employee"];

function toUserRole(value: string | null | undefined): UserRole | null {
  if (value === "owner" || value === "leader" || value === "employee") {
    return value;
  }

  return null;
}

export function resolveHighestUserRole(values: Array<string | null | undefined>): UserRole {
  const normalized = new Set(values.map(toUserRole).filter((value): value is UserRole => value !== null));

  for (const role of ROLE_PRIORITY) {
    if (normalized.has(role)) {
      return role;
    }
  }

  return "employee";
}
