import { AppObjectPM, getSingletonComponent, } from "@vived/core";
import { LabelDropRepo } from "../Entities/LabelDropRepo";
export const defaultLabelDropListVM = {
    answerItemIds: [],
    challengeValidationMessage: "",
};
export class LabelDropListPM extends AppObjectPM {
}
LabelDropListPM.type = "LabelDropListPM";
LabelDropListPM.get = (appObjects) => getSingletonComponent(LabelDropListPM.type, appObjects);
export function makeLabelDropListPM(appObject) {
    return new LabelDropListPMImp(appObject);
}
class LabelDropListPMImp extends LabelDropListPM {
    get labelDropRepo() {
        return this.getCachedSingleton(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