import { Session } from "@/hooks/useSessions";
import { ExamState } from "../types";

export type SessionActions =
    { type: 'SET_SESSION'; payload: { session: Session } }

export const SESSION_ACTIONS = [
    'SET_SESSION'
];

export const sessionReducer = (action: SessionActions): Partial<ExamState> => {
    switch (action.type) {
        case 'SET_SESSION':
            const { session } = action.payload;
            return {
                shuffles: session.userSolutions.map((x) => ({ exerciseID: x.exercise, shuffles: x.shuffleMaps ? x.shuffleMaps : [] })),
                selectedModules: session.selectedModules,
                exam: session.exam,
                exams: session.exams,
                sessionId: session.sessionId,
                assignment: session.assignment,
                exerciseIndex: session.exerciseIndex,
                partIndex: session.partIndex,
                moduleIndex: session.moduleIndex,
                timeSpent: session.timeSpent,
                userSolutions: session.userSolutions,
                timeSpentCurrentModule: session.timeSpentCurrentModule !== undefined ? session.timeSpentCurrentModule : session.timeSpent,
                showSolutions: false,
                questionIndex: session.questionIndex
            };
        default:
            return {};
    }
}
