import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { scenePixelPick, sceneRayPick } from "../../Babylon";
import { CalloutEntity } from "../../Callouts";
import { LabelEntity } from "../../Label";
import { HoverLabelEntity } from "../Entities/HoverLabelEntity";
export class TryShowHoverLabel extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(TryShowHoverLabel.type, appObjects);
    }
}
TryShowHoverLabel.type = "TryShowHoverLabel";
export function makeTryShowHoverLabel(appObject) {
    return new TryShowHoverLabelIMP(appObject);
}
class TryShowHoverLabelIMP extends TryShowHoverLabel {
    constructor(appObject) {
        super(appObject, TryShowHoverLabel.type);
        this.tryShowScreenHoverLabel = (position) => {
            if (!this.hoverLabelEntity)
                return;
            const appObjectID = scenePixelPick(position, this.appObjects)?.appObjectID;
            const displayLabel = this.getDisplayLabelForAppObjectID(appObjectID);
            if (displayLabel) {
                this.hoverLabelEntity.showScreenHoverLabel(displayLabel, position);
            }
            else {
                this.hoverLabelEntity.hideHoverLabel();
            }
        };
        this.tryShowWorldHoverLabel = (ray) => {
            if (!this.hoverLabelEntity)
                return;
            const pickHit = sceneRayPick(ray, this.appObjects);
            const appObjectID = pickHit?.appObjectID;
            const displayLabel = this.getDisplayLabelForAppObjectID(appObjectID);
            if (displayLabel && pickHit?.worldPickPoint) {
                this.hoverLabelEntity.showWorldHoverLabel(displayLabel, pickHit.worldPickPoint);
            }
            else {
                this.hoverLabelEntity.hideHoverLabel();
            }
        };
        this.hoverLabelEntity = this.appObject.getComponent(HoverLabelEntity.type);
        if (!this.hoverLabelEntity) {
            this.warn("PM has been added to an App Object that does not have a HoverLabelEntity. Add the Entity first");
        }
        this.appObjects.registerSingleton(this);
    }
    getDisplayLabelForAppObjectID(appObjectID) {
        if (!appObjectID)
            return;
        if (!this.hoverLabelEntity)
            return;
        if (!this.hoverLabelEntity.allowHoverLabel) {
            return;
        }
        const appObj = this.appObjects.get(appObjectID);
        if (!appObj) {
            return;
        }
        const calloutEntity = CalloutEntity.get(appObj);
        if (calloutEntity) {
            return;
        }
        const label = LabelEntity.get(appObj);
        if (!label) {
            return;
        }
        if (label.display) {
            return label.display;
        }
    }
}
//# sourceMappingURL=TryShowHoverLabel.js.map