import { n as e } from "../../utils-C_mWfcFX.js";
//#region src/display/tooltip/index.ts
function t(e) {
	switch (e) {
		case "top": return "bottom";
		case "bottom": return "top";
		case "left": return "right";
		case "right": return "left";
	}
}
var n = 8, r = class {
	trigger;
	placement;
	delay;
	className;
	tooltipId;
	tooltipEl = null;
	arrowEl = null;
	showTimeoutId = null;
	content;
	destroyed = !1;
	handleMouseEnter;
	handleMouseLeave;
	handleFocusIn;
	handleFocusOut;
	constructor(t) {
		this.trigger = t.trigger, this.content = t.content, this.placement = t.placement ?? "top", this.delay = t.delay ?? 200, this.className = t.className, this.tooltipId = e("cmm-tooltip"), this.handleMouseEnter = () => {
			this.scheduleShow();
		}, this.handleMouseLeave = () => {
			this.hide();
		}, this.handleFocusIn = () => {
			this.scheduleShow();
		}, this.handleFocusOut = () => {
			this.hide();
		}, this.trigger.addEventListener("mouseenter", this.handleMouseEnter), this.trigger.addEventListener("mouseleave", this.handleMouseLeave), this.trigger.addEventListener("focusin", this.handleFocusIn), this.trigger.addEventListener("focusout", this.handleFocusOut);
	}
	setContent(e) {
		if (this.content = e, this.tooltipEl) {
			let t = this.tooltipEl.firstChild;
			t && (t.textContent = e);
		}
	}
	show() {
		this.destroyed || (this.cancelScheduledShow(), this.createTooltipElement(), this.positionTooltip(), this.tooltipEl && (this.tooltipEl.offsetHeight, this.tooltipEl.classList.add("cmm-tooltip--visible")));
	}
	hide() {
		if (this.cancelScheduledShow(), this.tooltipEl) {
			this.tooltipEl.classList.remove("cmm-tooltip--visible");
			let e = this.tooltipEl;
			setTimeout(() => {
				e.remove();
			}, 150), this.tooltipEl = null, this.arrowEl = null, this.trigger.removeAttribute("aria-describedby");
		}
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.cancelScheduledShow(), this.hide(), this.trigger.removeEventListener("mouseenter", this.handleMouseEnter), this.trigger.removeEventListener("mouseleave", this.handleMouseLeave), this.trigger.removeEventListener("focusin", this.handleFocusIn), this.trigger.removeEventListener("focusout", this.handleFocusOut), this.trigger.removeAttribute("aria-describedby"));
	}
	scheduleShow() {
		this.destroyed || (this.cancelScheduledShow(), this.delay > 0 ? this.showTimeoutId = setTimeout(() => {
			this.show();
		}, this.delay) : this.show());
	}
	cancelScheduledShow() {
		this.showTimeoutId !== null && (clearTimeout(this.showTimeoutId), this.showTimeoutId = null);
	}
	createTooltipElement() {
		this.tooltipEl && this.tooltipEl.remove();
		let e = document.createElement("div");
		e.className = "cmm-tooltip", e.id = this.tooltipId, e.setAttribute("role", "tooltip"), e.textContent = this.content, this.className && e.classList.add(this.className);
		let t = document.createElement("div");
		t.className = "cmm-tooltip__arrow", e.appendChild(t), document.body.appendChild(e), this.tooltipEl = e, this.arrowEl = t, this.trigger.setAttribute("aria-describedby", this.tooltipId);
	}
	positionTooltip() {
		if (!this.tooltipEl || !this.arrowEl) return;
		let e = this.trigger.getBoundingClientRect(), r = this.tooltipEl.getBoundingClientRect(), i = window.innerWidth, a = window.innerHeight, o = this.placement;
		(o === "top" && e.top - r.height - n < 0 || o === "bottom" && e.bottom + r.height + n > a || o === "left" && e.left - r.width - n < 0 || o === "right" && e.right + r.width + n > i) && (o = t(o));
		let s = 0, c = 0;
		switch (o) {
			case "top":
				s = e.top - r.height - n, c = e.left + (e.width - r.width) / 2, this.arrowEl.className = "cmm-tooltip__arrow cmm-tooltip__arrow--bottom";
				break;
			case "bottom":
				s = e.bottom + n, c = e.left + (e.width - r.width) / 2, this.arrowEl.className = "cmm-tooltip__arrow cmm-tooltip__arrow--top";
				break;
			case "left":
				s = e.top + (e.height - r.height) / 2, c = e.left - r.width - n, this.arrowEl.className = "cmm-tooltip__arrow cmm-tooltip__arrow--right";
				break;
			case "right":
				s = e.top + (e.height - r.height) / 2, c = e.right + n, this.arrowEl.className = "cmm-tooltip__arrow cmm-tooltip__arrow--left";
				break;
		}
		c = Math.max(4, Math.min(c, i - r.width - 4)), s = Math.max(4, Math.min(s, a - r.height - 4)), this.tooltipEl.style.left = `${String(c)}px`, this.tooltipEl.style.top = `${String(s)}px`;
	}
};
//#endregion
export { r as Tooltip };

//# sourceMappingURL=index.js.map