import liuDelinAvatar from './components/team-avatar-assets/liu-delin.png';
import liSiruAvatar from './components/team-avatar-assets/li-siru.png';
import duanRanAvatar from './components/team-avatar-assets/duan-ran.png';
import chengchengMurphyAvatar from './components/team-avatar-assets/chengcheng-murphy.png';

export const TEAM_MEMBERS = [
  { id: 'li-siru', name: '李思儒', avatarSrc: liSiruAvatar },
  { id: 'chengcheng-murphy', name: '程程murphy', avatarSrc: chengchengMurphyAvatar },
  { id: 'liu-delin', name: '刘德琳', avatarSrc: liuDelinAvatar },
  { id: 'duan-ran', name: '段然', avatarSrc: duanRanAvatar },
];

export const DEFAULT_TEAM_MEMBER = TEAM_MEMBERS[0];
export const TEAM_MEMBER_AVATAR_SRCS = TEAM_MEMBERS.map((member) => member.avatarSrc);

export const TEAM_MEMBER_BY_NAME = Object.fromEntries(
  TEAM_MEMBERS.map((member) => [member.name, member]),
);

export function getTeamMemberByIndex(index = 0) {
  if (TEAM_MEMBERS.length === 0) return null;
  const normalizedIndex = Math.abs(Number(index) || 0) % TEAM_MEMBERS.length;
  return TEAM_MEMBERS[normalizedIndex];
}

export function getTeamMemberBySeed(seed = 0) {
  if (typeof seed === 'number') {
    return getTeamMemberByIndex(seed);
  }

  const input = String(seed || '');
  let hash = 0;
  for (let i = 0; i < input.length; i += 1) {
    hash = (hash * 31 + input.charCodeAt(i)) >>> 0;
  }
  return getTeamMemberByIndex(hash);
}

export function getTeamAvatarBySeed(seed = 0) {
  return getTeamMemberBySeed(seed)?.avatarSrc || DEFAULT_TEAM_MEMBER.avatarSrc;
}

export function getRandomTeamMember() {
  return getTeamMemberByIndex(Math.floor(Math.random() * TEAM_MEMBERS.length));
}

export function getRandomTeamAvatarSrc() {
  return getRandomTeamMember()?.avatarSrc || DEFAULT_TEAM_MEMBER.avatarSrc;
}

export function getTeamMemberByName(name) {
  if (!name) return null;
  return TEAM_MEMBER_BY_NAME[name] || null;
}
