//#region src/components/context-menu/index.ts
var e = class e {
	menuEl;
	items;
	activeIndex = -1;
	closed = !1;
	constructor(e, t, n) {
		this.items = n.items, this.menuEl = document.createElement("div"), this.menuEl.className = "cmm-context-menu", this.menuEl.style.position = "fixed", this.menuEl.style.left = `${String(e)}px`, this.menuEl.style.top = `${String(t)}px`, this.menuEl.setAttribute("tabindex", "-1"), this.renderItems(), document.body.appendChild(this.menuEl), this.adjustPosition(e, t), document.addEventListener("click", this.handleDocumentClick), document.addEventListener("keydown", this.handleKeyDown), this.menuEl.focus();
	}
	static show(t, n, r) {
		return new e(t, n, r);
	}
	close() {
		this.closed || (this.closed = !0, document.removeEventListener("click", this.handleDocumentClick), document.removeEventListener("keydown", this.handleKeyDown), this.menuEl.parentNode && this.menuEl.parentNode.removeChild(this.menuEl));
	}
	renderItems() {
		this.menuEl.innerHTML = "";
		let e = 0;
		for (let t of this.items) {
			if (t.separator === !0) {
				let e = document.createElement("div");
				e.className = "cmm-context-menu__separator", this.menuEl.appendChild(e);
				continue;
			}
			let n = document.createElement("div");
			if (n.className = "cmm-context-menu__item", t.disabled === !0 && n.classList.add("cmm-context-menu__item--disabled"), e === this.activeIndex && n.classList.add("cmm-context-menu__item--active"), n.dataset.index = String(e), t.icon) {
				let e = document.createElement("span");
				e.className = "cmm-context-menu__icon", e.textContent = t.icon, n.appendChild(e);
			}
			let r = document.createElement("span");
			if (r.textContent = t.label, n.appendChild(r), t.shortcut) {
				let e = document.createElement("span");
				e.className = "cmm-context-menu__shortcut", e.textContent = t.shortcut, n.appendChild(e);
			}
			t.disabled !== !0 && n.addEventListener("click", (e) => {
				e.stopPropagation(), t.handler && t.handler(), this.close();
			}), this.menuEl.appendChild(n), e += 1;
		}
	}
	adjustPosition(e, t) {
		let n = this.menuEl.getBoundingClientRect(), r = window.innerWidth, i = window.innerHeight, a = e, o = t;
		e + n.width > r && (a = r - n.width), t + n.height > i && (o = i - n.height), a < 0 && (a = 0), o < 0 && (o = 0), this.menuEl.style.left = `${String(a)}px`, this.menuEl.style.top = `${String(o)}px`;
	}
	getNavigableItems() {
		return this.items.filter((e) => e.separator !== !0);
	}
	handleDocumentClick = (e) => {
		let t = e.target;
		t instanceof Node && !this.menuEl.contains(t) && this.close();
	};
	handleKeyDown = (e) => {
		let t = this.getNavigableItems();
		if (e.key === "ArrowDown") e.preventDefault(), this.activeIndex = Math.min(this.activeIndex + 1, t.length - 1), this.renderItems();
		else if (e.key === "ArrowUp") e.preventDefault(), this.activeIndex = Math.max(this.activeIndex - 1, 0), this.renderItems();
		else if (e.key === "Enter") {
			if (e.preventDefault(), this.activeIndex >= 0) {
				let e = t[this.activeIndex];
				e && e.disabled !== !0 && e.handler && e.handler(), this.close();
			}
		} else e.key === "Escape" && (e.preventDefault(), this.close());
	};
};
//#endregion
export { e as ContextMenu };

//# sourceMappingURL=index.js.map