import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { sceneRayPick } from "../../Babylon";
import { BaseAppState } from "../../BaseApp";
import { Selectable } from "../Entities/Selectable";
import { Selection } from "../Entities/Selection";
export class PressAppObjectUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(PressAppObjectUC.type, appObjects);
    }
}
PressAppObjectUC.type = "PressAppObjectUC";
export function makePressAppObject(appObject) {
    return new PressAppObjectUCImp(appObject);
}
class PressAppObjectUCImp extends PressAppObjectUC {
    get selection() {
        return this.getCachedSingleton(Selection.type);
    }
    get baseApp() {
        return this.getCachedSingleton(BaseAppState.type);
    }
    constructor(appObject) {
        super(appObject, PressAppObjectUC.type);
        this.allowPress = () => {
            if (!this.baseApp) {
                return false;
            }
            return this.baseApp.isAuthoring;
        };
        this.allowTryPress = () => {
            if (!this.baseApp) {
                return false;
            }
            return this.baseApp.isAuthoring;
        };
        this.tryPressAppObject = (ray) => {
            if (!this.selection)
                return null;
            if (!this.allowTryPress())
                return null;
            const rayPick = sceneRayPick(ray, this.appObjects);
            if (!rayPick?.appObjectID) {
                this.selection.pressedObjectId = null;
                return this.selection.pressedObjectId;
            }
            const selectable = this.appObjects.getAppObjectComponent(rayPick.appObjectID, Selectable.type);
            if (!selectable) {
                this.selection.pressedObjectId = null;
                return this.selection.pressedObjectId;
            }
            this.selection.pressedObjectId = rayPick.appObjectID;
            return this.selection.pressedObjectId;
        };
        this.pressAppObject = (id, isPressed) => {
            if (!this.selection)
                return;
            if (!this.allowPress())
                return;
            if (isPressed) {
                this.selection.pressedObjectId = id;
            }
            else if (id === this.selection.pressedObjectId) {
                this.selection.pressedObjectId = null;
            }
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=PressAppObjectUC.js.map