import type { RankType, RankItem } from './types'

export const getRankBadgeClass = (rank: number) => {
    if (rank === 1) return 'bg-[#ff5400] text-white'
    if (rank === 2) return 'bg-[#EB6732] text-white'
    if (rank === 3) return 'bg-[#ff8400] text-white'
    return 'bg-gray-500 dark:bg-gray-600 text-white'
}

export const getRankGradientClass = (rank: number, activeType: RankType) => {
    if (rank > 3) return null
    const type = activeType === 'gift' ? 'gift' : activeType === 'rich' ? 'rich' : 'group'
    return `rank-name-gradient-${type}-${rank}`
}

export const getLevelIcon = (level?: number) => {
    if (!level || level < 1 || level > 17) return null
    const levelStr = level.toString().padStart(2, '0')
    return `/images/singer_level/${levelStr}.png`
}

export const getRichLevelIconStyle = (level?: number) => {
    if (!level || level < 1) return null
    const backgroundPositionY = -(level - 1) * 17
    return {
        backgroundImage: 'url(/images/richman.png)',
        backgroundPosition: `0 ${backgroundPositionY}px`,
        backgroundSize: '50px 867px',
        width: '50px',
        height: '17px',
    }
}

export const getRankIcon = (activeType: RankType) => {
    if (activeType === 'gift') return '/images/rank/img_artist.png'
    if (activeType === 'rich') return '/images/rank/img_user.png'
    if (activeType === 'group') return '/images/rank/img_family.png'
    return null
}

export const getLinkUrl = (item: RankItem, activeType: RankType) => {
    if (activeType === 'group' && item.groupId) {
        return `/groups/${item.groupId}`
    }
    if (item.userId) {
        return `/user/${item.userId}`
    }
    return '#'
}

export const renderNameWithEmoji = (name: string) => {
    const emojiRegex = /(\p{Emoji_Presentation}|\p{Emoji}\uFE0F|\p{Emoji_Modifier_Base})/gu
    const parts = name.split(emojiRegex)

    return parts.map((part, index) => {
        if (part && emojiRegex.test(part)) {
            emojiRegex.lastIndex = 0
            return <span key={index} className="emoji">{part}</span>
        }
        return <span key={index}>{part}</span>
    })
}

