import { AppObjectUC } from "@vived/core";
import { BaseAppState } from "../../BaseApp";
import { AppHandlerEntity, UnableToParsePayload, UnsupportedRequestVersion, } from "../Entities";
export class SetIsAuthoringUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "SET_IS_AUTHORING";
        this.payloadVersion = 1;
    }
}
SetIsAuthoringUC.type = "SetIsAuthoringBaseUC";
export function makeSetIsAuthoringUC(appObject) {
    return new SetIsAuthoringUCImp(appObject);
}
class SetIsAuthoringUCImp extends SetIsAuthoringUC {
    castPayloadV1(payload) {
        const castPayload = payload;
        if (castPayload.isAuthoring === undefined) {
            throw new UnableToParsePayload(this.requestType, 1, JSON.stringify(payload));
        }
        return castPayload;
    }
    constructor(appObject) {
        super(appObject, SetIsAuthoringUC.type);
        this.action = (isAuthoring) => {
            const appObjects = this.appObject.appObjectRepo;
            const baseApp = BaseAppState.get(appObjects);
            if (!baseApp) {
                this.warn("Unable to find BaseAppState");
                return;
            }
            baseApp.isAuthoring = isAuthoring;
        };
        this.handleRequest = (version, payload) => {
            if (version === this.payloadVersion) {
                const { isAuthoring: setIsAuthoring } = this.castPayloadV1(payload);
                this.action(setIsAuthoring);
            }
            else {
                throw new UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(AppHandlerEntity.type);
        if (!handler) {
            this.warn("SetIsAuthoringBaseUC has been added to an app object that does not have an AppHandlerEntity. Add one first");
            return;
        }
        handler.registerRequestHandler(this);
    }
}
//# sourceMappingURL=SetIsAuthoringUC.js.map