"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrySubmitObjectPickUC = void 0;
exports.makeTrySubmitObjectPickUC = makeTrySubmitObjectPickUC;
const core_1 = require("@vived/core");
const BaseAppState_1 = require("../../BaseApp/Entity/BaseAppState");
const UC_1 = require("../../Babylon/UC");
const ChallengeEntity_1 = require("../../Challenge/Entities/ChallengeEntity");
const SubmitResultResultUC_1 = require("../../Dispatcher/UC/SubmitResultResultUC");
const ObjectPickEntity_1 = require("../Entities/ObjectPickEntity");
class TrySubmitObjectPickUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(TrySubmitObjectPickUC.type, appObjects);
    }
}
exports.TrySubmitObjectPickUC = TrySubmitObjectPickUC;
TrySubmitObjectPickUC.type = "TrySubmitObjectPickUC";
function makeTrySubmitObjectPickUC(appObject) {
    return new TrySubmitObjectPickUCImp(appObject);
}
class TrySubmitObjectPickUCImp extends TrySubmitObjectPickUC {
    get baseApp() {
        return this.getCachedSingleton(BaseAppState_1.BaseAppState.type);
    }
    get challenge() {
        return this.getCachedSingleton(ChallengeEntity_1.ChallengeEntity.type);
    }
    get objectPick() {
        return this.getCachedSingleton(ObjectPickEntity_1.ObjectPickEntity.type);
    }
    get pixelToRay() {
        return this.getCachedSingleton(UC_1.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 = UC_1.SceneRayPickUC.get(this.appObjects)?.sceneRayPick(ray)
                ?.appObjectID;
            if (!sceneObjectId) {
                return;
            }
            this.objectPick.submit(sceneObjectId);
            const result = {
                description: this.baseApp.slideText,
                success: this.objectPick.hasSubmitted,
            };
            (0, SubmitResultResultUC_1.submitHitResult)(result, this.appObjects);
            this.baseApp.isReviewing = true;
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=TrySubmitObjectPickUC.js.map