export const specialtyCatalog = Object.freeze([
  Object.freeze({
    code: 'sports',
    name: '体育',
    types: Object.freeze([
      Object.freeze({ code: 'track_field', name: '田径' }),
      Object.freeze({ code: 'basketball', name: '篮球' }),
      Object.freeze({ code: 'football', name: '足球' }),
      Object.freeze({ code: 'volleyball', name: '排球' }),
      Object.freeze({ code: 'table_tennis', name: '乒乓球' }),
      Object.freeze({ code: 'badminton', name: '羽毛球' }),
      Object.freeze({ code: 'swimming', name: '游泳' }),
      Object.freeze({ code: 'martial_arts', name: '武术' }),
      Object.freeze({ code: 'aerobics_cheer', name: '健美操与啦啦操' })
    ])
  }),
  Object.freeze({
    code: 'arts',
    name: '艺术',
    types: Object.freeze([
      Object.freeze({ code: 'vocal_music', name: '声乐' }),
      Object.freeze({ code: 'instrumental_music', name: '器乐' }),
      Object.freeze({ code: 'dance', name: '舞蹈' }),
      Object.freeze({ code: 'fine_arts', name: '美术' }),
      Object.freeze({ code: 'calligraphy', name: '书法' }),
      Object.freeze({ code: 'drama_broadcasting', name: '戏剧与播音' })
    ])
  })
]);

const categoryMap = new Map(specialtyCatalog.map(category => [category.code, category]));
const typeMap = new Map(specialtyCatalog.flatMap(category => category.types.map(type => [type.code, { ...type, categoryCode: category.code, categoryName: category.name }])));
const legacyTypeMap = new Map(specialtyCatalog.flatMap(category => category.types.map(type => [type.name, { category: category.code, type: type.code }])));

export function specialtyCategory(code) {
  return categoryMap.get(String(code || '')) || null;
}

export function specialtyType(code) {
  return typeMap.get(String(code || '')) || null;
}

export function isValidSpecialty(categoryCode, typeCode) {
  if (!categoryCode && !typeCode) return true;
  const category = specialtyCategory(categoryCode);
  const type = specialtyType(typeCode);
  return Boolean(category && type && type.categoryCode === category.code);
}

export function resolveProfileSpecialty(profile = {}) {
  if (isValidSpecialty(profile.specialtyCategory, profile.specialtyType) && profile.specialtyCategory) {
    return { category: profile.specialtyCategory, type: profile.specialtyType };
  }
  const legacy = (Array.isArray(profile.specialtyTypes) ? profile.specialtyTypes : []).map(value => legacyTypeMap.get(String(value))).find(Boolean);
  return legacy || { category: '', type: '' };
}

export function specialtyLabel(categoryCode, typeCode) {
  const category = specialtyCategory(categoryCode);
  const type = specialtyType(typeCode);
  if (!category) {
    const legacy = legacyTypeMap.get(String(typeCode || ''));
    return legacy ? specialtyLabel(legacy.category, legacy.type) : '';
  }
  return type?.categoryCode === category.code ? `${category.name}·${type.name}` : category.name;
}

export function candidateEligibleForCategory(profile, category) {
  const legacy = !category?.specialtyCategory ? legacyTypeMap.get(String(category?.specialtyType || '')) : null;
  const requiredCategory = category?.specialtyCategory || legacy?.category || '';
  const requiredType = legacy?.type || category?.specialtyType || '';
  if (!requiredCategory) return true;
  const qualification = resolveProfileSpecialty(profile);
  if (qualification.category !== requiredCategory) return false;
  return !requiredType || qualification.type === requiredType;
}
