import { Exam, ExerciseOnlyExam, PartExam, UserSolution } from "@/interfaces/exam";

const updateExamWithUserSolutions = (exam: Exam, userSolutions: UserSolution[]): Exam => {
    if (["reading", "listening", "level"].includes(exam.module)) {
        const parts = (exam as PartExam).parts.map((p) =>
            Object.assign(p, {
                exercises: p.exercises.map((x) =>
                    Object.assign(x, {
                        userSolutions: userSolutions.find((y) => x.id === y.exercise)?.solutions,
                    }),
                ),
            }),
        );
        return Object.assign(exam, { parts });
    }

    const exercises = (exam as ExerciseOnlyExam).exercises.map((x) =>
        Object.assign(x, {
            userSolutions: userSolutions.find((y) => x.id === y.exercise)?.solutions,
        }),
    );
    return Object.assign(exam, { exercises });
};

export {
    updateExamWithUserSolutions,
}