import { FillBlanksExercise, MatchSentencesExercise, TrueFalseExercise, WriteBlanksExercise } from "@/interfaces/exam";

const getMinFillBlanksId = (exercise: FillBlanksExercise): number => {
    return Math.min(...exercise.solutions.map(s => parseInt(s.id)));
};

const getMinWriteBlanksId = (exercise: WriteBlanksExercise): number => {
    return Math.min(...exercise.solutions.map(s => parseInt(s.id)));
};

const getMinTrueFalseId = (exercise: TrueFalseExercise): number => {
    return Math.min(...exercise.questions.map(q => parseInt(q.id)));
};

const getMinMatchSentencesId = (exercise: MatchSentencesExercise): number => {
    return Math.min(...exercise.sentences.map(s => parseInt(s.id)));
};

const getMinExerciseId = (exercise: FillBlanksExercise | WriteBlanksExercise | TrueFalseExercise | MatchSentencesExercise): number => {
    switch (exercise.type) {
        case 'fillBlanks':
            return getMinFillBlanksId(exercise);
        case 'writeBlanks':
            return getMinWriteBlanksId(exercise);
        case 'trueFalse':
            return getMinTrueFalseId(exercise);
        case 'matchSentences':
            return getMinMatchSentencesId(exercise);
        default:
            return 1;
    }
};

export default getMinExerciseId;
