"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DispatchStateChangeUC = void 0;
exports.makeDispatchStateChangeUC = makeDispatchStateChangeUC;
exports.dispatchStateChange = dispatchStateChange;
const core_1 = require("@vived/core");
const BaseAppState_1 = require("../../BaseApp/Entity/BaseAppState");
const Entities_1 = require("../../StateMachine/Entities");
const AppDispatcher_1 = require("../Entities/AppDispatcher");
class DispatchStateChangeUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "ON_STATE_CHANGE";
    }
}
exports.DispatchStateChangeUC = DispatchStateChangeUC;
DispatchStateChangeUC.type = "DispatchStateChangeUC";
DispatchStateChangeUC.get = (appObjects) => (0, core_1.getSingletonComponent)(DispatchStateChangeUC.type, appObjects);
function makeDispatchStateChangeUC(appObject) {
    return new DispatchStateChangeUCImp(appObject);
}
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 = Entities_1.StateMachineEntity.get(this.appObject.appObjectRepo);
            if (!stateMachine) {
                this.warn("Could not find StateMachineEntity");
                return;
            }
            const baseApp = BaseAppState_1.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_1.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