import { icons, type LucideIcon } from 'lucide-react';

/**
 * 获取所有 Lucide React 图标列表
 * @returns 图标名称和组件的数组
 */
export const getAllLucideIcons = () => {
    return Object.entries(icons).map(([name, component]) => ({
        name,
        component: component as LucideIcon
    }));
};

/**
 * 根据名称获取 Lucide 图标组件
 * @param iconName 图标名称
 * @returns 图标组件或 null
 */
export const getLucideIcon = (iconName: string): LucideIcon | null => {
    return (icons[iconName as keyof typeof icons] as LucideIcon) || null;
};

/**
 * 图标分类配置
 * 每个分类包含名称、代表性图标和图标列表
 */
export interface IconCategory {
    name: string;
    icon: string;
    icons: string[];
}

/**
 * 常用图标分类
 */
export const ICON_CATEGORIES: Record<string, IconCategory> = {
    'common': {
        name: '常用',
        icon: 'Sparkles',
        icons: [
            'FolderKanban', 'Workflow', 'Settings', 'Users', 'Calendar',
            'Clock', 'Database', 'Server', 'Cloud', 'Package'
        ]
    },
    'file': {
        name: '文件与文档',
        icon: 'FileText',
        icons: [
            'File', 'FileText', 'Folder', 'FolderOpen', 'Files',
            'FileJson', 'FileCode', 'FilePlus', 'FileEdit'
        ]
    },
    'user': {
        name: '用户与团队',
        icon: 'Users',
        icons: [
            'User', 'Users', 'UserPlus', 'UserCheck', 'UserCog',
            'Shield', 'ShieldCheck', 'Contact'
        ]
    },
    'code': {
        name: '开发与代码',
        icon: 'Code',
        icons: [
            'Code', 'Terminal', 'GitBranch', 'Github', 'GitFork',
            'GitCommit', 'GitPullRequest', 'Bug', 'Braces'
        ]
    },
    'data': {
        name: '数据与存储',
        icon: 'Database',
        icons: [
            'Database', 'HardDrive', 'Save', 'Download', 'Upload',
            'Archive', 'Package', 'Inbox', 'FolderSync'
        ]
    },
    'network': {
        name: '网络与服务',
        icon: 'Server',
        icons: [
            'Server', 'Cloud', 'CloudDownload', 'CloudUpload', 'Globe',
            'Wifi', 'Zap', 'Activity', 'TrendingUp'
        ]
    },
    'time': {
        name: '时间与日历',
        icon: 'Calendar',
        icons: [
            'Calendar', 'CalendarDays', 'Clock', 'Timer', 'AlarmClock',
            'History', 'CalendarCheck', 'CalendarPlus'
        ]
    },
    'control': {
        name: '操作与控制',
        icon: 'Sliders',
        icons: [
            'Play', 'Pause', 'Stop', 'RotateCw', 'RefreshCw',
            'Power', 'Settings', 'Sliders', 'ToggleLeft'
        ]
    },
    'notification': {
        name: '通知与提醒',
        icon: 'Bell',
        icons: [
            'Bell', 'BellRing', 'MessageSquare', 'Mail', 'Inbox',
            'AlertCircle', 'AlertTriangle', 'Info', 'CheckCircle'
        ]
    },
    'edit': {
        name: '编辑与格式',
        icon: 'Edit',
        icons: [
            'Edit', 'Edit2', 'Edit3', 'Pencil', 'Trash2',
            'Copy', 'Clipboard', 'Scissors', 'FileEdit'
        ]
    },
    'navigation': {
        name: '导航与箭头',
        icon: 'Navigation',
        icons: [
            'ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown', 'ChevronRight',
            'ChevronLeft', 'ChevronUp', 'ChevronDown', 'Home', 'Menu'
        ]
    },
    'status': {
        name: '状态与标记',
        icon: 'CheckCircle2',
        icons: [
            'Check', 'CheckCircle', 'CheckCircle2', 'X', 'XCircle',
            'AlertCircle', 'AlertTriangle', 'HelpCircle', 'Plus', 'Minus'
        ]
    }
};

/**
 * 搜索图标
 * @param searchTerm 搜索关键词
 * @param category 分类（可选）
 * @returns 匹配的图标列表
 */
export const searchLucideIcons = (searchTerm: string, category?: string) => {
    const allIcons = getAllLucideIcons();
    
    let filtered = allIcons;
    
    // 按分类过滤
    if (category && category !== 'all') {
        const categoryData = ICON_CATEGORIES[category];
        if (categoryData) {
            filtered = allIcons.filter(icon => categoryData.icons.includes(icon.name));
        }
    }
    
    // 按搜索词过滤
    if (searchTerm) {
        const search = searchTerm.toLowerCase();
        filtered = filtered.filter(icon => 
            icon.name.toLowerCase().includes(search)
        );
    }
    
    return filtered;
};

