"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuestionEntity = exports.VALIDATION_NOT_ENOUGH_ANSWERS = exports.VALIDATION_NO_CORRECT = void 0;
exports.makeQuestionEntity = makeQuestionEntity;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
exports.VALIDATION_NO_CORRECT = "At least one answer must be correct.";
exports.VALIDATION_NOT_ENOUGH_ANSWERS = "Question must have at least two answers";
class QuestionEntity extends core_2.AppObjectEntity {
    constructor() {
        super(...arguments);
        this.challengeID = "QUESTION";
        this.name = "Question";
        this.helpHeading = "Question Challenge Help";
        this.consumerHelpText = "You are being challenged to answer a question. Read the question and select the answer you think is correct.";
        this.authorHelpText = "Challenge the user to answer a multiple choice question.";
    }
}
exports.QuestionEntity = QuestionEntity;
QuestionEntity.type = "QuestionEntity";
QuestionEntity.get = (appObjects) => (0, core_1.getSingletonComponent)(QuestionEntity.type, appObjects);
function makeQuestionEntity(appObject) {
    return new QuestionChallengeImp(appObject);
}
class QuestionChallengeImp extends QuestionEntity {
    get gotItRight() {
        return this._gotItRight;
    }
    get reviewFeedback() {
        return this._reviewFeedback;
    }
    get validationMessage() {
        if (this.answerLookup.size < 2) {
            return exports.VALIDATION_NOT_ENOUGH_ANSWERS;
        }
        let hasCorrect = false;
        this.answerLookup.forEach((ans) => {
            if (ans.isCorrect) {
                hasCorrect = true;
            }
        });
        if (!hasCorrect) {
            return exports.VALIDATION_NO_CORRECT;
        }
        return;
    }
    constructor(appObject) {
        super(appObject, QuestionEntity.type);
        this.answerLookup = new Map();
        this._gotItRight = false;
        this._reviewFeedback = "";
        this.addAnswer = (answer) => {
            this.answerLookup.set(answer.id, answer);
            this.reset();
            answer.addChangeObserver(this.notifyOnChange);
        };
        this.removeAnswer = (id) => {
            const ans = this.answerLookup.get(id);
            if (!ans)
                return;
            ans.removeChangeObserver(this.notifyOnChange);
            this.answerLookup.delete(id);
            ans.appObject.dispose();
            this.reset();
        };
        this.getAnswer = (id) => {
            return this.answerLookup.get(id);
        };
        this.getAllAnswers = () => {
            return Array.from(this.answerLookup.values());
        };
        this.hasAnswer = (id) => {
            return this.answerLookup.has(id);
        };
        this.reset = () => {
            this._gotItRight = false;
            this._reviewFeedback = "";
            this.notifyOnChange();
        };
        this.submitAnswer = (id) => {
            const ans = this.getAnswer(id);
            if (!ans) {
                this.warn(`Unable to find answer by ID:  ${id}`);
                return;
            }
            this._gotItRight = ans.isCorrect;
            if (ans.feedbackText) {
                this._reviewFeedback = ans.feedbackText;
            }
            else {
                this._reviewFeedback = ans.isCorrect ? "Correct" : "Incorrect";
            }
            this.notifyOnChange();
        };
        this.createNewAnswer = () => {
            const answer = this.answerFactory();
            this.addAnswer(answer);
            return answer;
        };
        this.removeAllAnswers = () => {
            const allAnswers = Array.from(this.answerLookup.keys());
            allAnswers.forEach((id) => this.removeAnswer(id));
            this.reset();
        };
        this.setDefaultState = () => {
            this.removeAllAnswers();
            const trueAnswer = this.createNewAnswer();
            trueAnswer.isCorrect = true;
            trueAnswer.text = "True";
            const falseAnswer = this.createNewAnswer();
            falseAnswer.text = "False";
            this.reset();
        };
        this.answerFactory = (id) => {
            throw new Error("Factory has not been injected");
        };
    }
}
//# sourceMappingURL=QuestionEntity.js.map