"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TryShowHoverLabel = void 0;
exports.makeTryShowHoverLabel = makeTryShowHoverLabel;
const core_1 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const Callouts_1 = require("../../Callouts");
const Label_1 = require("../../Label");
const HoverLabelEntity_1 = require("../Entities/HoverLabelEntity");
class TryShowHoverLabel extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(TryShowHoverLabel.type, appObjects);
    }
}
exports.TryShowHoverLabel = TryShowHoverLabel;
TryShowHoverLabel.type = "TryShowHoverLabel";
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 = (0, Babylon_1.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 = (0, Babylon_1.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_1.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 = Callouts_1.CalloutEntity.get(appObj);
        if (calloutEntity) {
            return;
        }
        const label = Label_1.LabelEntity.get(appObj);
        if (!label) {
            return;
        }
        if (label.display) {
            return label.display;
        }
    }
}
//# sourceMappingURL=TryShowHoverLabel.js.map