export function BadgeCell({ badges = [] }) {
  // Handle both array and object formats for backward compatibility
  let badgesArray = [];
  
  if (Array.isArray(badges)) {
    badgesArray = badges;
  } else if (badges && typeof badges === 'object') {
    // Priority order: 🚀⚡🔥💎💰⭐✔🟢/🔴
    // Extract badges in priority order
    const priorityOrder = ['🚀', '⚡', '🔥', '💎', '💰', '⭐', '✔', '🟢', '🔴'];
    
    // Extract rocket first (highest priority)
    if (badges.rocket) {
      const rocketCount = (badges.rocket.match(/🚀/g) || []).length;
      for (let i = 0; i < rocketCount; i++) {
        badgesArray.push('🚀');
      }
    }
    
    // Extract flash (⚡)
    if (badges.flash) badgesArray.push(badges.flash);
    
    // Extract from more string in priority order
    if (badges.more) {
      const emojiRegex = /[\u{1F300}-\u{1F9FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}\u{1F600}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{2700}-\u{27BF}\u{1F900}-\u{1F9FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}✔]/gu;
      const moreBadges = badges.more.match(emojiRegex) || [];
      
      // Sort by priority
      const sortedMore = moreBadges.sort((a, b) => {
        const aIndex = priorityOrder.indexOf(a);
        const bIndex = priorityOrder.indexOf(b);
        if (aIndex === -1 && bIndex === -1) return 0;
        if (aIndex === -1) return 1;
        if (bIndex === -1) return -1;
        return aIndex - bIndex;
      });
      
      badgesArray.push(...sortedMore);
    }
    
    // Extract round badge last (🟢/🔴)
    if (badges.round) badgesArray.push(badges.round);
  }
  
  if (!badgesArray || badgesArray.length === 0) return null;

  return (
    <div className="flex items-center gap-1 flex-wrap">
      {badgesArray.map((badge, index) => (
        <span
          key={index}
          className="text-base cursor-help"
          title={getBadgeLabel(badge)}
        >
          {badge}
        </span>
      ))}
    </div>
  );
}

function getBadgeLabel(badge) {
  const labels = {
    "🟢": "Bullish Flow",
    "🔴": "Bearish Flow",
    "💎": "ITM Dominance (Real Money)",
    "⭐": "OTM Flow (Speculation)",
    "💰": "High OI Magnet",
    "⚡": "Aggressive Sweep",
    "🚀": "Highest Conviction",
    "🔥": "$1M+ Premium",
    "✔": "Vol > OI (New Positions)"
  };
  return labels[badge] || badge;
}

