"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrySelectObjectPickUC = void 0;
exports.makeTrySelectObjectPickUC = makeTrySelectObjectPickUC;
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 DispatchStateChangeUC_1 = require("../../Dispatcher/UC/DispatchStateChangeUC");
const UndoRedo_1 = require("../../UndoRedo");
const ObjectPickEntity_1 = require("../Entities/ObjectPickEntity");
class TrySelectObjectPickUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(TrySelectObjectPickUC.type, appObjects);
    }
}
exports.TrySelectObjectPickUC = TrySelectObjectPickUC;
TrySelectObjectPickUC.type = "TrySelectObjectPickUC";
function makeTrySelectObjectPickUC(appObject) {
    return new TrySelectObjectPickUCImp(appObject);
}
class TrySelectObjectPickUCImp extends TrySelectObjectPickUC {
    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;
    }
    get undo() {
        return this.getCachedSingleton(UndoRedo_1.UndoRedoEntity.type);
    }
    constructor(appObject) {
        super(appObject, TrySelectObjectPickUC.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)
                return;
            if (!this.objectPick)
                return;
            if (!this.baseApp.isAuthoring) {
                return;
            }
            if (!this.objectPick.isSelectingCorrectObjects) {
                return;
            }
            if (this.challenge?.selectedChallenge !== this.objectPick.challengeID) {
                return;
            }
            const appObjectID = UC_1.SceneRayPickUC.get(this.appObjects)?.sceneRayPick(ray)
                ?.appObjectID;
            if (!appObjectID) {
                return;
            }
            this.objectPick.addCorrectObjectId(appObjectID);
            (0, DispatchStateChangeUC_1.dispatchStateChange)(this.appObjects);
            const objectPick = this.objectPick;
            this.undo?.submitAction({
                redoAction: () => {
                    objectPick.addCorrectObjectId(appObjectID);
                    (0, DispatchStateChangeUC_1.dispatchStateChange)(this.appObjects);
                },
                undoAction: () => {
                    objectPick.removeCorrectObjectId(appObjectID);
                    (0, DispatchStateChangeUC_1.dispatchStateChange)(this.appObjects);
                },
            });
        };
    }
}
//# sourceMappingURL=TrySelectObjectPickUC.js.map