"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PressAppObjectUC = void 0;
exports.makePressAppObject = makePressAppObject;
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 PressAppObjectUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(PressAppObjectUC.type, appObjects);
    }
}
exports.PressAppObjectUC = PressAppObjectUC;
PressAppObjectUC.type = "PressAppObjectUC";
function makePressAppObject(appObject) {
    return new PressAppObjectUCImp(appObject);
}
class PressAppObjectUCImp extends PressAppObjectUC {
    get selection() {
        return this.getCachedSingleton(Selection_1.Selection.type);
    }
    get baseApp() {
        return this.getCachedSingleton(BaseApp_1.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 = (0, Babylon_1.sceneRayPick)(ray, this.appObjects);
            if (!rayPick?.appObjectID) {
                this.selection.pressedObjectId = null;
                return this.selection.pressedObjectId;
            }
            const selectable = this.appObjects.getAppObjectComponent(rayPick.appObjectID, Selectable_1.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