import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { BaseAppState } from "../../BaseApp/Entity/BaseAppState";
import { StateMachineEntity } from "../../StateMachine/Entities";
import { AppDispatcher } from "../Entities/AppDispatcher";
export class DispatchStateChangeUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "ON_STATE_CHANGE";
    }
}
DispatchStateChangeUC.type = "DispatchStateChangeUC";
DispatchStateChangeUC.get = (appObjects) => getSingletonComponent(DispatchStateChangeUC.type, appObjects);
export function makeDispatchStateChangeUC(appObject) {
    return new DispatchStateChangeUCImp(appObject);
}
export function dispatchStateChange(appObjects) {
    const dispatchUC = DispatchStateChangeUC.get(appObjects);
    if (dispatchUC) {
        dispatchUC.doDispatch();
    }
    else {
        appObjects.submitWarning("dispatchStateChange", "Can't find DispatchStateChangeUC");
    }
}
class DispatchStateChangeUCImp extends DispatchStateChangeUC {
    constructor(appObject) {
        super(appObject, DispatchStateChangeUC.type);
        this.requestVersion = 3;
        this.validateState = () => {
            return undefined;
        };
        this.getStateAssets = () => {
            return [];
        };
        this.doDispatch = () => {
            const stateMachine = StateMachineEntity.get(this.appObject.appObjectRepo);
            if (!stateMachine) {
                this.warn("Could not find StateMachineEntity");
                return;
            }
            const baseApp = BaseAppState.get(this.appObject.appObjectRepo);
            if (!baseApp) {
                this.warn("Could not find BaseAppState");
                return;
            }
            if (!baseApp.isAuthoring)
                return;
            const payload = {
                stateObject: stateMachine.getSerializedStates(),
                assets: this.getStateAssets(),
                validationErrorMessage: this.validateState(),
            };
            this.appDispatcher?.formRequestAndDispatch(this.requestType, this.requestVersion, payload);
        };
        this.appDispatcher = appObject.getComponent(AppDispatcher.type);
        if (!this.appDispatcher) {
            this.warn("DispatchStateChangeUC has been added to an App Object that does not have a AppDispatcher. Add a dispatcher first");
            this.dispose();
        }
    }
}
//# sourceMappingURL=DispatchStateChangeUC.js.map