import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { BaseAppState } from "../../BaseApp/Entity/BaseAppState";
import { PixelToSceneRayUC, SceneRayPickUC } from "../../Babylon/UC";
import { ChallengeEntity } from "../../Challenge/Entities/ChallengeEntity";
import { submitHitResult, } from "../../Dispatcher/UC/SubmitResultResultUC";
import { ObjectPickEntity } from "../Entities/ObjectPickEntity";
export class TrySubmitObjectPickUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(TrySubmitObjectPickUC.type, appObjects);
    }
}
TrySubmitObjectPickUC.type = "TrySubmitObjectPickUC";
export function makeTrySubmitObjectPickUC(appObject) {
    return new TrySubmitObjectPickUCImp(appObject);
}
class TrySubmitObjectPickUCImp extends TrySubmitObjectPickUC {
    get baseApp() {
        return this.getCachedSingleton(BaseAppState.type);
    }
    get challenge() {
        return this.getCachedSingleton(ChallengeEntity.type);
    }
    get objectPick() {
        return this.getCachedSingleton(ObjectPickEntity.type);
    }
    get pixelToRay() {
        return this.getCachedSingleton(PixelToSceneRayUC.type)
            ?.pixelToSceneRay;
    }
    constructor(appObject) {
        super(appObject, TrySubmitObjectPickUC.type);
        this.tryWithPixel = (pixel) => {
            if (!this.pixelToRay) {
                this.error("Missing component");
                return;
            }
            const ray = this.pixelToRay(pixel);
            return this.tryWithRay(ray);
        };
        this.tryWithRay = (ray) => {
            if (!this.baseApp || !this.challenge || !this.objectPick)
                return;
            if (this.baseApp.isAuthoring)
                return;
            if (this.baseApp.isReviewing)
                return;
            if (this.challenge.selectedChallenge !== "OBJECT_PICK")
                return;
            const sceneObjectId = SceneRayPickUC.get(this.appObjects)?.sceneRayPick(ray)
                ?.appObjectID;
            if (!sceneObjectId) {
                return;
            }
            this.objectPick.submit(sceneObjectId);
            const result = {
                description: this.baseApp.slideText,
                success: this.objectPick.hasSubmitted,
            };
            submitHitResult(result, this.appObjects);
            this.baseApp.isReviewing = true;
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=TrySubmitObjectPickUC.js.map