"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetIsAuthoringUC = void 0;
exports.makeSetIsAuthoringUC = makeSetIsAuthoringUC;
const core_1 = require("@vived/core");
const BaseApp_1 = require("../../BaseApp");
const Entities_1 = require("../Entities");
class SetIsAuthoringUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "SET_IS_AUTHORING";
        this.payloadVersion = 1;
    }
}
exports.SetIsAuthoringUC = SetIsAuthoringUC;
SetIsAuthoringUC.type = "SetIsAuthoringBaseUC";
function makeSetIsAuthoringUC(appObject) {
    return new SetIsAuthoringUCImp(appObject);
}
class SetIsAuthoringUCImp extends SetIsAuthoringUC {
    castPayloadV1(payload) {
        const castPayload = payload;
        if (castPayload.isAuthoring === undefined) {
            throw new Entities_1.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 = BaseApp_1.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 Entities_1.UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(Entities_1.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