import { t as e } from "../../purify.es-Dsqc2y0I.js";
import { n as t } from "../../utils-C_mWfcFX.js";
//#region src/display/hover-card/index.ts
function n(e) {
	switch (e) {
		case "top": return "bottom";
		case "bottom": return "top";
		case "left": return "right";
		case "right": return "left";
	}
}
var r = 8, i = class {
	trigger;
	placement;
	openDelay;
	closeDelay;
	className;
	cardId;
	cardEl = null;
	contentEl = null;
	arrowEl = null;
	content;
	showTimeoutId = null;
	hideTimeoutId = null;
	destroyed = !1;
	handleTriggerMouseEnter;
	handleTriggerMouseLeave;
	handleCardMouseEnter;
	handleCardMouseLeave;
	constructor(e) {
		this.trigger = e.trigger, this.content = e.content, this.placement = e.placement ?? "bottom", this.openDelay = e.openDelay ?? 300, this.closeDelay = e.closeDelay ?? 200, this.className = e.className, this.cardId = t("cmm-hover-card"), this.handleTriggerMouseEnter = () => {
			this.scheduleShow();
		}, this.handleTriggerMouseLeave = () => {
			this.scheduleHide();
		}, this.handleCardMouseEnter = () => {
			this.cancelScheduledHide();
		}, this.handleCardMouseLeave = () => {
			this.scheduleHide();
		}, this.trigger.setAttribute("aria-haspopup", "dialog"), this.trigger.addEventListener("mouseenter", this.handleTriggerMouseEnter), this.trigger.addEventListener("mouseleave", this.handleTriggerMouseLeave);
	}
	setContent(e) {
		this.content = e, this.contentEl && this.renderContent();
	}
	show() {
		this.destroyed || (this.cancelScheduledShow(), this.cancelScheduledHide(), this.createCardElement(), this.positionCard(), this.cardEl && (this.cardEl.offsetHeight, this.cardEl.classList.add("cmm-hover-card--visible")));
	}
	hide() {
		if (this.cancelScheduledShow(), this.cancelScheduledHide(), this.cardEl) {
			this.cardEl.classList.remove("cmm-hover-card--visible"), this.cardEl.removeEventListener("mouseenter", this.handleCardMouseEnter), this.cardEl.removeEventListener("mouseleave", this.handleCardMouseLeave);
			let e = this.cardEl;
			setTimeout(() => {
				e.remove();
			}, 200), this.cardEl = null, this.contentEl = null, this.arrowEl = null;
		}
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.cancelScheduledShow(), this.cancelScheduledHide(), this.hide(), this.trigger.removeEventListener("mouseenter", this.handleTriggerMouseEnter), this.trigger.removeEventListener("mouseleave", this.handleTriggerMouseLeave), this.trigger.removeAttribute("aria-haspopup"));
	}
	scheduleShow() {
		this.destroyed || (this.cancelScheduledHide(), this.cancelScheduledShow(), this.openDelay > 0 ? this.showTimeoutId = setTimeout(() => {
			this.show();
		}, this.openDelay) : this.show());
	}
	scheduleHide() {
		this.cancelScheduledShow(), this.cancelScheduledHide(), this.closeDelay > 0 ? this.hideTimeoutId = setTimeout(() => {
			this.hide();
		}, this.closeDelay) : this.hide();
	}
	cancelScheduledShow() {
		this.showTimeoutId !== null && (clearTimeout(this.showTimeoutId), this.showTimeoutId = null);
	}
	cancelScheduledHide() {
		this.hideTimeoutId !== null && (clearTimeout(this.hideTimeoutId), this.hideTimeoutId = null);
	}
	createCardElement() {
		this.cardEl && (this.cardEl.removeEventListener("mouseenter", this.handleCardMouseEnter), this.cardEl.removeEventListener("mouseleave", this.handleCardMouseLeave), this.cardEl.remove());
		let e = document.createElement("div");
		e.className = "cmm-hover-card", e.id = this.cardId, e.setAttribute("role", "dialog"), this.className && e.classList.add(this.className);
		let t = document.createElement("div");
		t.className = "cmm-hover-card__content", e.appendChild(t);
		let n = document.createElement("div");
		n.className = "cmm-hover-card__arrow", e.appendChild(n), document.body.appendChild(e), this.cardEl = e, this.contentEl = t, this.arrowEl = n, this.renderContent(), this.cardEl.addEventListener("mouseenter", this.handleCardMouseEnter), this.cardEl.addEventListener("mouseleave", this.handleCardMouseLeave);
	}
	renderContent() {
		this.contentEl && (this.contentEl.innerHTML = "", typeof this.content == "string" ? this.contentEl.innerHTML = e.sanitize(this.content) : this.contentEl.appendChild(this.content));
	}
	positionCard() {
		if (!this.cardEl || !this.arrowEl) return;
		let e = this.trigger.getBoundingClientRect(), t = this.cardEl.getBoundingClientRect(), i = window.innerWidth, a = window.innerHeight, o = this.placement;
		(o === "top" && e.top - t.height - r < 0 || o === "bottom" && e.bottom + t.height + r > a || o === "left" && e.left - t.width - r < 0 || o === "right" && e.right + t.width + r > i) && (o = n(o));
		let s = 0, c = 0;
		switch (o) {
			case "top":
				s = e.top - t.height - r, c = e.left + (e.width - t.width) / 2, this.arrowEl.className = "cmm-hover-card__arrow cmm-hover-card__arrow--bottom";
				break;
			case "bottom":
				s = e.bottom + r, c = e.left + (e.width - t.width) / 2, this.arrowEl.className = "cmm-hover-card__arrow cmm-hover-card__arrow--top";
				break;
			case "left":
				s = e.top + (e.height - t.height) / 2, c = e.left - t.width - r, this.arrowEl.className = "cmm-hover-card__arrow cmm-hover-card__arrow--right";
				break;
			case "right":
				s = e.top + (e.height - t.height) / 2, c = e.right + r, this.arrowEl.className = "cmm-hover-card__arrow cmm-hover-card__arrow--left";
				break;
		}
		c = Math.max(4, Math.min(c, i - t.width - 4)), s = Math.max(4, Math.min(s, a - t.height - 4)), this.cardEl.style.left = `${String(c)}px`, this.cardEl.style.top = `${String(s)}px`;
	}
};
//#endregion
export { i as HoverCard };

//# sourceMappingURL=index.js.map