import { AnyTypedQuizQuestion } from "@model/questionTypes/shared";
import { QuizSettings } from "@model/settingsData";

export interface GetQuizDataResponse {
    cnt: number;
    settings: {
        fp: boolean;
        rep: boolean;
        name: string;
        cfg: string;
        lim: number;
        due: number;
        delay: number;
        pausable: boolean;
    };
    items: {
        id: number;
        title: string;
        desc: string;
        typ: string;
        req: boolean;
        p: number;
        c: string;
    }[];
}

export function parseQuizData(quizDataResponse: GetQuizDataResponse): Omit<QuizSettings, "recentlyCompleted"> {
    const items: QuizSettings["questions"] = quizDataResponse.items.map((item) => {
        const content = JSON.parse(item.c);

        return {
            description: item.desc,
            id: item.id,
            page: item.p,
            required: item.req,
            title: item.title,
            type: item.typ,
            content
        } as unknown as AnyTypedQuizQuestion;
    });

    const settings: QuizSettings["settings"] = {
        fp: quizDataResponse.settings.fp,
        rep: quizDataResponse.settings.rep,
        name: quizDataResponse.settings.name,
        cfg: JSON.parse(quizDataResponse?.settings.cfg),
        lim: quizDataResponse.settings.lim,
        due: quizDataResponse.settings.due,
        delay: quizDataResponse.settings.delay,
        pausable: quizDataResponse.settings.pausable
    };

    return { cnt: quizDataResponse.cnt, settings, questions: items };
}
