import { AppObjectUC } from "@vived/core";
import { BaseAppState } from "../../BaseApp/Entity";
import { StateMachineEntity, } from "../../StateMachine/Entities";
import { UndoRedoEntity } from "../../UndoRedo/Entities";
import { AppHandlerEntity, UnableToParsePayload, UnsupportedRequestVersion, } from "../Entities";
export class SetAppStateBaseUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "SET_APP_STATE";
        this.payloadVersion = 3;
    }
}
SetAppStateBaseUC.type = "SetAppStateBaseUC";
export function makeSetAppStateUC(appObject) {
    return new SetAppStateBaseUCImp(appObject);
}
class SetAppStateBaseUCImp extends SetAppStateBaseUC {
    get baseApp() {
        return this.getCachedSingleton(BaseAppState.type);
    }
    get stateMachine() {
        return this.getCachedSingleton(StateMachineEntity.type);
    }
    get undoRedo() {
        return this.getCachedSingleton(UndoRedoEntity.type);
    }
    castPayload(payload) {
        const castPayload = payload;
        if (castPayload.hasNextSlide === undefined) {
            throw new 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 UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(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