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 HoverAppObjectUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(HoverAppObjectUC.type, appObjects);
    }
}
HoverAppObjectUC.type = "HoverAppObjectUC";
export function makeHoverAppObject(appObject) {
    return new HoverAppObjectUCImp(appObject);
}
class HoverAppObjectUCImp extends HoverAppObjectUC {
    get selection() {
        return this.getCachedSingleton(Selection.type);
    }
    get baseApp() {
        return this.getCachedSingleton(BaseAppState.type);
    }
    constructor(appObject) {
        super(appObject, HoverAppObjectUC.type);
        this.allowHover = () => {
            if (!this.baseApp) {
                return false;
            }
            return this.baseApp.isAuthoring;
        };
        this.allowTryHover = () => {
            if (!this.baseApp) {
                return false;
            }
            return this.baseApp.isAuthoring;
        };
        this.tryHoverAppObject = (ray) => {
            if (!this.selection)
                return;
            if (!this.allowTryHover())
                return;
            const rayPick = sceneRayPick(ray, this.appObjects);
            if (!rayPick?.appObjectID) {
                this.selection.hoveredObjectId = null;
                return;
            }
            const selectable = this.appObjects.getAppObjectComponent(rayPick.appObjectID, Selectable.type);
            if (!selectable) {
                this.selection.hoveredObjectId = null;
                return;
            }
            this.selection.hoveredObjectId = rayPick.appObjectID;
        };
        this.hoverAppObject = (id, isHovered) => {
            if (!this.selection)
                return;
            if (!this.allowHover())
                return;
            if (isHovered) {
                this.selection.hoveredObjectId = id;
            }
            else if (id === this.selection.hoveredObjectId) {
                this.selection.hoveredObjectId = null;
            }
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=HoverAppObjectUC.js.map