"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectedChallengePM = exports.defaultSelectedChallengeVM = void 0;
exports.makeSelectedChallengePM = makeSelectedChallengePM;
const core_1 = require("@vived/core");
const ChallengeEntity_1 = require("../Entities/ChallengeEntity");
exports.defaultSelectedChallengeVM = {
    options: [],
    selectedChallenge: "",
};
class SelectedChallengePM extends core_1.AppObjectPM {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(SelectedChallengePM.type, appObjects);
    }
}
exports.SelectedChallengePM = SelectedChallengePM;
SelectedChallengePM.type = "SelectedChallengePM";
function makeSelectedChallengePM(appObject) {
    return new SelectedChallengePMImp(appObject);
}
class SelectedChallengePMImp extends SelectedChallengePM {
    get challengeEntity() {
        return this.getCachedSingleton(ChallengeEntity_1.ChallengeEntity.type);
    }
    vmsAreEqual(a, b) {
        if (a.selectedChallenge !== b.selectedChallenge)
            return false;
        if (a.options.length !== b.options.length)
            return false;
        let optionsAreTheSame = true;
        a.options.forEach((optionA, i) => {
            const optionB = b.options[i];
            if (optionA.label !== optionB.label || optionA.value !== optionB.value) {
                optionsAreTheSame = false;
            }
        });
        return optionsAreTheSame;
    }
    constructor(appObject) {
        super(appObject, SelectedChallengePM.type);
        this.onChallengeChange = () => {
            if (!this.challengeEntity)
                return;
            const options = [];
            this.challengeEntity.allChallenges.forEach((challenge) => {
                options.push({ value: challenge.challengeID, label: challenge.name });
            });
            const vm = {
                selectedChallenge: this.challengeEntity.selectedChallenge,
                options,
            };
            this.doUpdateView(vm);
        };
        this.challengeEntity?.addChangeObserver(this.onChallengeChange);
        this.onChallengeChange();
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=SelectedChallengePM.js.map