/**
 * Dropdown 项图标语义解析
 *
 * showIcon 开启时，未显式传 icon / iconName 的项按 label（及 desc）从 B 端 Icon 库匹配。
 * 显式 iconName 始终优先；danger 项默认删除类图标。
 */

import { ICON_MAP } from './icons/icon-data';

/** @type {Array<{ keywords?: string[], icon: string, test?: (text: string, item: object) => boolean }>} */
const SEMANTIC_ICON_RULES = [
  { test: (_text, item) => Boolean(item?.danger), icon: 'trash-01-stroked' },
  { keywords: ['删除', '移除', 'delete', 'remove', '清空', 'destroy'], icon: 'trash-01-stroked' },
  { keywords: ['完结并归档', '完结', 'folder-check'], icon: 'folder-check-stroked' },
  { keywords: ['归档', 'archive'], icon: 'archive-stroked' },
  { keywords: ['抖音社区', '社区业务', '社区治理', '体验与服务'], icon: 'home-smile-stroked' },
  { keywords: ['首页', '主页', '平台首页', 'home'], icon: 'home-02-stroked' },
  { keywords: ['转交二线', '转交', '二线复核', '复核升级', 'handoff'], icon: 'headphones-01-stroked' },
  { keywords: ['抖音电商', '电商', 'shopping'], icon: 'shopping-bag-01-stroked' },
  { keywords: ['生活服务', '预约到店', '团购', '核销', '切换业务', '业务切换', '切换店铺', '店铺', '门店', '商家', 'building'], icon: 'building-02-stroked' },
  { keywords: ['设置', '配置', '偏好', 'settings'], icon: 'settings-02-stroked' },
  { keywords: ['通知', '消息', '提醒', 'bell'], icon: 'bell-03-stroked' },
  { keywords: ['搜索', '查找', '检索', 'search'], icon: 'search-md-stroked' },
  { keywords: ['编辑', '修改', 'rename', 'edit'], icon: 'edit-02-stroked' },
  { keywords: ['复制', 'copy'], icon: 'copy-01-stroked' },
  { keywords: ['下载', '导出文件', 'download'], icon: 'download-01-stroked' },
  { keywords: ['上传', 'import', 'upload'], icon: 'upload-01-stroked' },
  { keywords: ['导出', '分享链接', 'export', 'share'], icon: 'share-01-stroked' },
  { keywords: ['刷新', 'reload', 'refresh', '重新加载'], icon: 'refresh-cw-01-stroked' },
  { keywords: ['添加', '新建', '创建', '新增', 'create', 'add'], icon: 'plus-stroked' },
  { keywords: ['查看', '详情', '预览', 'view', 'detail'], icon: 'eye-stroked' },
  { keywords: ['日志', '记录', 'log', 'history'], icon: 'file-02-stroked' },
  { keywords: ['帮助', 'help', 'faq'], icon: 'help-circle-stroked' },
  { keywords: ['退出', '登出', '注销', 'logout', 'sign out'], icon: 'log-out-01-stroked' },
  { keywords: ['用户', '账号', '成员', 'profile', 'user'], icon: 'user-01-stroked' },
  { keywords: ['agent', '机器人', '智能体', 'bot'], icon: 'bot-01-stroked' },
  { keywords: ['@', '提及', 'mention', 'at-sign'], icon: 'at-sign-stroked' },
  { keywords: ['指令', '命令', 'terminal', 'command'], icon: 'terminal-stroked' },
  { keywords: ['更多', 'more', 'dots'], icon: 'dots-horizontal-stroked' },
  { keywords: ['收藏', 'favorite', 'bookmark'], icon: 'bookmark-stroked' },
  { keywords: ['链接', 'link'], icon: 'link-01-stroked' },
  { keywords: ['筛选', 'filter'], icon: 'filter-lines-stroked' },
  { keywords: ['排序', 'sort'], icon: 'switch-vertical-01-stroked' },
  { keywords: ['打印', 'print'], icon: 'printer-stroked' },
  { keywords: ['邮件', 'email', 'mail'], icon: 'mail-01-stroked' },
  { keywords: ['电话', '外呼', 'phone', 'call'], icon: 'phone-stroked' },
  { keywords: ['视频', 'video'], icon: 'video-recorder-stroked' },
  { keywords: ['图片', 'image', 'photo'], icon: 'image-01-stroked' },
  { keywords: ['文件夹', '目录', 'folder'], icon: 'folder-stroked' },
  { keywords: ['知识', '文档', 'document', 'book'], icon: 'book-open-01-stroked' },
  { keywords: ['策略', '规则', 'rule', 'policy'], icon: 'settings-02-stroked' },
  { keywords: ['工单', 'ticket'], icon: 'ticket-01-stroked' },
  { keywords: ['客服', '会话', 'conversation', 'chat'], icon: 'message-chat-square-stroked' },
  { keywords: ['转人工', '人工', 'handoff'], icon: 'headphones-01-stroked' },
  { keywords: ['暂停', '停止', 'pause', 'stop'], icon: 'pause-circle-stroked' },
  { keywords: ['播放', '开始', 'play', 'start'], icon: 'play-circle-stroked' },
  { keywords: ['完成', '成功', 'done', 'success'], icon: 'check-circle-stroked' },
  { keywords: ['警告', '风险', 'warning', 'alert'], icon: 'alert-circle-stroked' },
  { keywords: ['锁定', 'lock'], icon: 'lock-01-stroked' },
  { keywords: ['解锁', 'unlock'], icon: 'lock-unlocked-01-stroked' },
  { keywords: ['置顶', 'pin'], icon: 'pin-01-stroked' },
];

function isValidIcon(name) {
  return Boolean(name && ICON_MAP[name]);
}

function collectItemText(item) {
  return [item?.label, item?.desc, item?.value, item?.id]
    .filter((part) => part !== null && part !== undefined && String(part).trim() !== '')
    .map((part) => String(part).trim().toLowerCase())
    .join(' ');
}

/**
 * 按项文案语义解析 Icon 库名称；已有 icon / iconName 时不覆盖。
 * @returns {string|undefined}
 */
export function resolveDropdownItemIcon(item) {
  if (!item || item.icon || item.iconName) return undefined;

  const text = collectItemText(item);
  if (!text) return undefined;

  for (const rule of SEMANTIC_ICON_RULES) {
    if (typeof rule.test === 'function' && rule.test(text, item) && isValidIcon(rule.icon)) {
      return rule.icon;
    }
    if (rule.keywords?.some((keyword) => text.includes(String(keyword).toLowerCase())) && isValidIcon(rule.icon)) {
      return rule.icon;
    }
  }

  return undefined;
}

/** 是否应显示图标列（显式图标或语义可解析） */
export function itemHasDropdownIconSignal(item) {
  return Boolean(item?.icon || item?.iconName || resolveDropdownItemIcon(item));
}

/** showIcon 开启时为缺省项补全 iconName */
export function enrichDropdownItems(items) {
  if (!Array.isArray(items) || items.length === 0) return items;

  return items.map((item) => {
    if (!item || item.icon || item.iconName) return item;
    const iconName = resolveDropdownItemIcon(item);
    return iconName ? { ...item, iconName } : item;
  });
}
