"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StartAppBaseUC = void 0;
exports.makeStartAppUC = makeStartAppUC;
const core_1 = require("@vived/core");
const BaseApp_1 = require("../../BaseApp");
const Entities_1 = require("../Entities");
class StartAppBaseUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "START_APP";
        this.payloadVersion = 2;
    }
}
exports.StartAppBaseUC = StartAppBaseUC;
StartAppBaseUC.type = "StartAppBaseUC";
function makeStartAppUC(appObject) {
    return new StartAppBaseUCImp(appObject);
}
class StartAppBaseUCImp extends StartAppBaseUC {
    castPayloadV2(payload) {
        const castPayload = payload;
        if (castPayload.container === undefined) {
            throw new Entities_1.UnableToParsePayload(this.requestType, 1, JSON.stringify(payload));
        }
        return castPayload;
    }
    constructor(appObject) {
        super(appObject, StartAppBaseUC.type);
        this.action = (container) => {
            const appObjects = this.appObject.appObjectRepo;
            const appRenderingUC = BaseApp_1.AppRenderingUC.get(appObjects);
            if (!appRenderingUC) {
                console.warn("Unable to find AppRenderingUC");
                return;
            }
            appRenderingUC.startRendering(container);
        };
        this.handleRequest = (version, payload) => {
            if (version === this.payloadVersion) {
                const { container } = this.castPayloadV2(payload);
                this.action(container);
            }
            else {
                throw new Entities_1.UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(Entities_1.AppHandlerEntity.type);
        if (!handler) {
            this.warn("StartAppBaseUC has been added to an app object that does not have an AppHandlerEntity. Add one first");
            return;
        }
        handler.registerRequestHandler(this);
    }
}
//# sourceMappingURL=StartAppUC.js.map