import { AppObjectUC } from "@vived/core";
import { AppRenderingUC } from "../../BaseApp";
import { AppHandlerEntity, UnableToParsePayload, UnsupportedRequestVersion, } from "../Entities";
export class StartAppBaseUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "START_APP";
        this.payloadVersion = 2;
    }
}
StartAppBaseUC.type = "StartAppBaseUC";
export function makeStartAppUC(appObject) {
    return new StartAppBaseUCImp(appObject);
}
class StartAppBaseUCImp extends StartAppBaseUC {
    castPayloadV2(payload) {
        const castPayload = payload;
        if (castPayload.container === undefined) {
            throw new 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 = 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 UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(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