import { Exercise } from "@/interfaces/exam";
import ExerciseItem from "./types";
import ExerciseLabel from "../../Shared/ExerciseLabel";
import MultipleChoice from "../../Exercises/MultipleChoice";
import FillBlanksMC from "../../Exercises/Blanks/MultipleChoice";

const getLevelQuestionItems = (exercises: Exercise[], sectionId: number): ExerciseItem[] => {

	const previewLabel = (text: string) => {
		return text !== undefined ? text.replaceAll('\\n', ' ').split(' ').slice(0, 15).join(' ') : "";
	}

	const items: ExerciseItem[] = exercises.map((exercise, index) => {
		let firstWordId, lastWordId;
		switch (exercise.type) {
			case "multipleChoice":
				firstWordId = exercise.questions[0].id;
				lastWordId = exercise.questions[exercise.questions.length - 1].id;
				return {
					id: index,
					sectionId,
					label: (
						<ExerciseLabel
							label={`Multiple Choice Questions #${firstWordId} - #${lastWordId}`}
							preview={
								<>
									&quot;{previewLabel(exercise.prompt)}...&quot;
								</>
							}
						/>
					),
					content: <MultipleChoice exercise={exercise} sectionId={sectionId} />
				};
			case "fillBlanks":
				firstWordId = exercise.solutions[0].id;
				lastWordId = exercise.solutions[exercise.solutions.length - 1].id;
				return {
					id: index,
					sectionId,
					label: (
						<ExerciseLabel
							label={`Fill Blanks Question #${firstWordId} - #${lastWordId}`}
							preview={
								<>
									&quot;{previewLabel(exercise.prompt)}...&quot;
								</>
							}
						/>
					),
					content: <FillBlanksMC exercise={exercise} sectionId={sectionId} />
				};
			default:
				return {} as unknown as ExerciseItem;
		}
	}).filter((item) => item !== undefined);

	return items || [];
};


export default getLevelQuestionItems;
