export const riasecTypes = {
  R: { name: '现实型', items: [1, 2, 3, 4, 5, 6, 7, 8] },
  I: { name: '研究型', items: [9, 10, 11, 12, 13, 14, 15, 16] },
  A: { name: '艺术型', items: [17, 18, 19, 20, 21, 22, 23, 24] },
  S: { name: '社会型', items: [25, 26, 27, 28, 29, 30, 31, 32] },
  E: { name: '企业型', items: [33, 34, 35, 36, 37, 38, 39, 40] },
  C: { name: '常规型', items: [41, 42, 43, 44, 45, 46, 47, 48] },
} as const;

export function calculateRIASECResults(answers: string[]) {
  const scores = Object.fromEntries(
    Object.entries(riasecTypes).map(([code, type]) => {
      const score = type.items.reduce(
        (sum, item) => sum + Number(answers[item - 1] || 0),
        0,
      );
      return [code, { score, average: score / type.items.length }];
    }),
  );

  const ranking = Object.entries(scores).sort(
    ([codeA, resultA], [codeB, resultB]) =>
      resultB.score - resultA.score || codeA.localeCompare(codeB),
  );

  return {
    scores,
    ranking,
    hollandCode: ranking.slice(0, 3).map(([code]) => code).join(''),
  };
}
