/**
 * 流程节点右侧面板共用 UI：节点图标资源与说明文案（与 ProcessDesigner 保持一致）
 */

const iconCategories: Record<string, { start: number; end: number }> = {
  审核: { start: 1000, end: 1011 },
  开始: { start: 2000, end: 2016 },
  录入: { start: 3000, end: 3028 },
  确认: { start: 4000, end: 4024 },
  结束: { start: 5000, end: 5018 },
  能量隔离: { start: 6000, end: 6027 },
  解除隔离: { start: 7000, end: 7021 },
};

const iconModules = import.meta.glob('../assets/节点图标/**/*.png', { eager: true, as: 'url' });

export function generateIconPaths(category: string): Array<{ id: string; fileName: string; path: string }> {
  const config = iconCategories[category];
  if (!config) return [];

  const paths: Array<{ id: string; fileName: string; path: string }> = [];
  const allKeys = Object.keys(iconModules);

  if (allKeys.length > 0) {
    for (let i = config.start; i <= config.end; i++) {
      const fileName = `${i}.png`;
      const matchingKey = allKeys.find((k) => {
        const normalizedKey = k.replace(/\\/g, '/').toLowerCase();
        const normalizedCategory = category.toLowerCase();
        const normalizedFileName = fileName.toLowerCase();
        return normalizedKey.includes(normalizedCategory) && normalizedKey.endsWith(normalizedFileName);
      });
      if (matchingKey) {
        paths.push({ id: `${category}_${i}`, fileName, path: iconModules[matchingKey] as string });
      }
    }
  } else {
    for (let i = config.start; i <= config.end; i++) {
      try {
        const fileName = `${i}.png`;
        const iconPath = new URL(`../assets/节点图标/${category}/${fileName}`, import.meta.url).href;
        paths.push({ id: `${category}_${i}`, fileName, path: iconPath });
      } catch {
        /* skip */
      }
    }
  }
  return paths;
}

export function getIconPathByFileName(fileName: string | undefined): string | null {
  if (!fileName) return null;
  let actualFileName = fileName;
  if (fileName.includes('/') || fileName.includes('\\')) {
    const pathParts = fileName.replace(/\\/g, '/').split('/');
    actualFileName = pathParts[pathParts.length - 1];
  }
  const match = actualFileName.match(/^(\d+)\.png$/);
  if (!match) return null;
  const iconNumber = parseInt(match[1], 10);
  let category = '';
  if (iconNumber >= 1000 && iconNumber <= 1011) category = '审核';
  else if (iconNumber >= 2000 && iconNumber <= 2016) category = '开始';
  else if (iconNumber >= 3000 && iconNumber <= 3028) category = '录入';
  else if (iconNumber >= 4000 && iconNumber <= 4024) category = '确认';
  else if (iconNumber >= 5000 && iconNumber <= 5018) category = '结束';
  else if (iconNumber >= 6000 && iconNumber <= 6027) category = '能量隔离';
  else if (iconNumber >= 7000 && iconNumber <= 7021) category = '解除隔离';
  if (!category) return null;
  const allKeys = Object.keys(iconModules);
  const matchingKey = allKeys.find((k) => {
    const normalizedKey = k.replace(/\\/g, '/').toLowerCase();
    return normalizedKey.includes(category.toLowerCase()) && normalizedKey.endsWith(actualFileName.toLowerCase());
  });
  if (matchingKey) return iconModules[matchingKey] as string;
  return null;
}

/** 与 ProcessDesigner 中 getNodeDescription 文案一致 */
export function getWorkflowNodeDescription(type: string): string {
  const descriptions: Record<string, string> = {
    createJob: '该节点为作业创建人员创建作业录入信息开始节点。',
    confirm: '该节点为作业确认，为"上一节点"操作分配确认模式及确认人员权限',
    review: '该节点为作业审核，为"上一节点"操作分配审核模式及审核人员权限',
    inputInfo: '该节点为作业录入提交，可提交信息或图片，主要为"信息确认"',
    lock: '该节点为作业隔离类型选择，主要包括盲板，上锁挂牌，拆除等。',
    unlock: '该节点在解锁时与所选上锁节点保持隔离方式等信息一致。',
    coLock: '该节点需选择流程中的上锁任务并配置共锁人；隔离信息随所选上锁任务一致。',
    unlockCoLock: '该节点在解除共锁时与所选共锁节点保持隔离方式等信息一致。',
    isolation: '该节点为作业隔离类型选择，主要包括盲板，上锁挂牌，拆除等。',
    releaseIsolation: '该节点为作业隔离类型选择，主要包括盲板，上锁挂牌，拆除等。',
    returnLock: '该节点为还锁操作，归还钥匙，确认隔离操作完成',
    complete: '该节点为流程结束点',
  };
  return descriptions[type] || '该节点的功能描述。';
}
