import { getIconSvgInner as e } from "./icons/index.js";
import { o as t } from "./sanitize-uWpY18N9.js";
import { t as n } from "./utils-CSEWuKHo.js";
import { createBrandLockupElement as r } from "./components/brand/index.js";
//#region src/sidebar/component.ts
var i = 900, a = "http://localhost/";
function o(e) {
	return `sidebar--${e.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "app"}`;
}
function s(e) {
	let t = e.trim().split(/\s+/).filter(Boolean);
	return t.length >= 2 ? `${t[0]?.charAt(0) ?? ""}${t[t.length - 1]?.charAt(0) ?? ""}`.toUpperCase() : e.slice(0, 2).toUpperCase();
}
function c() {
	try {
		return typeof window < "u" && window.localStorage !== void 0;
	} catch {
		return !1;
	}
}
function l(e) {
	try {
		return new URL(e, a).pathname;
	} catch {
		return e.split(/[?#]/, 1)[0] ?? e;
	}
}
function u(e) {
	return typeof window < "u" && window.innerWidth <= e;
}
function d(e, t) {
	if (e === void 0) return null;
	if (typeof e == "number") return {
		label: e > 0 ? String(e) : "",
		tone: t ?? "primary",
		visible: e > 0,
		dot: e > 0
	};
	let n = e.label, r = {
		label: n,
		tone: e.tone ?? t ?? "primary",
		visible: n.length > 0 || e.dot === !0,
		dot: e.dot ?? n.length > 0
	};
	return e.title !== void 0 && (r.title = e.title), r;
}
function f(n, r, i = "sidebar__item-icon") {
	let a = "http://www.w3.org/2000/svg", o = document.createElementNS(a, "svg");
	o.setAttribute("xmlns", a), o.setAttribute("width", "16"), o.setAttribute("height", "16"), o.setAttribute("viewBox", "0 0 24 24"), o.setAttribute("fill", "none"), o.setAttribute("stroke", "currentColor"), o.setAttribute("stroke-width", "2"), o.setAttribute("stroke-linecap", "round"), o.setAttribute("stroke-linejoin", "round"), o.classList.add(i), o.setAttribute("data-icon", r), o.setAttribute("aria-hidden", "true");
	let s = e(n, r);
	return s && t(o, s), o;
}
function p(e, t) {
	if (t === void 0) return;
	let n = Array.isArray(t) ? t : [t];
	for (let t of n) e.appendChild(t);
}
var m = class {
	container;
	aside;
	config;
	iconSet;
	variant;
	cleanupFns = [];
	collapseButton = null;
	backdrop = null;
	constructor(e) {
		this.config = e, this.container = n(e.container), this.iconSet = e.iconSet ?? "lucide", this.variant = e.variant ?? "app", this.aside = document.createElement("aside"), this.aside.classList.add("sidebar", o(this.variant)), (this.variant === "threat-library" || this.variant === "cm") && this.aside.classList.add("sidebar--product"), this.aside.setAttribute("data-sidebar", this.variant), this.aside.setAttribute("data-open", String(e.open === !0)), this.aside.setAttribute("aria-label", "Primary navigation"), this.resolveInitialCollapsed() && this.aside.classList.add("sidebar--collapsed"), this.render(), this.container.appendChild(this.aside), e.mobileBackdrop === !0 && this.renderBackdrop(), this.setupBehavior();
	}
	render() {
		this.config.collapsible === !0 && this.renderCollapseButton(), this.renderHeader(), this.renderScope();
		let e = document.createElement("nav");
		e.classList.add("sidebar__body"), e.setAttribute("aria-label", "Primary"), p(e, this.config.beforeNav);
		for (let t of this.config.sections) e.appendChild(this.buildSection(t));
		p(e, this.config.afterNav), this.aside.appendChild(e), this.renderFooter(), this.config.activeItemId !== void 0 && this.setActive(this.config.activeItemId);
	}
	buildSection(e) {
		let t = document.createElement("div");
		t.classList.add("sidebar__section"), t.setAttribute("data-section-id", e.id);
		let n = e.collapsible === !0, r = e.defaultExpanded !== !1;
		if (n && e.label) {
			t.classList.add("sidebar__section--collapsible"), r && t.classList.add("sidebar__section--expanded");
			let n = document.createElement("button");
			n.type = "button", n.classList.add("sidebar__group-header"), n.setAttribute("aria-expanded", String(r)), n.setAttribute("aria-controls", `sidebar-group-${e.id}`), e.icon && n.appendChild(f(this.iconSet, e.icon));
			let i = document.createElement("span");
			i.classList.add("sidebar__group-label"), i.textContent = e.label, n.appendChild(i);
			let a = document.createElementNS("http://www.w3.org/2000/svg", "svg");
			a.setAttribute("class", "sidebar__group-chevron"), a.setAttribute("viewBox", "0 0 24 24"), a.setAttribute("width", "14"), a.setAttribute("height", "14"), a.setAttribute("fill", "none"), a.setAttribute("stroke", "currentColor"), a.setAttribute("stroke-width", "2"), a.setAttribute("stroke-linecap", "round"), a.setAttribute("stroke-linejoin", "round"), a.setAttribute("aria-hidden", "true");
			let o = document.createElementNS("http://www.w3.org/2000/svg", "path");
			o.setAttribute("d", "m6 9 6 6 6-6"), a.appendChild(o), n.appendChild(a), n.addEventListener("click", () => {
				let e = t.classList.toggle("sidebar__section--expanded");
				n.setAttribute("aria-expanded", String(e));
			}), t.appendChild(n);
		} else if (e.label) {
			let n = document.createElement("div");
			n.classList.add("sidebar__section-label"), n.textContent = e.label, t.appendChild(n);
		}
		let i = document.createElement("div");
		i.classList.add("sidebar__items"), i.id = `sidebar-group-${e.id}`;
		for (let t of e.items) i.appendChild(this.buildItem(t));
		return t.appendChild(i), t;
	}
	buildItem(e) {
		let t = e.disabled === !0, n = e.href !== void 0 && !t ? document.createElement("a") : t ? document.createElement("span") : document.createElement("button");
		n.classList.add("sidebar__item"), n.setAttribute("data-item-id", e.id), n.setAttribute("data-nav-item", ""), n.setAttribute("title", e.tooltip ?? e.label), e.ariaLabel !== void 0 && n.setAttribute("aria-label", e.ariaLabel), t ? (n.setAttribute("aria-disabled", "true"), n.setAttribute("role", "link"), n.setAttribute("tabindex", "-1"), n.classList.add("sidebar__item--disabled")) : n instanceof HTMLAnchorElement ? (n.href = e.href ?? "#", e.target !== void 0 && (n.target = e.target), e.rel === void 0 ? e.target === "_blank" && (n.rel = "noreferrer noopener") : n.rel = e.rel) : n instanceof HTMLButtonElement && (n.type = "button"), this.isItemActive(e) && (n.classList.add("sidebar__item--active"), n.setAttribute("aria-current", "page")), e.icon && n.appendChild(f(this.iconSet, e.icon));
		let r = document.createElement("span");
		if (r.classList.add("sidebar__item-label"), r.textContent = e.label, n.appendChild(r), e.tag !== void 0) {
			let t = document.createElement("span");
			t.classList.add("sidebar__tag"), t.textContent = e.tag, n.appendChild(t);
		}
		let i = d(e.badge, e.badgeTone);
		return i?.visible === !0 && this.applyBadge(n, e.id, i), t || n.addEventListener("click", (t) => {
			this.config.onNavigate?.(e), e.onClick?.(t), this.config.closeOnNavigate !== !1 && this.isOpen() && this.setOpen(!1);
		}), n;
	}
	renderHeader() {
		let e = this.config.brand;
		if (e === void 0) return;
		let t = document.createElement("div");
		t.classList.add("sidebar__header");
		let n = document.createElement("div");
		n.classList.add("sidebar__header-row");
		let i = e.href === void 0 ? e.onClick === void 0 ? document.createElement("span") : document.createElement("button") : document.createElement("a");
		i.classList.add("sidebar__brand"), i instanceof HTMLAnchorElement && (i.href = e.href ?? "#", i.setAttribute("aria-label", e.label ?? "CounterMeasure")), i instanceof HTMLButtonElement && (i.type = "button", i.setAttribute("aria-label", e.label ?? "CounterMeasure")), e.onClick !== void 0 && i.addEventListener("click", e.onClick), i.appendChild(r({
			label: e.label ?? "CounterMeasure",
			markSize: e.markSize ?? 22,
			showWordmark: e.showWordmark ?? !0,
			decorative: e.decorative ?? !1
		})), n.appendChild(i), t.appendChild(n), this.aside.appendChild(t);
	}
	renderScope() {
		let e = this.config.scope;
		if (e === void 0 || e.options.length === 0) return;
		let t = e.options.find((t) => t.id === e.value) ?? e.options[0] ?? null, n = document.createElement("div");
		n.classList.add("sidebar__scope");
		let r = document.createElement("button");
		r.type = "button", r.classList.add("sidebar__scope-trigger"), r.setAttribute("aria-haspopup", "listbox"), r.setAttribute("aria-expanded", "false"), r.setAttribute("title", t?.label ?? e.placeholder ?? "Select scope"), e.icon !== void 0 && r.appendChild(f(this.iconSet, e.icon, "sidebar__scope-icon"));
		let i = document.createElement("span");
		if (i.classList.add("sidebar__scope-copy"), e.label !== void 0) {
			let t = document.createElement("span");
			t.classList.add("sidebar__scope-label"), t.textContent = e.label, i.appendChild(t);
		}
		let a = document.createElement("span");
		a.classList.add("sidebar__scope-current"), a.textContent = t?.label ?? e.placeholder ?? "Select scope", i.appendChild(a), r.appendChild(i);
		let o = f(this.iconSet, "chevron-down", "sidebar__scope-chevron");
		r.appendChild(o);
		let s = document.createElement("div");
		s.classList.add("sidebar__scope-menu"), s.setAttribute("role", "listbox"), s.hidden = !0;
		let c = (e) => {
			r.setAttribute("aria-expanded", String(e)), s.hidden = !e;
		};
		for (let n of e.options) {
			let i = document.createElement("button");
			if (i.type = "button", i.classList.add("sidebar__scope-option"), i.setAttribute("role", "option"), i.setAttribute("data-option-id", n.id), i.setAttribute("aria-selected", String(n.id === t?.id)), n.disabled === !0 && (i.disabled = !0), n.status !== void 0) {
				let e = document.createElement("span");
				e.classList.add("sidebar__scope-dot", `sidebar__scope-dot--${n.status}`), i.appendChild(e);
			}
			let o = document.createElement("span");
			o.classList.add("sidebar__scope-option-label"), o.textContent = n.label, i.appendChild(o);
			let l = f(this.iconSet, "check", "sidebar__scope-check");
			i.appendChild(l), i.addEventListener("click", () => {
				a.textContent = n.label, r.setAttribute("title", n.label), s.querySelectorAll("[role=\"option\"]").forEach((e) => {
					e.setAttribute("aria-selected", String(e.getAttribute("data-option-id") === n.id));
				}), c(!1), e.onChange?.(n);
			}), s.appendChild(i);
		}
		r.addEventListener("click", () => {
			c(s.hidden === !0);
		});
		let l = (e) => {
			n.contains(e.target) || c(!1);
		};
		document.addEventListener("pointerdown", l), this.cleanupFns.push(() => {
			document.removeEventListener("pointerdown", l);
		}), n.appendChild(r), n.appendChild(s), this.aside.appendChild(n);
	}
	renderCollapseButton() {
		let e = document.createElement("button");
		e.type = "button", e.classList.add("sidebar__collapse-btn", "sidebar__collapse-btn--edge"), e.setAttribute("data-sidebar-toggle", ""), this.config.collapseButtonVisible === !0 && e.setAttribute("data-visible", "true");
		let t = document.createElementNS("http://www.w3.org/2000/svg", "svg");
		t.setAttribute("aria-hidden", "true"), t.setAttribute("viewBox", "0 0 24 24"), t.setAttribute("fill", "none"), t.setAttribute("stroke", "currentColor"), t.setAttribute("stroke-width", "2"), t.setAttribute("stroke-linecap", "round"), t.setAttribute("stroke-linejoin", "round");
		let n = document.createElementNS("http://www.w3.org/2000/svg", "path");
		t.appendChild(n), e.appendChild(t), e.addEventListener("click", () => {
			this.toggleCollapse();
		}), this.collapseButton = e, this.updateCollapseButton(), this.aside.appendChild(e);
	}
	renderFooter() {
		let e = this.config.user !== void 0, t = this.config.footerActions !== void 0 && this.config.footerActions.length > 0;
		if (!e && !t) return;
		let n = document.createElement("div");
		n.classList.add("sidebar__footer");
		let r = document.createElement("div");
		if (r.classList.add("sidebar__footer-bar"), e && this.config.user) {
			let e = this.config.user, t = e.onClick === void 0 ? document.createElement("span") : document.createElement("button");
			t.classList.add("sidebar__user-trigger", "sidebar__footer-user");
			let n = e.onClick;
			t instanceof HTMLButtonElement && n !== void 0 && (t.type = "button", t.setAttribute("aria-label", e.label ?? `User menu for ${e.name}`), t.addEventListener("click", n));
			let i = document.createElement("span");
			if (i.classList.add("sidebar__avatar", "sidebar__footer-avatar"), e.avatarUrl !== void 0) {
				let t = document.createElement("img");
				t.src = e.avatarUrl, t.alt = "", i.appendChild(t);
			} else i.textContent = e.initials ?? s(e.name);
			if (e.presence !== void 0 && e.presence !== "offline") {
				let t = document.createElement("span");
				t.classList.add("sidebar__presence", `sidebar__presence--${e.presence}`), t.setAttribute("aria-hidden", "true"), i.appendChild(t);
			}
			let a = document.createElement("span");
			a.classList.add("sidebar__user-details");
			let o = document.createElement("span");
			if (o.classList.add("sidebar__user-name", "sidebar__footer-name"), o.textContent = e.name, a.appendChild(o), e.detail !== void 0) {
				let t = document.createElement("span");
				t.classList.add("sidebar__user-status"), t.textContent = e.detail, a.appendChild(t);
			}
			t.appendChild(i), t.appendChild(a), r.appendChild(t);
		}
		if (t && this.config.footerActions) {
			let e = document.createElement("span");
			e.classList.add("sidebar__footer-icons");
			for (let t of this.config.footerActions) {
				let n = document.createElement("button");
				n.type = "button", n.classList.add("sidebar__footer-icon"), t.danger === !0 && n.classList.add("sidebar__footer-icon--danger"), n.setAttribute("aria-label", t.label), n.setAttribute("title", t.label);
				let r = f(this.iconSet, t.icon);
				if (n.appendChild(r), t.onClick) {
					let e = t.onClick;
					n.addEventListener("click", () => {
						e();
					});
				}
				e.appendChild(n);
			}
			r.appendChild(e);
		}
		n.appendChild(r), this.aside.appendChild(n);
	}
	renderBackdrop() {
		let e = document.createElement("button");
		e.type = "button", e.classList.add("sidebar__backdrop"), e.setAttribute("data-sidebar-backdrop", this.variant), e.setAttribute("data-visible", String(this.isOpen())), e.setAttribute("aria-label", "Close sidebar"), e.addEventListener("click", () => {
			this.setOpen(!1);
		}), this.backdrop = e, this.container.appendChild(e);
	}
	setupBehavior() {
		this.setupKeyboardNavigation(), this.setupMobileToggles();
		let e = (e) => {
			e.key === "Escape" && (this.isOpen() && this.setOpen(!1), this.closeScopeMenus());
		};
		document.addEventListener("keydown", e), this.cleanupFns.push(() => {
			document.removeEventListener("keydown", e);
		});
		let t = () => {
			u(this.getMobileBreakpoint()) && this.setOpen(!1);
		};
		window.addEventListener("resize", t), this.cleanupFns.push(() => {
			window.removeEventListener("resize", t);
		});
	}
	setupKeyboardNavigation() {
		let e = (e) => {
			if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
			let t = Array.from(this.aside.querySelectorAll("[data-nav-item]:not([aria-disabled=\"true\"])")).filter((e) => !e.hidden && e.offsetParent !== null);
			if (t.length === 0) return;
			let n = t.indexOf(document.activeElement);
			n !== -1 && (e.preventDefault(), t[e.key === "ArrowDown" ? (n + 1) % t.length : (n - 1 + t.length) % t.length]?.focus());
		};
		this.aside.addEventListener("keydown", e), this.cleanupFns.push(() => {
			this.aside.removeEventListener("keydown", e);
		});
	}
	setupMobileToggles() {
		let e = Array.from(document.querySelectorAll("[data-sidebar-mobile-toggle]")).filter((e) => {
			let t = e.getAttribute("data-sidebar-mobile-toggle");
			return t === null || t === "" || t === this.variant;
		});
		for (let t of e) {
			let e = () => {
				this.setOpen(!this.isOpen());
			};
			t.addEventListener("click", e), this.cleanupFns.push(() => {
				t.removeEventListener("click", e);
			});
		}
	}
	setActive(e) {
		this.config.activeItemId = e, this.aside.querySelectorAll(".sidebar__item").forEach((e) => {
			e.classList.remove("sidebar__item--active"), e.removeAttribute("aria-current");
		});
		let t = Array.from(this.aside.querySelectorAll("[data-item-id]")).find((t) => t.getAttribute("data-item-id") === e);
		t && (t.classList.add("sidebar__item--active"), t.setAttribute("aria-current", "page"));
	}
	setPathname(e) {
		e === void 0 ? delete this.config.pathname : this.config.pathname = e, delete this.config.activeItemId, this.syncActiveRouteState();
	}
	updateBadge(e, t, n) {
		let r = Array.from(this.aside.querySelectorAll("[data-item-id]")).find((t) => t.getAttribute("data-item-id") === e);
		r && this.applyBadge(r, e, d(t, n ?? r.getAttribute("data-badge-tone") ?? void 0));
	}
	toggleCollapse() {
		this.setCollapsed(!this.isCollapsed());
	}
	collapse() {
		this.setCollapsed(!0);
	}
	expand() {
		this.setCollapsed(!1);
	}
	setOpen(e) {
		let t = this.isOpen();
		this.aside.setAttribute("data-open", String(e)), this.backdrop?.setAttribute("data-visible", String(e)), t !== e && this.config.onOpenChange?.(e);
	}
	destroy() {
		for (let e of this.cleanupFns.splice(0)) e();
		this.backdrop?.remove(), this.aside.remove();
	}
	applyBadge(e, t, n) {
		if (n === null) return;
		let r = e.querySelector(`[data-badge="${t}"]`);
		r === null && (r = document.createElement("span"), r.classList.add("sidebar__badge"), r.setAttribute("data-badge", t), e.appendChild(r)), r.className = `sidebar__badge sidebar__badge--${n.tone}`, r.textContent = n.label, n.title === void 0 ? r.removeAttribute("title") : r.setAttribute("title", n.title), e.setAttribute("data-badge-visible", String(n.visible)), e.setAttribute("data-badge-dot", String(n.dot)), e.setAttribute("data-badge-tone", n.tone);
	}
	setCollapsed(e) {
		let t = this.isCollapsed();
		this.aside.classList.toggle("sidebar--collapsed", e), this.persistCollapsed(e), this.updateCollapseButton(), t !== e && this.config.onCollapse?.(e);
	}
	isCollapsed() {
		return this.aside.classList.contains("sidebar--collapsed");
	}
	isOpen() {
		return this.aside.getAttribute("data-open") === "true";
	}
	getMobileBreakpoint() {
		return this.config.mobileBreakpoint ?? i;
	}
	resolveInitialCollapsed() {
		if (this.config.storageKey && this.config.persistCollapsed !== !1 && c()) try {
			if (!u(this.getMobileBreakpoint())) return window.localStorage.getItem(this.config.storageKey) === "true";
		} catch {
			return this.config.collapsed === !0;
		}
		return this.config.collapsed === !0;
	}
	persistCollapsed(e) {
		if (!(!this.config.storageKey || this.config.persistCollapsed === !1 || !c())) try {
			window.localStorage.setItem(this.config.storageKey, String(e));
		} catch {}
	}
	getCurrentPathname() {
		return this.config.pathname === void 0 ? typeof window > "u" ? null : window.location.pathname : l(this.config.pathname);
	}
	isItemActive(e) {
		if (e.active === !0 || e.id === this.config.activeItemId) return !0;
		let t = this.getCurrentPathname();
		if (t === null) return !1;
		let n = e.activeMatch;
		if (typeof n == "function") return n(t, e);
		if (n instanceof RegExp) return n.test(t);
		if (typeof n == "string" && n !== "exact" && n !== "prefix") return t === n || t.startsWith(`${n.replace(/\/$/, "")}/`);
		if (e.href === void 0) return !1;
		try {
			let r = typeof window > "u" ? a : window.location.href, i = new URL(e.href, r).pathname;
			return n === "exact" || e.end === !0 ? t === i : t === i || t.startsWith(`${i.replace(/\/$/, "")}/`);
		} catch {
			return !1;
		}
	}
	syncActiveRouteState() {
		this.aside.querySelectorAll(".sidebar__item").forEach((e) => {
			e.classList.remove("sidebar__item--active"), e.removeAttribute("aria-current");
		});
		let e = Array.from(this.aside.querySelectorAll("[data-item-id]")).find((e) => {
			let t = e.getAttribute("data-item-id"), n = this.config.sections.flatMap((e) => e.items).find((e) => e.id === t);
			return n === void 0 ? !1 : this.isItemActive(n);
		});
		e !== void 0 && (e.classList.add("sidebar__item--active"), e.setAttribute("aria-current", "page"));
	}
	closeScopeMenus() {
		this.aside.querySelectorAll(".sidebar__scope-menu").forEach((e) => {
			e.hidden = !0;
		}), this.aside.querySelectorAll(".sidebar__scope-trigger").forEach((e) => {
			e.setAttribute("aria-expanded", "false");
		});
	}
	updateCollapseButton() {
		if (this.collapseButton === null) return;
		let e = this.isCollapsed(), t = e ? "Expand sidebar" : "Collapse sidebar";
		this.collapseButton.setAttribute("aria-label", t), this.collapseButton.setAttribute("aria-expanded", String(!e)), this.collapseButton.setAttribute("title", t), this.collapseButton.setAttribute("data-collapsed", String(e)), this.collapseButton.querySelector("path")?.setAttribute("d", e ? "m9 18 6-6-6-6" : "m15 18-6-6 6-6");
	}
};
//#endregion
export { m as t };

//# sourceMappingURL=component-D5sRm1fq.js.map