import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { sceneRayPick } from "../../Babylon";
import { BaseAppState } from "../../BaseApp";
import { ToolRepository } from "../../Tools";
import { Selectable } from "../Entities/Selectable";
import { Selection } from "../Entities/Selection";
export class SelectAppObjectUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(SelectAppObjectUC.type, appObjects);
    }
}
SelectAppObjectUC.type = "SelectAppObjectUC";
export function makeSelectAppObject(appObject) {
    return new SelectAppObjectUCImp(appObject);
}
class SelectAppObjectUCImp extends SelectAppObjectUC {
    get selection() {
        return this.getCachedSingleton(Selection.type);
    }
    get baseApp() {
        return this.getCachedSingleton(BaseAppState.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(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 = sceneRayPick(ray, this.appObjects);
            if (!rayPick?.appObjectID) {
                this.selection.selectedObjectId = null;
                return;
            }
            const selectable = this.appObjects.getAppObjectComponent(rayPick.appObjectID, 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