"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetAppStateBaseUC = void 0;
exports.makeSetAppStateUC = makeSetAppStateUC;
const core_1 = require("@vived/core");
const Entity_1 = require("../../BaseApp/Entity");
const Entities_1 = require("../../StateMachine/Entities");
const Entities_2 = require("../../UndoRedo/Entities");
const Entities_3 = require("../Entities");
class SetAppStateBaseUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "SET_APP_STATE";
        this.payloadVersion = 3;
    }
}
exports.SetAppStateBaseUC = SetAppStateBaseUC;
SetAppStateBaseUC.type = "SetAppStateBaseUC";
function makeSetAppStateUC(appObject) {
    return new SetAppStateBaseUCImp(appObject);
}
class SetAppStateBaseUCImp extends SetAppStateBaseUC {
    get baseApp() {
        return this.getCachedSingleton(Entity_1.BaseAppState.type);
    }
    get stateMachine() {
        return this.getCachedSingleton(Entities_1.StateMachineEntity.type);
    }
    get undoRedo() {
        return this.getCachedSingleton(Entities_2.UndoRedoEntity.type);
    }
    castPayload(payload) {
        const castPayload = payload;
        if (castPayload.hasNextSlide === undefined) {
            throw new Entities_3.UnableToParsePayload(this.requestType, 1, JSON.stringify(payload));
        }
        return castPayload;
    }
    constructor(appObject) {
        super(appObject, SetAppStateBaseUC.type);
        this.action = (data) => {
            if (!this.stateMachine || !this.baseApp) {
                this.error("Missing component");
                return;
            }
            const { finalState, hasNextSlide, hasPreviousSlide, hideNavigation, duration, } = data;
            if (finalState) {
                this.stateMachine.setSerializedStates(finalState, duration ?? 0);
            }
            else {
                this.stateMachine.setDefaultState(duration ?? 0);
            }
            this.baseApp.isReviewing = false;
            this.baseApp.hasNextState = hasNextSlide;
            this.baseApp.hasPreviousState = hasPreviousSlide;
            this.baseApp.hideSlideNavigation = hideNavigation;
            this.undoRedo?.clearStack();
        };
        this.handleRequest = (version, payload) => {
            if (version === this.payloadVersion) {
                const data = this.castPayload(payload);
                this.action(data);
            }
            else {
                throw new Entities_3.UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(Entities_3.AppHandlerEntity.type);
        if (!handler) {
            this.warn("SetAppStateBaseUC has been added to an app object that does not have an AppHandlerEntity. Add one first");
            return;
        }
        handler.registerRequestHandler(this);
    }
}
//# sourceMappingURL=SetAppStateUC.js.map