"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LabelDropListPM = exports.defaultLabelDropListVM = void 0;
exports.makeLabelDropListPM = makeLabelDropListPM;
const core_1 = require("@vived/core");
const LabelDropRepo_1 = require("../Entities/LabelDropRepo");
exports.defaultLabelDropListVM = {
    answerItemIds: [],
    challengeValidationMessage: "",
};
class LabelDropListPM extends core_1.AppObjectPM {
}
exports.LabelDropListPM = LabelDropListPM;
LabelDropListPM.type = "LabelDropListPM";
LabelDropListPM.get = (appObjects) => (0, core_1.getSingletonComponent)(LabelDropListPM.type, appObjects);
function makeLabelDropListPM(appObject) {
    return new LabelDropListPMImp(appObject);
}
class LabelDropListPMImp extends LabelDropListPM {
    get labelDropRepo() {
        return this.getCachedSingleton(LabelDropRepo_1.LabelDropRepo.type);
    }
    vmsAreEqual(a, b) {
        if (a.answerItemIds.length !== b.answerItemIds.length)
            return false;
        if (a.challengeValidationMessage !== b.challengeValidationMessage)
            return false;
        let itemListIsEqual = true;
        for (let i = 0; i < a.answerItemIds.length; i++) {
            if (a.answerItemIds[i] !== b.answerItemIds[i]) {
                itemListIsEqual = false;
            }
        }
        return itemListIsEqual;
    }
    constructor(appObject) {
        super(appObject, LabelDropListPM.type);
        this.onEntityChange = () => {
            if (!this.labelDropRepo)
                return;
            const answerItemIds = this.labelDropRepo
                .getAll()
                .map((item) => item.id);
            const vm = {
                answerItemIds,
                challengeValidationMessage: this.labelDropRepo.validationMessage ?? "",
            };
            this.doUpdateView(vm);
        };
        this.labelDropRepo?.addChangeObserver(this.onEntityChange);
        this.appObjects.registerSingleton(this);
        this.onEntityChange();
    }
}
//# sourceMappingURL=LabelDropListPM.js.map