const highStandardItems = [1, 4, 5, 10, 13];
const searchItems = [2, 3, 6, 8, 9, 12];
const difficultyItems = [7, 11];

function average(scores: number[]) {
  return scores.reduce((sum, score) => sum + score, 0) / scores.length;
}

function scoreItems(answers: string[], items: number[]) {
  return average(items.map((item) => Number(answers[item - 1] || 1)));
}

export function calculateMaximizerResults(answers: string[]) {
  const itemScores = answers.slice(0, 13).map((answer) => Number(answer || 1));
  const averageScore = average(itemScores);

  let level: 'satisficer' | 'balanced' | 'maximizer' = 'balanced';
  if (averageScore < 3.25) {
    level = 'satisficer';
  } else if (averageScore >= 4.75) {
    level = 'maximizer';
  }

  return {
    itemScores,
    average: averageScore,
    highStandards: scoreItems(answers, highStandardItems),
    search: scoreItems(answers, searchItems),
    difficulty: scoreItems(answers, difficultyItems),
    level,
  };
}
