import { AppObjectUC } from "@vived/core";
import { StateMachineEntity } from "../../StateMachine";
import { QuestionEntity } from "../Entities/QuestionEntity";
export class SerializeQuestionChallenge extends AppObjectUC {
    getSystemState() {
        if (!this.questionEntity) {
            return {};
        }
        const answers = this.questionEntity
            .getAllAnswers()
            .map((answer) => {
            return {
                id: answer.id,
                feedback: answer.feedbackText,
                isCorrect: answer.isCorrect,
                text: answer.text,
            };
        });
        const state = {
            answers,
        };
        return state;
    }
    applySystemState(state, version) {
        if (version === 1) {
            const mappedState = this.mapV1ToV3(state);
            this.applyDataV3(mappedState);
        }
        else if (version === 2) {
            const mappedState = this.mapV2ToV3(state);
            this.applyDataV3(mappedState);
        }
        else if (version === 3) {
            this.applyDataV3(state);
        }
        else {
            // Unsupported version
            this.warn(`Unsupported data version ${version} for serialized system ${this.name}. Reverting to default state`);
            this.applyDefaultState();
        }
    }
    applyDefaultState() {
        this.questionEntity?.setDefaultState();
    }
    mapV1ToV3(data) {
        // Easy!
        return data;
    }
    mapV2ToV3(data) {
        const { answers } = data;
        return {
            answers,
        };
    }
    applyDataV3(data) {
        if (!this.questionEntity)
            return;
        const stateAnswerIDs = [];
        data.answers.forEach((answerData) => {
            let answer = this.questionEntity.getAnswer(answerData.id);
            if (!answer) {
                answer = this.questionEntity.answerFactory(answerData.id);
                this.questionEntity?.addAnswer(answer);
            }
            answer.text = answerData.text;
            answer.isCorrect = answerData.isCorrect;
            answer.feedbackText = answerData.feedback;
            stateAnswerIDs.push(answerData.id);
        });
        const allAnswers = Array.from(this.questionEntity.getAllAnswers());
        allAnswers.forEach((ans) => {
            if (!stateAnswerIDs.includes(ans.id)) {
                this.questionEntity.removeAnswer(ans.id);
            }
        });
    }
    constructor(appObject) {
        super(appObject, SerializeQuestionChallenge.type);
        this.name = "question";
        this.seriliziationVersion = 3;
        this.questionEntity = appObject.getComponent(QuestionEntity.type);
        if (!this.questionEntity) {
            this.warn("SerializeQuestionChallenge has been added to an app object that does not have QuestionEntity");
        }
        const stateMachine = StateMachineEntity.get(this.appObject.appObjectRepo);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
SerializeQuestionChallenge.type = "SerializeQuestionChallenge";
//# sourceMappingURL=SerializeQuestionChallenge.js.map