// Function to process and combine notes
function ProcessNotes(notes) {
  const groupedBySemestre = notes.reduce((acc, note) => {
    if (!acc[note.semestre]) acc[note.semestre] = {}
    if (!acc[note.semestre][note.ue]) acc[note.semestre][note.ue] = []

    acc[note.semestre][note.ue].push(note)
    return acc
  }, {})

  const result = {}
  for (const semestre in groupedBySemestre) {
    result[semestre] = []
    for (const ue in groupedBySemestre[semestre]) {
      const elements = groupedBySemestre[semestre][ue]
      const totalCredits = elements.reduce((sum, el) => sum + el.credit, 0)
      const moyenne = elements.reduce((sum, el) => sum + el.note, 0) / elements.length

      elements.forEach((el, index) => {
        result[semestre].push({
          ue,
          element: el.element,
          credits: el.credits,
          note: el.note,
          totalCredits: index === 0 ? totalCredits : '', // Only display on the first row for each UE
          moyenne: index === 0 ? moyenne.toFixed(2) : '' // Only display on the first row for each UE
        })
      })
    }
  }
  return result
}

export default ProcessNotes
