"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StartZSpaceBaseUC = void 0;
exports.makeStartZSpaceUC = makeStartZSpaceUC;
const core_1 = require("@vived/core");
const Stylus_1 = require("../../Stylus");
const XR_1 = require("../../XR");
const zSpace_1 = require("../../zSpace");
const Entities_1 = require("../Entities");
class StartZSpaceBaseUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "START_ZSPACE";
        this.payloadVersion = 3;
    }
}
exports.StartZSpaceBaseUC = StartZSpaceBaseUC;
StartZSpaceBaseUC.type = "StartZSpaceBaseUC";
function makeStartZSpaceUC(appObject) {
    return new StartZSpaceBaseUCImp(appObject);
}
class StartZSpaceBaseUCImp extends StartZSpaceBaseUC {
    castPayloadV3(payload) {
        const castPayload = payload;
        if (castPayload.session === undefined) {
            throw new Entities_1.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 = zSpace_1.ZSpaceEntity.get(appObjects);
            if (!zSpaceEntity) {
                this.warn("No zSpace Entity Found");
                return;
            }
            const renderUC = zSpaceEntity.appObject.getComponent(XR_1.RenderToXR.type);
            if (!renderUC) {
                console.warn("No renderUC found");
                return;
            }
            zSpaceEntity.display.setForDevice(zSpace_1.ZSpaceDevice.INSPIRE);
            zSpaceEntity.emulate = emulate;
            if (emulate) {
                const emulateStylus = Stylus_1.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(zSpace_1.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 Entities_1.UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(Entities_1.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