import { n as e } from "../sanitize-CDxuPfMB.js";
import { AppShell as t, createAppShell as n } from "./app-shell.js";
import { PageHeader as r, createPageHeader as i } from "./page-header.js";
//#region src/layout/index.ts
var a = () => {
	if (typeof window > "u") return null;
	try {
		return window.localStorage;
	} catch {
		return console.warn("Unable to access localStorage"), null;
	}
}, o = (e) => {
	try {
		return new URL(e, window.location.href).origin === window.location.origin;
	} catch {
		return !1;
	}
}, s = {
	storageKey: "cmm-sidebar-state",
	desktopBreakpoint: "(min-width: 1024px)",
	collapsedClass: "cmm-shell--sidebar-collapsed",
	openClass: "cmm-shell--sidebar-open"
};
function c(e = {}) {
	let t = {
		...s,
		...e
	}, n = document.querySelector("[data-shell-layout]"), r = document.querySelector("[data-shell-sidebar]");
	if (!n || !r) return () => {};
	let i = a(), o = window.matchMedia(t.desktopBreakpoint), c = Array.from(document.querySelectorAll("[data-sidebar-toggle]")), l = Array.from(document.querySelectorAll("[data-sidebar-dismiss]")), u = r.id || "shell-sidebar";
	r.id = u;
	let d = () => i?.getItem(t.storageKey) === "collapsed", f = () => {
		let e = o.matches ? !n.classList.contains(t.collapsedClass) : n.classList.contains(t.openClass);
		c.forEach((t) => {
			t.setAttribute("aria-expanded", e.toString()), t.setAttribute("aria-controls", u);
		});
	}, p = (e, r = !0) => {
		n.classList.toggle(t.collapsedClass, e), r && o.matches && i?.setItem(t.storageKey, e ? "collapsed" : "expanded"), f();
	}, m = (e) => {
		n.classList.toggle(t.openClass, e), f();
	}, h = () => {
		o.matches ? (m(!1), p(d(), !1)) : (m(!1), p(!1, !1));
	}, g = () => {
		o.matches ? p(!n.classList.contains(t.collapsedClass)) : m(!n.classList.contains(t.openClass));
	}, _ = () => {
		m(!1);
	}, v = (e) => {
		e.key === "Escape" && !o.matches && m(!1);
	};
	return c.forEach((e) => {
		e.addEventListener("click", g);
	}), l.forEach((e) => {
		e.addEventListener("click", _);
	}), document.addEventListener("keydown", v), o.addEventListener("change", h), h(), () => {
		c.forEach((e) => {
			e.removeEventListener("click", g);
		}), l.forEach((e) => {
			e.removeEventListener("click", _);
		}), document.removeEventListener("keydown", v), o.removeEventListener("change", h);
	};
}
function l(e = {}) {
	let t = e.triggerSelector ?? "[data-user-trigger]", n = e.dropdownSelector ?? "[data-user-dropdown]", r = document.querySelector("[data-user-menu]"), i = document.querySelector(t), a = document.querySelector(n);
	if (!r || !i || !a) return () => {};
	let o = (e) => {
		i.setAttribute("aria-expanded", e.toString()), r.setAttribute("aria-expanded", e.toString());
	}, s = () => i.getAttribute("aria-expanded") === "true", c = (e) => {
		e.stopPropagation(), o(!s());
	}, l = (e) => {
		r.contains(e.target) || o(!1);
	}, u = (e) => {
		e.key === "Escape" && s() && (o(!1), i.focus());
	}, d = e.dropdownItemSelector ?? ".cmm-user-menu__dropdown-item", f = (e) => {
		let t = Array.from(a.querySelectorAll(d)), n = t.indexOf(document.activeElement);
		e.key === "ArrowDown" ? (e.preventDefault(), t[n < t.length - 1 ? n + 1 : 0]?.focus()) : e.key === "ArrowUp" ? (e.preventDefault(), t[n > 0 ? n - 1 : t.length - 1]?.focus()) : (e.key === "Tab" && !e.shiftKey && n === t.length - 1 || e.key === "Tab" && e.shiftKey && n === 0) && o(!1);
	}, p = (e) => {
		(e.key === "Enter" || e.key === " " || e.key === "ArrowDown") && !s() && (e.preventDefault(), o(!0), setTimeout(() => {
			a.querySelector(d)?.focus();
		}, 50));
	};
	return i.addEventListener("click", c), document.addEventListener("click", l), document.addEventListener("keydown", u), a.addEventListener("keydown", f), i.addEventListener("keydown", p), () => {
		i.removeEventListener("click", c), document.removeEventListener("click", l), document.removeEventListener("keydown", u), a.removeEventListener("keydown", f), i.removeEventListener("keydown", p);
	};
}
var u = (e) => e.dataset.collapsibleOpen === "true" || !e.classList.contains("hidden"), d = "cmm-collapsible--hidden", f = (e, t) => {
	e.classList.toggle("hidden", !t), e.classList.toggle(d, !t), e.dataset.collapsibleOpen = t.toString(), e.setAttribute("aria-hidden", t ? "false" : "true");
}, p = (e, t) => {
	document.querySelectorAll(`[data-toggle-target='${e}']`).forEach((e) => {
		e.setAttribute("aria-expanded", t.toString());
	});
};
function m(e = {}) {
	let { scrollIntoView: t = !0, scrollBehavior: n = "smooth", hiddenClass: r = "cmm-collapsible--hidden" } = e;
	d = r, document.querySelectorAll("[data-collapsible]").forEach((e) => {
		f(e, e.dataset.collapsibleOpen === "true" || !e.classList.contains("hidden"));
	});
	let i = [];
	document.querySelectorAll("[data-toggle-target]").forEach((e) => {
		let r = e.dataset.toggleTarget;
		if (!r) return;
		let a = document.querySelector(r);
		if (!a) return;
		let o = a.id || r.replace(/^#/, "");
		e.setAttribute("aria-controls", o), e.setAttribute("aria-expanded", u(a).toString());
		let s = (e) => {
			e.preventDefault();
			let i = !u(a);
			f(a, i), p(r, i), i && t && a.scrollIntoView({
				behavior: n,
				block: "start"
			});
		};
		e.addEventListener("click", s), i.push({
			element: e,
			handler: s
		});
	}), document.querySelectorAll("[data-collapse-target]").forEach((e) => {
		let t = e.dataset.collapseTarget;
		if (!t) return;
		let n = document.querySelector(t);
		if (!n) return;
		let r = (e) => {
			e.preventDefault(), f(n, !1), p(t, !1);
		};
		e.addEventListener("click", r), i.push({
			element: e,
			handler: r
		});
	});
	let a = window.location.hash;
	if (a) {
		let e = document.querySelector(a);
		e?.dataset.collapsible !== void 0 && (f(e, !0), p(a, !0));
	}
	return () => {
		i.forEach(({ element: e, handler: t }) => {
			e.removeEventListener("click", t);
		});
	};
}
var h = "\n  <div class=\"cmm-sheet__loading\">\n    <div class=\"cmm-spinner\"></div>\n  </div>\n", g = "\n  <div class=\"cmm-sheet__error\">\n    <p>Failed to load content. Please try the full page.</p>\n  </div>\n";
function _(t = {}) {
	let { openClass: n = "cmm-sheet--open", hiddenClass: r = "cmm-sheet--hidden", backdropVisibleClass: i = "cmm-sheet__backdrop--visible", backdropHiddenClass: a = "cmm-sheet__backdrop--hidden", spinnerHtml: s = h, errorHtml: c = g, sanitizeFetchedHtml: l = !1, allowCrossOriginFetch: u = !1 } = t, d = /* @__PURE__ */ new Map();
	if (document.querySelectorAll("[data-sheet]").forEach((e) => {
		let t = e.dataset.sheet;
		if (!t) return;
		let n = document.querySelector(`[data-sheet-backdrop='${t}']`), r = e.querySelector("[data-sheet-content]");
		!n || !r || d.set(t, {
			key: t,
			sheet: e,
			backdrop: n,
			content: r,
			fullPageLink: e.querySelector("[data-sheet-fullpage]"),
			expandLink: e.querySelector("[data-sheet-expand]"),
			focusSelector: e.dataset.sheetFocus
		});
	}), d.size === 0) return () => {};
	let f = null, p = (e, t) => {
		e.sheet.classList.toggle(n, t), e.sheet.classList.toggle(r, !t), e.backdrop.classList.toggle(i, t), e.backdrop.classList.toggle(a, !t), e.sheet.setAttribute("aria-hidden", t ? "false" : "true");
	}, m = (e) => {
		f?.key === e.key && (f = null), p(e, !1);
	}, _ = async (t, n) => {
		f && f.key !== t.key && m(f), f = t, p(t, !0);
		let r = n?.dataset.sheetUrl ?? t.sheet.dataset.sheetUrl, i = n?.dataset.sheetFocus ?? t.focusSelector, a = n?.dataset.sheetFullpage ?? t.sheet.dataset.sheetFullpage, d = n?.dataset.sheetExpand ?? t.sheet.dataset.sheetExpand;
		if (t.fullPageLink && a && (t.fullPageLink.href = a), t.expandLink && d && (t.expandLink.href = d), !r || !u && !o(r)) {
			t.content.innerHTML = c;
			return;
		}
		t.content.innerHTML = s;
		try {
			let n = await fetch(r);
			if (n.ok) {
				let r = await n.text();
				t.content.innerHTML = l ? e(r, { allowSvg: !0 }) : r, i && t.content.querySelector(i)?.focus();
			} else a ? window.location.href = a : t.content.innerHTML = c;
		} catch {
			a ? window.location.href = a : t.content.innerHTML = c;
		}
	}, v = [];
	d.forEach((e) => {
		let t = (t) => {
			t.target.closest("[data-sheet-close]") && (t.preventDefault(), m(e));
		}, n = () => {
			m(e);
		}, r = () => {
			m(e);
		};
		e.sheet.addEventListener("click", t), e.backdrop.addEventListener("click", n), v.push({
			element: e.sheet,
			event: "click",
			handler: t
		}), v.push({
			element: e.backdrop,
			event: "click",
			handler: n
		}), e.expandLink && (e.expandLink.addEventListener("click", r), v.push({
			element: e.expandLink,
			event: "click",
			handler: r
		}));
	}), document.querySelectorAll("[data-sheet-trigger]").forEach((e) => {
		let t = e.dataset.sheetTrigger;
		if (!t) return;
		let n = d.get(t);
		if (!n) return;
		let r = (t) => {
			t.preventDefault(), _(n, e);
		};
		e.addEventListener("click", r), v.push({
			element: e,
			event: "click",
			handler: r
		});
	});
	let y = (e) => {
		e.key === "Escape" && f && m(f);
	};
	return document.addEventListener("keydown", y), () => {
		v.forEach(({ element: e, event: t, handler: n }) => {
			e.removeEventListener(t, n);
		}), document.removeEventListener("keydown", y);
	};
}
function v() {
	let e = document.querySelector("[data-nav-list]");
	if (!e) return () => {};
	let t = () => Array.from(e.querySelectorAll("[data-nav-item]")), n = (e) => {
		let n = t(), r = n.indexOf(document.activeElement);
		e.key === "ArrowDown" ? (e.preventDefault(), n[r < n.length - 1 ? r + 1 : 0]?.focus()) : e.key === "ArrowUp" ? (e.preventDefault(), n[r > 0 ? r - 1 : n.length - 1]?.focus()) : e.key === "Home" ? (e.preventDefault(), n[0]?.focus()) : e.key === "End" && (e.preventDefault(), n[n.length - 1]?.focus());
	};
	return e.addEventListener("keydown", n), () => {
		e.removeEventListener("keydown", n);
	};
}
function y(e = {}) {
	let t = [
		c(e.sidebar),
		l(e.userMenu),
		m(e.collapsibles),
		_(e.sheets),
		v()
	];
	return () => {
		t.forEach((e) => {
			e();
		});
	};
}
//#endregion
export { t as AppShell, r as PageHeader, n as createAppShell, i as createPageHeader, y as initLayout, m as setupCollapsibles, v as setupNavKeyboard, _ as setupSheets, c as setupSidebar, l as setupUserMenu };

//# sourceMappingURL=index.js.map