const reverseItems = new Set([2, 5, 6, 8, 9]);

export function calculateSelfEsteemResults(answers: string[]) {
  const itemScores = answers.slice(0, 10).map((answer, index) => {
    const raw = Number(answer || 0);
    const item = index + 1;
    return reverseItems.has(item) ? 3 - raw : raw;
  });

  const total = itemScores.reduce((sum, score) => sum + score, 0);

  let level: 'low' | 'moderate' | 'high' = 'moderate';
  if (total <= 14) {
    level = 'low';
  } else if (total >= 25) {
    level = 'high';
  }

  return {
    total,
    itemScores,
    level,
  };
}
