import { t as e } from "../../purify.es-Dsqc2y0I.js";
import { n as t } from "../../utils-C_mWfcFX.js";
//#region src/display/popover/index.ts
function n(e) {
	let t = [
		"a[href]",
		"button:not([disabled])",
		"input:not([disabled])",
		"select:not([disabled])",
		"textarea:not([disabled])",
		"[tabindex]:not([tabindex=\"-1\"])"
	].join(", ");
	return Array.from(e.querySelectorAll(t));
}
var r = 8, i = class {
	trigger;
	placement;
	closeOnClickOutside;
	closeOnEscape;
	className;
	onOpenCallback;
	onCloseCallback;
	popoverId;
	popoverEl = null;
	contentEl = null;
	content;
	opened = !1;
	destroyed = !1;
	handleTriggerClick;
	handleDocumentClick;
	handleKeyDown;
	constructor(e) {
		this.trigger = e.trigger, this.content = e.content, this.placement = e.placement ?? "bottom", this.closeOnClickOutside = e.closeOnClickOutside !== !1, this.closeOnEscape = e.closeOnEscape !== !1, this.className = e.className, this.onOpenCallback = e.onOpen, this.onCloseCallback = e.onClose, this.popoverId = t("cmm-popover"), this.handleTriggerClick = () => {
			this.toggle();
		}, this.handleDocumentClick = (e) => {
			let t = e.target;
			t instanceof Node && this.popoverEl && !this.popoverEl.contains(t) && !this.trigger.contains(t) && this.close();
		}, this.handleKeyDown = (e) => {
			e.key === "Escape" && (e.preventDefault(), this.close(), this.trigger.focus());
		}, this.trigger.setAttribute("aria-haspopup", "dialog"), this.trigger.setAttribute("aria-expanded", "false"), this.trigger.addEventListener("click", this.handleTriggerClick);
	}
	isOpen() {
		return this.opened;
	}
	open() {
		if (!(this.destroyed || this.opened)) {
			if (this.opened = !0, this.createPopoverElement(), this.positionPopover(), this.popoverEl && (this.popoverEl.offsetHeight, this.popoverEl.classList.add("cmm-popover--open")), this.trigger.setAttribute("aria-expanded", "true"), this.closeOnClickOutside && setTimeout(() => {
				document.addEventListener("click", this.handleDocumentClick);
			}, 0), this.closeOnEscape && document.addEventListener("keydown", this.handleKeyDown), this.popoverEl) {
				let e = n(this.popoverEl)[0];
				e ? e.focus() : (this.popoverEl.setAttribute("tabindex", "-1"), this.popoverEl.focus());
			}
			this.onOpenCallback?.();
		}
	}
	close() {
		if (this.opened) {
			if (this.opened = !1, this.trigger.setAttribute("aria-expanded", "false"), document.removeEventListener("click", this.handleDocumentClick), document.removeEventListener("keydown", this.handleKeyDown), this.popoverEl) {
				this.popoverEl.classList.remove("cmm-popover--open");
				let e = this.popoverEl;
				setTimeout(() => {
					e.remove();
				}, 200), this.popoverEl = null, this.contentEl = null;
			}
			this.onCloseCallback?.();
		}
	}
	toggle() {
		this.opened ? this.close() : this.open();
	}
	setContent(e) {
		this.content = e, this.contentEl && this.renderContent();
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.close(), this.trigger.removeEventListener("click", this.handleTriggerClick), this.trigger.removeAttribute("aria-haspopup"), this.trigger.removeAttribute("aria-expanded"));
	}
	createPopoverElement() {
		this.popoverEl && this.popoverEl.remove();
		let e = document.createElement("div");
		e.className = "cmm-popover", e.id = this.popoverId, this.className && e.classList.add(this.className);
		let t = document.createElement("div");
		t.className = "cmm-popover__content", e.appendChild(t), document.body.appendChild(e), this.popoverEl = e, this.contentEl = t, this.renderContent();
	}
	renderContent() {
		this.contentEl && (this.contentEl.innerHTML = "", typeof this.content == "string" ? this.contentEl.innerHTML = e.sanitize(this.content) : this.contentEl.appendChild(this.content));
	}
	positionPopover() {
		if (!this.popoverEl) return;
		let e = this.trigger.getBoundingClientRect(), t = this.popoverEl.getBoundingClientRect(), n = window.innerWidth, i = window.innerHeight, a = 0, o = 0, s = this.placement.split("-")[0], c = this.placement.includes("-") ? this.placement.split("-")[1] : void 0, l = s ?? "bottom";
		switch (l) {
			case "top":
				a = e.top - t.height - r, o = this.getAlignedLeft(e, t, c);
				break;
			case "bottom":
				a = e.bottom + r, o = this.getAlignedLeft(e, t, c);
				break;
			case "left":
				a = e.top + (e.height - t.height) / 2, o = e.left - t.width - r;
				break;
			case "right":
				a = e.top + (e.height - t.height) / 2, o = e.right + r;
				break;
		}
		l === "top" && a < 0 ? a = e.bottom + r : l === "bottom" && a + t.height > i && (a = e.top - t.height - r), o = Math.max(4, Math.min(o, n - t.width - 4)), a = Math.max(4, Math.min(a, i - t.height - 4)), this.popoverEl.style.left = `${String(o)}px`, this.popoverEl.style.top = `${String(a)}px`;
	}
	getAlignedLeft(e, t, n) {
		return n === "start" ? e.left : n === "end" ? e.right - t.width : e.left + (e.width - t.width) / 2;
	}
};
//#endregion
export { i as Popover };

//# sourceMappingURL=index.js.map