"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HoverAppObjectUC = void 0;
exports.makeHoverAppObject = makeHoverAppObject;
const core_1 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const BaseApp_1 = require("../../BaseApp");
const Selectable_1 = require("../Entities/Selectable");
const Selection_1 = require("../Entities/Selection");
class HoverAppObjectUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(HoverAppObjectUC.type, appObjects);
    }
}
exports.HoverAppObjectUC = HoverAppObjectUC;
HoverAppObjectUC.type = "HoverAppObjectUC";
function makeHoverAppObject(appObject) {
    return new HoverAppObjectUCImp(appObject);
}
class HoverAppObjectUCImp extends HoverAppObjectUC {
    get selection() {
        return this.getCachedSingleton(Selection_1.Selection.type);
    }
    get baseApp() {
        return this.getCachedSingleton(BaseApp_1.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 = (0, Babylon_1.sceneRayPick)(ray, this.appObjects);
            if (!rayPick?.appObjectID) {
                this.selection.hoveredObjectId = null;
                return;
            }
            const selectable = this.appObjects.getAppObjectComponent(rayPick.appObjectID, Selectable_1.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