"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuestionChallengePM = exports.defaultQuestionChallengeVM = exports.SLIDE_TEXT_CANNOT_BE_FALSY = void 0;
exports.makeQuestionChallengePM = makeQuestionChallengePM;
const core_1 = require("@vived/core");
const BaseApp_1 = require("../../BaseApp");
const QuestionEntity_1 = require("../Entities/QuestionEntity");
exports.SLIDE_TEXT_CANNOT_BE_FALSY = "Slide text is required.";
exports.defaultQuestionChallengeVM = {
    answerIds: [],
    gotItRight: false,
    reviewFeedback: "",
};
class QuestionChallengePM extends core_1.AppObjectPM {
}
exports.QuestionChallengePM = QuestionChallengePM;
QuestionChallengePM.type = "QuestionChallengePM";
QuestionChallengePM.get = (appObjects) => (0, core_1.getSingletonComponent)(QuestionChallengePM.type, appObjects);
function makeQuestionChallengePM(appObject) {
    return new QuestionChallengePMImp(appObject);
}
class QuestionChallengePMImp extends QuestionChallengePM {
    get questionEntity() {
        return this.getCachedSingleton(QuestionEntity_1.QuestionEntity.type);
    }
    get baseAppState() {
        return this.getCachedSingleton(BaseApp_1.BaseAppState.type);
    }
    vmsAreEqual(a, b) {
        if (a.gotItRight !== b.gotItRight)
            return false;
        if (a.validationMessage !== b.validationMessage)
            return false;
        if (a.reviewFeedback !== b.reviewFeedback)
            return false;
        if (a.answerIds.length !== b.answerIds.length)
            return false;
        let idsMatch = true;
        a.answerIds.forEach((_, i) => {
            if (a.answerIds[i] !== b.answerIds[i])
                idsMatch = false;
        });
        return idsMatch;
    }
    constructor(appObject) {
        super(appObject, QuestionChallengePM.type);
        this.onEntityChange = () => {
            if (!this.baseAppState)
                return;
            if (!this.questionEntity)
                return;
            let slideTextValidationMessage;
            if (this.baseAppState.slideText.length === 0) {
                slideTextValidationMessage = exports.SLIDE_TEXT_CANNOT_BE_FALSY;
            }
            const { gotItRight } = this.questionEntity;
            const answerIds = this.questionEntity.getAllAnswers().map((ans) => ans.id);
            const vm = {
                gotItRight,
                answerIds,
                reviewFeedback: this.questionEntity.reviewFeedback,
                validationMessage: slideTextValidationMessage ?? this.questionEntity.validationMessage,
            };
            this.doUpdateView(vm);
        };
        this.dispose = () => {
            super.dispose();
            this.questionEntity?.removeChangeObserver(this.onEntityChange);
            this.baseAppState?.removeChangeObserver(this.onEntityChange);
        };
        if (!this.baseAppState) {
            this.error("Unable to find BaseAppStateEntity");
            return;
        }
        else {
            this.baseAppState.addChangeObserver(this.onEntityChange);
            this.onEntityChange();
        }
        if (this.questionEntity) {
            this.questionEntity.addChangeObserver(this.onEntityChange);
            this.onEntityChange();
        }
        else {
            this.error("Unable to find QuestionEntity");
        }
    }
}
//# sourceMappingURL=QuestionChallengePM.js.map