import { type ComponentType } from "react";
import {
  Clock,
  Cloud,
  Scale,
  Zap,
  CheckSquare,
  PenLine,
  Pencil,
  Sparkles,
  Waves,
  Globe,
  CloudSun,
  Mic,
  SlidersHorizontal,
  Film,
  Palette,
  Gamepad2,
  Music,
  Building2,
  Home,
  Tag,
  Github,
  Bug,
  Bird,
  PawPrint,
  Eye,
  Users,
  Gem,
  Lightbulb,
  MessageCircle,
  DollarSign,
  CreditCard,
  FileText,
  Calendar,
  BookUser,
  TrendingUp,
  BarChart3,
  ClipboardList,
  Pin,
  MapPin,
  BookOpen,
  Library,
  ScrollText,
  FileEdit,
  Phone,
  Megaphone,
  Inbox,
  Package,
  Mail,
  Newspaper,
  Smartphone,
  Camera,
  Tv,
  ArrowLeftRight,
  Volume2,
  Search,
  Lock,
  KeyRound,
  Wrench,
  Microscope,
  Network,
  Image,
  Database,
  AudioLines,
  Map,
  Rocket,
  Hammer,
  Bike,
  Bot,
  Egg,
  Brain,
  Puzzle,
  FlaskConical,
  Compass,
  Magnet,
  Layers,
  Eraser,
  Receipt,
  CircleDot,
  Cherry,
  XCircle,
  AlertTriangle,
  Info,
  Target,
  Shield,
  Laptop,
  Key,
  Flame,
  Heart,
  Star,
  Siren,
  Bell,
  Clipboard,
  Flag,
  type LucideProps,
} from "lucide-react";

export type { LucideProps };

export const EMOJI_ICON_MAP: Record<string, ComponentType<LucideProps>> = {
  "⏰": Clock,
  "☁️": Cloud,
  "♊️": Sparkles,
  "⚖️": Scale,
  "⚡": Zap,
  "✅": CheckSquare,
  "✍️": PenLine,
  "✏️": Pencil,
  "✨": Sparkles,
  "🌊": Waves,
  "🌐": Globe,
  "🌤️": CloudSun,
  "🎙️": Mic,
  "🎛️": SlidersHorizontal,
  "🎞️": Film,
  "🎨": Palette,
  "🎮": Gamepad2,
  "🎯": Target,
  "🎵": Music,
  "🏗️": Building2,
  "🏠": Home,
  "🏷️": Tag,
  "🐙": Github,
  "🐛": Bug,
  "🐦": Bird,
  "🐻": PawPrint,
  "👀": Eye,
  "👁️": Eye,
  "👨‍👩‍👧‍👦": Users,
  "💎": Gem,
  "💡": Lightbulb,
  "💬": MessageCircle,
  "💰": DollarSign,
  "💳": CreditCard,
  "💻": Laptop,
  "📄": FileText,
  "📅": Calendar,
  "📇": BookUser,
  "📈": TrendingUp,
  "📊": BarChart3,
  "📋": ClipboardList,
  "📌": Pin,
  "📍": MapPin,
  "📖": BookOpen,
  "📚": Library,
  "📜": ScrollText,
  "📝": FileEdit,
  "📞": Phone,
  "📣": Megaphone,
  "📥": Inbox,
  "📦": Package,
  "📧": Mail,
  "📨": Mail,
  "📰": Newspaper,
  "📱": Smartphone,
  "📸": Camera,
  "📺": Tv,
  "🔄": ArrowLeftRight,
  "🔊": Volume2,
  "🔍": Search,
  "🔎": Search,
  "🔐": KeyRound,
  "🔑": Key,
  "🔒": Lock,
  "🔥": Flame,
  "🔧": Wrench,
  "🔬": Microscope,
  "🕸️": Network,
  "🖼️": Image,
  "🗄️": Database,
  "🗣️": AudioLines,
  "🗺️": Map,
  "🚀": Rocket,
  "🚨": Siren,
  "🛠️": Hammer,
  "🛡️": Shield,
  "🛵": Bike,
  "🤖": Bot,
  "🥡": Egg,
  "🧠": Brain,
  "🧩": Puzzle,
  "🧪": FlaskConical,
  "🧭": Compass,
  "🧲": Magnet,
  "🧵": Layers,
  "🧹": Eraser,
  "🧾": Receipt,
  "🧿": CircleDot,
  "🫐": Cherry,
  "❌": XCircle,
  "❤️": Heart,
  "⭐": Star,
  "⚠️": AlertTriangle,
  "⚠": AlertTriangle,
  ℹ️: Info,
  ℹ: Info,
  "🔔": Bell,
  "📎": Clipboard,
  "🏁": Flag,
};

/**
 * Variation-selector-tolerant emoji lookup.
 * Handles mismatches where the map key has a VS16 (U+FE0F) but the input
 * doesn't, or vice versa, by building a normalized index at module load.
 * Strips skin-tone modifiers (U+1F3FB–U+1F3FF) for lookup.
 */
const VS_REGEX = /[\uFE0E\uFE0F]/g;
const SKIN_TONE_REGEX = /[\u{1F3FB}-\u{1F3FF}]/gu;
const NORMALIZED: Record<string, ComponentType<LucideProps>> = {};
for (const [key, value] of Object.entries(EMOJI_ICON_MAP)) {
  NORMALIZED[key] = value;
  const stripped = key.replace(VS_REGEX, "");
  if (stripped !== key) NORMALIZED[stripped] = value;
}

/**
 * Resolve emoji to a Lucide icon. Always returns a Lucide component;
 * uses Bot as fallback when no mapping exists (never shows raw emoji).
 */
export function getEmojiIcon(emoji: string): ComponentType<LucideProps> {
  const normalized = emoji.replace(VS_REGEX, "").replace(SKIN_TONE_REGEX, "");
  return (
    NORMALIZED[emoji] ||
    NORMALIZED[normalized] ||
    NORMALIZED[emoji.replace(VS_REGEX, "")] ||
    Bot
  );
}
