"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetChallengeUC = void 0;
exports.makeSetChallengeUC = makeSetChallengeUC;
const core_1 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const UndoRedo_1 = require("../../UndoRedo");
const ChallengeEntity_1 = require("../Entities/ChallengeEntity");
class SetChallengeUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(SetChallengeUC.type, appObjects);
    }
}
exports.SetChallengeUC = SetChallengeUC;
SetChallengeUC.type = "SetChallengeUC";
function makeSetChallengeUC(appObject) {
    return new SetChallengeUCImp(appObject);
}
class SetChallengeUCImp extends SetChallengeUC {
    get challengeEntity() {
        return this.getCachedSingleton(ChallengeEntity_1.ChallengeEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(Dispatcher_1.DispatchStateChangeUC.type)?.doDispatch;
    }
    get undo() {
        return this.getCachedSingleton(UndoRedo_1.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