import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { DispatchStateChangeUC } from "../../Dispatcher";
import { UndoRedoEntity } from "../../UndoRedo";
import { ChallengeEntity } from "../Entities/ChallengeEntity";
export class SetChallengeUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(SetChallengeUC.type, appObjects);
    }
}
SetChallengeUC.type = "SetChallengeUC";
export function makeSetChallengeUC(appObject) {
    return new SetChallengeUCImp(appObject);
}
class SetChallengeUCImp extends SetChallengeUC {
    get challengeEntity() {
        return this.getCachedSingleton(ChallengeEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC.type)?.doDispatch;
    }
    get undo() {
        return this.getCachedSingleton(UndoRedoEntity.type);
    }
    setChallenge(challenge) {
        const challengeEntity = this.challengeEntity;
        const dispatchStateChange = this.dispatchStateChange;
        if (!challengeEntity || !dispatchStateChange) {
            return;
        }
        if (challengeEntity.selectedChallenge === challenge)
            return;
        const initialChallenge = challengeEntity.selectedChallenge;
        challengeEntity.selectedChallenge = challenge;
        dispatchStateChange();
        this.undo?.submitAction({
            undoAction: () => {
                challengeEntity.selectedChallenge = initialChallenge;
                dispatchStateChange();
            },
            redoAction: () => {
                challengeEntity.selectedChallenge = challenge;
                dispatchStateChange();
            },
        });
    }
    constructor(appObject) {
        super(appObject, SetChallengeUC.type);
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=SetChallengeUC.js.map