import type { ComponentType } from 'react';
import {
  YoutubeFilled,
  InstagramFilled,
  FacebookFilled,
  GlobeFilled,
  VideoFilled,
  MessageFilled,
  TiktokFilled,
  FileTextFilled,
  MegaphoneFilled,
} from './FilledIcons';

type IconComponent = ComponentType<{ size?: number; className?: string }>;

// API icon 필드는 채널 키(naverBlog) 또는 일반명(blog/map/star)으로 올 수 있어 둘 다 매핑.
export const channelIconMap: Record<string, IconComponent> = {
  youtube: YoutubeFilled,
  instagram: InstagramFilled,
  facebook: FacebookFilled,
  tiktok: TiktokFilled,
  naverblog: FileTextFilled,
  naver_blog: FileTextFilled,
  blog: FileTextFilled,
  naverplace: GlobeFilled,
  naver_place: GlobeFilled,
  map: GlobeFilled,
  gangnamunni: MegaphoneFilled,
  gangnam_unni: MegaphoneFilled,
  star: MegaphoneFilled,
  website: GlobeFilled,
  homepage: GlobeFilled,
  globe: GlobeFilled,
  video: VideoFilled,
  messagesquare: MessageFilled,
};

export function getChannelIcon(icon: string | undefined | null): IconComponent {
  if (!icon) return GlobeFilled;
  return channelIconMap[icon.toLowerCase()] ?? GlobeFilled;
}

// icon 필드가 generic('video', 'globe' 등) 으로 와도 채널 라벨로 보정.
// e.g. channel='TikTok', icon='video' → TiktokFilled
export function resolveChannelIcon(channelLabel?: string | null, icon?: string | null): IconComponent {
  if (channelLabel) {
    const lower = channelLabel.toLowerCase();
    if (lower.includes('youtube')) return YoutubeFilled;
    if (lower.includes('instagram')) return InstagramFilled;
    if (lower.includes('facebook')) return FacebookFilled;
    if (lower.includes('tiktok') || lower.includes('틱톡')) return TiktokFilled;
    if (lower.includes('blog') || lower.includes('블로그')) return FileTextFilled;
    if (lower.includes('place') || lower.includes('플레이스')) return GlobeFilled;
    if (lower.includes('gangnam') || lower.includes('강남언니')) return MegaphoneFilled;
    if (lower.includes('website') || lower.includes('홈페이지') || lower.includes('웹사이트')) return GlobeFilled;
  }
  return getChannelIcon(icon ?? undefined);
}
