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 { dispatchStateChange } from "../../Dispatcher/UC/DispatchStateChangeUC";
import { UndoRedoEntity } from "../../UndoRedo";
import { ObjectPickEntity } from "../Entities/ObjectPickEntity";
export class TrySelectObjectPickUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(TrySelectObjectPickUC.type, appObjects);
    }
}
TrySelectObjectPickUC.type = "TrySelectObjectPickUC";
export function makeTrySelectObjectPickUC(appObject) {
    return new TrySelectObjectPickUCImp(appObject);
}
class TrySelectObjectPickUCImp extends TrySelectObjectPickUC {
    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;
    }
    get undo() {
        return this.getCachedSingleton(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 = SceneRayPickUC.get(this.appObjects)?.sceneRayPick(ray)
                ?.appObjectID;
            if (!appObjectID) {
                return;
            }
            this.objectPick.addCorrectObjectId(appObjectID);
            dispatchStateChange(this.appObjects);
            const objectPick = this.objectPick;
            this.undo?.submitAction({
                redoAction: () => {
                    objectPick.addCorrectObjectId(appObjectID);
                    dispatchStateChange(this.appObjects);
                },
                undoAction: () => {
                    objectPick.removeCorrectObjectId(appObjectID);
                    dispatchStateChange(this.appObjects);
                },
            });
        };
    }
}
//# sourceMappingURL=TrySelectObjectPickUC.js.map