const dimensions = {
  explorer: [1, 5, 9, 13, 17, 21, 25, 29, 33, 37],
  builder: [2, 6, 10, 14, 18, 22, 26, 30, 34, 38],
  director: [3, 7, 11, 15, 19, 23, 27, 31, 35, 39],
  negotiator: [4, 8, 12, 16, 20, 24, 28, 32, 36, 40],
} as const;

export type FisherDimension = keyof typeof dimensions;

const dimensionNames: Record<FisherDimension, string> = {
  explorer: "探索者",
  builder: "建设者",
  director: "指挥者",
  negotiator: "协商者",
};

function scoreDimension(answers: string[], items: readonly number[]) {
  return items.reduce((sum, item) => sum + Number(answers[item - 1] || 1), 0);
}

export function calculateFisherResults(answers: string[]) {
  const scores = Object.fromEntries(
    Object.entries(dimensions).map(([dimension, items]) => [
      dimension,
      scoreDimension(answers, items),
    ]),
  ) as Record<FisherDimension, number>;

  const ranked = (Object.keys(scores) as FisherDimension[])
    .map((id) => ({
      id,
      name: dimensionNames[id],
      score: scores[id],
      percentage: Math.round(((scores[id] - 10) / 30) * 100),
    }))
    .sort((a, b) => b.score - a.score);

  return {
    scores,
    ranked,
    primary: ranked[0],
    secondary: ranked[1],
  };
}
