"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuestionAnswerPM = exports.defaultQuestionChallengeAnswerVM = void 0;
exports.makeQuestionAnswerPM = makeQuestionAnswerPM;
const core_1 = require("@vived/core");
const QuestionAnswerEntity_1 = require("../Entities/QuestionAnswerEntity");
exports.defaultQuestionChallengeAnswerVM = {
    answerText: "",
    feedbackText: "",
    isCorrect: false,
};
class QuestionAnswerPM extends core_1.AppObjectPM {
    static get(appObject) {
        return appObject.getComponent(QuestionAnswerPM.type);
    }
    static getByID(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(QuestionAnswerPM.type);
    }
}
exports.QuestionAnswerPM = QuestionAnswerPM;
QuestionAnswerPM.type = "QuestionAnswerPM";
function makeQuestionAnswerPM(appObject) {
    return new QuestionAnswerPMImp(appObject);
}
class QuestionAnswerPMImp extends QuestionAnswerPM {
    get answer() {
        return this.getCachedLocalComponent(QuestionAnswerEntity_1.QuestionAnswerEntity.type);
    }
    vmsAreEqual(a, b) {
        if (a.answerText !== b.answerText)
            return false;
        if (a.feedbackText !== b.feedbackText)
            return false;
        if (a.isCorrect !== b.isCorrect)
            return false;
        if (a.textValidation !== b.textValidation)
            return false;
        if (a.feedbackValidation !== b.feedbackValidation)
            return false;
        return true;
    }
    constructor(appObject) {
        super(appObject, QuestionAnswerPM.type);
        this.dispose = () => {
            super.dispose();
            this.answer?.removeChangeObserver(this.onAnswerChange);
        };
        this.onAnswerChange = () => {
            if (!this.answer)
                return;
            const { feedbackText, text, isCorrect, feedbackValidationMessage, textValidationMessage, } = this.answer;
            const vm = {
                answerText: text,
                feedbackText,
                isCorrect,
                feedbackValidation: feedbackValidationMessage,
                textValidation: textValidationMessage,
            };
            this.doUpdateView(vm);
        };
        if (this.answer) {
            this.answer.addChangeObserver(this.onAnswerChange);
            this.onAnswerChange();
        }
        else {
            this.error("QuestionAnswerPM has been added to an app object that does not have QuestionAnswerEntity");
        }
    }
}
//# sourceMappingURL=QuestionAnswerPM.js.map