import { AppObjectPM, getSingletonComponent, } from "@vived/core";
import { ChallengeEntity } from "../Entities/ChallengeEntity";
export const defaultSelectedChallengeVM = {
    options: [],
    selectedChallenge: "",
};
export class SelectedChallengePM extends AppObjectPM {
    static get(appObjects) {
        return getSingletonComponent(SelectedChallengePM.type, appObjects);
    }
}
SelectedChallengePM.type = "SelectedChallengePM";
export function makeSelectedChallengePM(appObject) {
    return new SelectedChallengePMImp(appObject);
}
class SelectedChallengePMImp extends SelectedChallengePM {
    get challengeEntity() {
        return this.getCachedSingleton(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