import { AppObjectEntity, getSingletonComponent, MemoizedBoolean, Vector2, Vector3 } from "@vived/core";
import { Transform3D } from "../../Transform3D";
export var HoverLabelMode;
(function (HoverLabelMode) {
    HoverLabelMode[HoverLabelMode["NONE"] = 0] = "NONE";
    HoverLabelMode[HoverLabelMode["SCREEN"] = 1] = "SCREEN";
    HoverLabelMode[HoverLabelMode["WORLD"] = 2] = "WORLD";
})(HoverLabelMode || (HoverLabelMode = {}));
export class HoverLabelEntity extends AppObjectEntity {
    static get(appObjects) {
        return getSingletonComponent(HoverLabelEntity.type, appObjects);
    }
}
HoverLabelEntity.type = "HoverLabelEntity";
export function makeHoverLabelEntity(appObject) {
    Transform3D.addIfMissing(appObject);
    return new HoverLabelEntityImp(appObject);
}
class HoverLabelEntityImp extends HoverLabelEntity {
    get mode() {
        return this._mode;
    }
    get pixelPosition() {
        return this._pixelPosition;
    }
    get show() {
        return this._show;
    }
    get label() {
        return this._label;
    }
    get allowHoverLabel() {
        return this.memoizedAllow.val;
    }
    set allowHoverLabel(allow) {
        if (allow === this.memoizedAllow.val) {
            return;
        }
        this.memoizedAllow.val = allow;
        if (allow === false) {
            this.hideHoverLabel();
        }
    }
    constructor(appObject) {
        super(appObject, HoverLabelEntity.type);
        this._mode = HoverLabelMode.NONE;
        this._pixelPosition = Vector2.Zero();
        this._show = false;
        this._label = "";
        this.memoizedAllow = new MemoizedBoolean(true, this.notifyOnChange);
        this.showScreenHoverLabel = (label, pixelPosition) => {
            if (!this.allowHoverLabel) {
                return;
            }
            if (this.mode === HoverLabelMode.SCREEN &&
                label === this._label &&
                Vector2.Equal(pixelPosition, this._pixelPosition) &&
                this.show) {
                return;
            }
            this._label = label;
            this._pixelPosition = pixelPosition;
            this._show = true;
            this._mode = HoverLabelMode.SCREEN;
            this.notifyOnChange();
        };
        this.showWorldHoverLabel = (label, worldPosition) => {
            if (!this.allowHoverLabel) {
                return;
            }
            if (this.mode === HoverLabelMode.WORLD &&
                label === this._label &&
                Vector3.Equal(worldPosition, this.transform.worldPosition) &&
                this.show) {
                return;
            }
            this._label = label;
            this.transform.worldPosition = worldPosition;
            this._show = true;
            this._mode = HoverLabelMode.WORLD;
            this.notifyOnChange();
        };
        this.hideHoverLabel = () => {
            if (!this._show)
                return;
            this._show = false;
            this._label = "";
            this._pixelPosition = Vector2.Zero();
            this.transform.localPosition = Vector3.Zero();
            this._mode = HoverLabelMode.NONE;
            this.notifyOnChange();
        };
        this.transform = Transform3D.addIfMissing(appObject);
    }
}
//# sourceMappingURL=HoverLabelEntity.js.map