import { i as e } from "../../sanitize-CDxuPfMB.js";
import { n as t, t as n } from "../../utils-C_mWfcFX.js";
import { n as r } from "../../a11y-CuHKgVSx.js";
//#region src/navigation/navigation-menu/index.ts
var i = 640, a = class {
	container;
	className;
	onNavigate;
	navId;
	items;
	navEl = null;
	menuListEl = null;
	hamburgerEl = null;
	openIndex = -1;
	dropdownEl = null;
	collapsed = !1;
	mobileOpen = !1;
	activeKeyIndex = -1;
	destroyed = !1;
	mobileFocusTrap = null;
	handleDocumentClick;
	handleKeyDown;
	handleResize;
	constructor(e) {
		this.container = n(e.container), this.items = [...e.items], this.className = e.className, this.onNavigate = e.onNavigate, this.navId = t("cmm-nav-menu"), this.handleDocumentClick = (e) => {
			let t = e.target;
			t instanceof Node && this.navEl && !this.navEl.contains(t) && this.dropdownEl?.contains(t) !== !0 && (this.closeDropdown(), this.mobileOpen && this.closeMobileMenu());
		}, this.handleKeyDown = (e) => {
			this.onKeyDown(e);
		}, this.handleResize = () => {
			this.checkCollapse();
		}, this.render(), this.checkCollapse(), document.addEventListener("click", this.handleDocumentClick), document.addEventListener("keydown", this.handleKeyDown), window.addEventListener("resize", this.handleResize);
	}
	update(e) {
		this.destroyed || (this.items = [...e], this.closeDropdown(), this.render(), this.checkCollapse());
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.closeDropdown(), document.removeEventListener("click", this.handleDocumentClick), document.removeEventListener("keydown", this.handleKeyDown), window.removeEventListener("resize", this.handleResize), this.container.innerHTML = "", this.navEl = null, this.menuListEl = null, this.hamburgerEl = null);
	}
	closeDropdown() {
		this.dropdownEl &&= (this.dropdownEl.remove(), null), this.openIndex = -1, this.updateTriggerStates();
	}
	openDropdown(e) {
		this.closeDropdown();
		let n = this.items[e];
		if (!n?.children || n.children.length === 0) return;
		this.openIndex = e, this.updateTriggerStates();
		let r = this.menuListEl?.querySelectorAll(".cmm-nav-menu__trigger")?.[e];
		if (!r) return;
		let i = r.getBoundingClientRect(), a = document.createElement("div");
		a.className = "cmm-nav-menu__dropdown", a.setAttribute("role", "menu"), a.id = t("cmm-nav-menu-dropdown");
		let o = document.createElement("div");
		o.className = "cmm-nav-menu__grid";
		for (let e of n.children) o.appendChild(this.buildChildCard(e));
		a.appendChild(o), document.body.appendChild(a), this.dropdownEl = a;
		let s = a.getBoundingClientRect(), c = i.left, l = i.bottom + 4;
		c + s.width > window.innerWidth && (c = window.innerWidth - s.width - 8), l + s.height > window.innerHeight && (l = i.top - s.height - 4), a.style.left = `${String(Math.max(0, c))}px`, a.style.top = `${String(Math.max(0, l))}px`;
	}
	buildChildCard(t) {
		let n = document.createElement("a");
		if (n.className = "cmm-nav-menu__card", n.href = t.href, n.setAttribute("role", "menuitem"), n.addEventListener("click", (e) => {
			this.onNavigate && (e.preventDefault(), this.onNavigate(t.href)), this.closeDropdown();
		}), t.icon) {
			let r = document.createElement("span");
			r.className = "cmm-nav-menu__card-icon", r.setAttribute("aria-hidden", "true"), e(r, t.icon), n.appendChild(r);
		}
		let r = document.createElement("div");
		r.className = "cmm-nav-menu__card-text";
		let i = document.createElement("div");
		if (i.className = "cmm-nav-menu__card-label", i.textContent = t.label, r.appendChild(i), t.description) {
			let e = document.createElement("div");
			e.className = "cmm-nav-menu__card-desc", e.textContent = t.description, r.appendChild(e);
		}
		return n.appendChild(r), n;
	}
	updateTriggerStates() {
		let e = this.menuListEl?.querySelectorAll(".cmm-nav-menu__trigger");
		e && e.forEach((e, t) => {
			let n = t === this.openIndex;
			e.setAttribute("aria-expanded", n ? "true" : "false"), n ? e.classList.add("cmm-nav-menu__trigger--active") : e.classList.remove("cmm-nav-menu__trigger--active");
		});
	}
	checkCollapse() {
		if (this.destroyed || !this.navEl) return;
		let e = this.container.getBoundingClientRect().width < i;
		e !== this.collapsed && (this.collapsed = e, this.hamburgerEl && (this.hamburgerEl.style.display = e ? "flex" : "none"), this.menuListEl && (e ? (this.menuListEl.classList.add("cmm-nav-menu__list--collapsed"), this.mobileOpen || (this.menuListEl.style.display = "none")) : (this.menuListEl.classList.remove("cmm-nav-menu__list--collapsed"), this.menuListEl.style.display = "", this.mobileOpen = !1)));
	}
	openMobileMenu() {
		this.mobileOpen = !0, this.menuListEl && (this.menuListEl.style.display = "", this.menuListEl.classList.add("cmm-nav-menu__list--mobile-open"), this.mobileFocusTrap === null ? this.mobileFocusTrap = r(this.menuListEl) : this.mobileFocusTrap.update(), this.mobileFocusTrap.activate()), this.hamburgerEl && this.hamburgerEl.setAttribute("aria-expanded", "true");
	}
	closeMobileMenu() {
		this.mobileOpen = !1, this.mobileFocusTrap !== null && this.mobileFocusTrap.deactivate(), this.menuListEl && this.collapsed && (this.menuListEl.style.display = "none", this.menuListEl.classList.remove("cmm-nav-menu__list--mobile-open")), this.hamburgerEl && this.hamburgerEl.setAttribute("aria-expanded", "false");
	}
	onKeyDown(e) {
		if (this.destroyed) return;
		let t = e.target;
		if (!(t instanceof Node && this.navEl && !this.navEl.contains(t) && this.dropdownEl?.contains(t) !== !0)) {
			if (e.key === "Escape") {
				e.preventDefault(), this.closeDropdown(), this.mobileOpen && this.closeMobileMenu();
				return;
			}
			if (e.key === "ArrowRight") {
				e.preventDefault(), this.activeKeyIndex = Math.min(this.activeKeyIndex + 1, this.items.length - 1), this.focusTrigger(this.activeKeyIndex);
				return;
			}
			if (e.key === "ArrowLeft") {
				e.preventDefault(), this.activeKeyIndex = Math.max(this.activeKeyIndex - 1, 0), this.focusTrigger(this.activeKeyIndex);
				return;
			}
			if (e.key === "ArrowDown") {
				if (e.preventDefault(), this.openIndex >= 0 && this.dropdownEl) {
					let e = this.dropdownEl.querySelector(".cmm-nav-menu__card");
					e && e.focus();
				} else this.activeKeyIndex >= 0 && this.openDropdown(this.activeKeyIndex);
				return;
			}
			if ((e.key === "Enter" || e.key === " ") && this.activeKeyIndex >= 0 && this.openIndex < 0) {
				e.preventDefault();
				let t = this.items[this.activeKeyIndex];
				t !== void 0 && t.children && t.children.length > 0 ? this.openDropdown(this.activeKeyIndex) : t !== void 0 && t.href && this.onNavigate && this.onNavigate(t.href);
			}
		}
	}
	focusTrigger(e) {
		let t = this.menuListEl?.querySelectorAll(".cmm-nav-menu__trigger")?.[e];
		t && t.focus();
	}
	render() {
		this.container.innerHTML = "";
		let t = document.createElement("nav");
		t.className = "cmm-nav-menu", t.id = this.navId, t.setAttribute("aria-label", "Main navigation"), this.className && t.classList.add(this.className);
		let n = document.createElement("button");
		n.className = "cmm-nav-menu__hamburger", n.type = "button", n.setAttribute("aria-label", "Toggle navigation"), n.setAttribute("aria-expanded", "false"), n.style.display = "none", n.innerHTML = "<span></span><span></span><span></span>", n.addEventListener("click", (e) => {
			e.stopPropagation(), this.mobileOpen ? this.closeMobileMenu() : this.openMobileMenu();
		}), t.appendChild(n), this.hamburgerEl = n;
		let r = document.createElement("div");
		r.className = "cmm-nav-menu__list", r.setAttribute("role", "menubar");
		for (let t = 0; t < this.items.length; t++) {
			let n = this.items[t];
			if (n) if (n.children !== void 0 && n.children.length > 0) {
				let i = document.createElement("button");
				if (i.className = "cmm-nav-menu__trigger", i.type = "button", i.setAttribute("aria-haspopup", "true"), i.setAttribute("aria-expanded", "false"), n.active === !0 && i.classList.add("cmm-nav-menu__trigger--current"), n.icon) {
					let t = document.createElement("span");
					t.className = "cmm-nav-menu__icon", t.setAttribute("aria-hidden", "true"), e(t, n.icon), i.appendChild(t);
				}
				let a = document.createElement("span");
				a.textContent = n.label, i.appendChild(a);
				let o = document.createElement("span");
				o.className = "cmm-nav-menu__chevron", o.setAttribute("aria-hidden", "true"), o.textContent = "▾", i.appendChild(o);
				let s = t;
				i.addEventListener("click", (e) => {
					e.stopPropagation(), this.openIndex === s ? this.closeDropdown() : this.openDropdown(s);
				}), i.addEventListener("mouseenter", () => {
					this.openIndex >= 0 && this.openIndex !== s && !this.collapsed && this.openDropdown(s);
				}), r.appendChild(i);
			} else {
				let t = document.createElement("a");
				if (t.className = "cmm-nav-menu__trigger", n.href && (t.href = n.href), n.active === !0 && (t.classList.add("cmm-nav-menu__trigger--current"), t.setAttribute("aria-current", "page")), n.icon) {
					let r = document.createElement("span");
					r.className = "cmm-nav-menu__icon", r.setAttribute("aria-hidden", "true"), e(r, n.icon), t.appendChild(r);
				}
				let i = document.createElement("span");
				i.textContent = n.label, t.appendChild(i), t.addEventListener("click", (e) => {
					this.onNavigate && n.href && (e.preventDefault(), this.onNavigate(n.href));
				}), r.appendChild(t);
			}
		}
		t.appendChild(r), this.container.appendChild(t), this.navEl = t, this.menuListEl = r;
	}
};
//#endregion
export { a as NavigationMenu };

//# sourceMappingURL=index.js.map