import { AppObjectUC } from "@vived/core";
import { EmulateStylusUC } from "../../Stylus";
import { RenderToXR } from "../../XR";
import { CheckZSpaceDisplaySizeUC, ZSpaceDevice, ZSpaceEntity, } from "../../zSpace";
import { AppHandlerEntity, UnableToParsePayload, UnsupportedRequestVersion, } from "../Entities";
export class StartZSpaceBaseUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "START_ZSPACE";
        this.payloadVersion = 3;
    }
}
StartZSpaceBaseUC.type = "StartZSpaceBaseUC";
export function makeStartZSpaceUC(appObject) {
    return new StartZSpaceBaseUCImp(appObject);
}
class StartZSpaceBaseUCImp extends StartZSpaceBaseUC {
    castPayloadV3(payload) {
        const castPayload = payload;
        if (castPayload.session === undefined) {
            throw new UnableToParsePayload(this.requestType, 1, JSON.stringify(payload));
        }
        if (castPayload.emulate === undefined) {
            castPayload.emulate = false;
        }
        return castPayload;
    }
    constructor(appObject) {
        super(appObject, StartZSpaceBaseUC.type);
        this.action = (session, emulate) => {
            const appObjects = this.appObject.appObjectRepo;
            const zSpaceEntity = ZSpaceEntity.get(appObjects);
            if (!zSpaceEntity) {
                this.warn("No zSpace Entity Found");
                return;
            }
            const renderUC = zSpaceEntity.appObject.getComponent(RenderToXR.type);
            if (!renderUC) {
                console.warn("No renderUC found");
                return;
            }
            zSpaceEntity.display.setForDevice(ZSpaceDevice.INSPIRE);
            zSpaceEntity.emulate = emulate;
            if (emulate) {
                const emulateStylus = EmulateStylusUC.get(appObjects);
                if (emulateStylus) {
                    emulateStylus.startEmulating();
                }
                else {
                    appObjects.submitWarning("startZSpaceAction", "EmulateStylusUC not found");
                    return;
                }
            }
            else {
                const xrSession = session;
                if (!xrSession) {
                    this.warn("Unable to cast the XR session");
                    return;
                }
                renderUC.startRenderingXR(xrSession);
                const checkDisplaySizeUC = zSpaceEntity.appObject.getComponent(CheckZSpaceDisplaySizeUC.type);
                if (checkDisplaySizeUC) {
                    checkDisplaySizeUC.checkDisplayFromXRSession(session);
                }
                else {
                    console.warn("No checkDisplaySizeUC found");
                }
            }
            zSpaceEntity.enabled = true;
        };
        this.handleRequest = (version, payload) => {
            if (version === this.payloadVersion) {
                const { session, emulate } = this.castPayloadV3(payload);
                this.action(session, emulate);
            }
            else {
                throw new UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(AppHandlerEntity.type);
        if (!handler) {
            this.warn("StartZSpaceBaseUC has been added to an app object that does not have an AppHandlerEntity. Add one first");
            return;
        }
        handler.registerRequestHandler(this);
    }
}
//# sourceMappingURL=StartZSpaceUC.js.map