"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectPickPM = exports.defaultObjectPickVM = void 0;
exports.makeObjectPickPM = makeObjectPickPM;
const core_1 = require("@vived/core");
const ObjectPickEntity_1 = require("../Entities/ObjectPickEntity");
const ValidateObjectPickUC_1 = require("../UseCases/ValidateObjectPickUC");
exports.defaultObjectPickVM = {
    correctObjectIds: [],
    isSelectingCorrectObjects: false,
    hasSubmitted: false,
    isCorrect: false,
    validationMessage: null,
};
class ObjectPickPM extends core_1.AppObjectPM {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(ObjectPickPM.type, appObjects);
    }
}
exports.ObjectPickPM = ObjectPickPM;
ObjectPickPM.type = "ObjectPickPM";
function makeObjectPickPM(appObject) {
    return new ObjectPickPMImp(appObject);
}
class ObjectPickPMImp extends ObjectPickPM {
    get objectPick() {
        return this.getCachedSingleton(ObjectPickEntity_1.ObjectPickEntity.type);
    }
    get validateObjectPickUC() {
        return this.getCachedSingleton(ValidateObjectPickUC_1.ValidateObjectPickUC.type);
    }
    vmsAreEqual(a, b) {
        if (a.correctObjectIds.length !== b.correctObjectIds.length)
            return false;
        if (a.hasSubmitted !== b.hasSubmitted)
            return false;
        if (a.isSelectingCorrectObjects !== b.isSelectingCorrectObjects)
            return false;
        if (a.validationMessage !== b.validationMessage)
            return false;
        if (a.isCorrect !== b.isCorrect)
            return false;
        let listsAreTheSame = true;
        a.correctObjectIds.forEach((objA, i) => {
            const objB = b.correctObjectIds[i];
            if (objA !== objB)
                listsAreTheSame = false;
        });
        return listsAreTheSame;
    }
    constructor(appObject) {
        super(appObject, ObjectPickPM.type);
        this.onEntityChange = () => {
            if (!this.objectPick)
                return;
            if (!this.validateObjectPickUC)
                return;
            const vm = {
                correctObjectIds: this.objectPick.correctObjectIds,
                isSelectingCorrectObjects: this.objectPick.isSelectingCorrectObjects,
                hasSubmitted: this.objectPick.hasSubmitted,
                isCorrect: this.objectPick.isCorrect,
                validationMessage: this.validateObjectPickUC.validateObjectPick(),
            };
            this.doUpdateView(vm);
        };
        this.objectPick?.addChangeObserver(this.onEntityChange);
        this.onEntityChange();
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=ObjectPickPM.js.map