"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectAppObjectUC = void 0;
exports.makeSelectAppObject = makeSelectAppObject;
const core_1 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const BaseApp_1 = require("../../BaseApp");
const Tools_1 = require("../../Tools");
const Selectable_1 = require("../Entities/Selectable");
const Selection_1 = require("../Entities/Selection");
class SelectAppObjectUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(SelectAppObjectUC.type, appObjects);
    }
}
exports.SelectAppObjectUC = SelectAppObjectUC;
SelectAppObjectUC.type = "SelectAppObjectUC";
function makeSelectAppObject(appObject) {
    return new SelectAppObjectUCImp(appObject);
}
class SelectAppObjectUCImp extends SelectAppObjectUC {
    get selection() {
        return this.getCachedSingleton(Selection_1.Selection.type);
    }
    get baseApp() {
        return this.getCachedSingleton(BaseApp_1.BaseAppState.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(Tools_1.ToolRepository.type);
    }
    constructor(appObject) {
        super(appObject, SelectAppObjectUC.type);
        this.allowSelect = () => {
            if (!this.baseApp) {
                return false;
            }
            return this.baseApp.isAuthoring;
        };
        this.allowTrySelect = () => {
            if (!this.baseApp) {
                return false;
            }
            if (this.toolRepo?.activeTool?.toolType === "CALLOUT") {
                return false;
            }
            if (this.toolRepo?.activeTool?.toolType === "LANDMARK") {
                return false;
            }
            return this.baseApp.isAuthoring;
        };
        this.trySelectAppObject = (ray) => {
            if (!this.selection)
                return;
            if (!this.allowTrySelect())
                return;
            const rayPick = (0, Babylon_1.sceneRayPick)(ray, this.appObjects);
            if (!rayPick?.appObjectID) {
                this.selection.selectedObjectId = null;
                return;
            }
            const selectable = this.appObjects.getAppObjectComponent(rayPick.appObjectID, Selectable_1.Selectable.type);
            if (!selectable) {
                this.selection.selectedObjectId = null;
                return;
            }
            this.selection.selectedObjectId = rayPick.appObjectID;
        };
        this.selectAppObject = (id, isSelected) => {
            if (!this.selection)
                return;
            if (!this.allowSelect())
                return;
            if (isSelected) {
                this.selection.selectedObjectId = id;
            }
            else if (id === this.selection.selectedObjectId) {
                this.selection.selectedObjectId = null;
            }
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=SelectAppObjectUC.js.map