const RESOURCE_NAME_TRANSLATION_ENTRIES = ([
  ['头像框', 'avatar frame'],
  ['座驾', 'ride'],
  ['坐骑', 'mount'],
  ['勋章', 'badge'],
  ['徽章', 'badge'],
  ['管理员', 'admin'],
  ['圣诞', 'christmas'],
  ['新年', 'new year'],
  ['情人节', 'valentine'],
  ['万圣节', 'halloween'],
  ['锦鲤', 'koi'],
  ['熊猫', 'panda'],
  ['独角兽', 'unicorn'],
  ['天使', 'angel'],
  ['恶魔', 'devil'],
  ['皇冠', 'crown'],
  ['国王', 'king'],
  ['女王', 'queen'],
  ['玫瑰', 'rose'],
  ['星光', 'starlight'],
  ['星星', 'star'],
  ['月亮', 'moon'],
  ['太阳', 'sun'],
  ['彩虹', 'rainbow'],
  ['火焰', 'flame'],
  ['冰雪', 'snow'],
  ['蓝色', 'blue'],
  ['红色', 'red'],
  ['紫色', 'purple'],
  ['粉色', 'pink'],
  ['金色', 'gold'],
  ['银色', 'silver'],
  ['黑色', 'black'],
  ['白色', 'white'],
  ['绿色', 'green'],
  ['黄色', 'yellow'],
  ['钻石', 'diamond'],
  ['水晶', 'crystal'],
  ['幸运', 'lucky'],
  ['浪漫', 'romantic'],
  ['荣耀', 'glory'],
  ['守护', 'guard'],
  ['贵族', 'noble'],
  ['梦幻', 'dream'],
  ['闪耀', 'shining'],
  ['限定', 'limited'],
  ['豪华', 'deluxe'],
  ['高级', 'premium'],
  ['经典', 'classic'],
  ['热气球', 'hot air balloon'],
  ['跑车', 'sports car'],
  ['飞船', 'spaceship'],
  ['游艇', 'yacht'],
  ['摩托', 'motorcycle'],
  ['龙', 'dragon'],
  ['凤', 'phoenix'],
  ['虎', 'tiger'],
  ['兔', 'rabbit'],
  ['猫', 'cat'],
  ['狗', 'dog'],
  ['鱼', 'fish'],
] as Array<[string, string]>).toSorted(
  (current, next) => next[0].length - current[0].length,
);

export function normalizeResourceBaseName(value: string) {
  const name = value
    .replace(/\.[^.]+$/, '')
    .replace(/([_-]?(cover|covers|source|resource|resources|ios|android|expand))$/i, '')
    .replace(/([_-]?(封面|资源))$/i, '')
    .trim();
  return name || value.replace(/\.[^.]+$/, '') || 'resource';
}

export function normalizeResourceCode(value: string) {
  const baseName = normalizeResourceBaseName(value);
  const translated = translateResourceName(baseName);
  return slugifyResourceCode(translated) || slugifyResourceCode(baseName) || 'resource';
}

function translateResourceName(value: string) {
  let result = value.trim();
  RESOURCE_NAME_TRANSLATION_ENTRIES.forEach(([source, target]) => {
    result = result.replaceAll(source, ` ${target} `);
  });
  return [...result]
    .map((char) => {
      if (/[\u4E00-\u9FFF]/.test(char)) {
        return ` u${char.codePointAt(0)?.toString(16) || ''} `;
      }
      return char;
    })
    .join('');
}

function slugifyResourceCode(value: string) {
  return value
    .normalize('NFKC')
    .replaceAll(/[^\d A-Za-z]+/g, ' ')
    .trim()
    .replaceAll(/\s+/g, '_')
    .toLowerCase();
}
