import { t as e } from "../../purify.es-Dsqc2y0I.js";
import { n as t } from "../../utils-C_mWfcFX.js";
//#region src/overlays/sheet/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 = class {
	side;
	size;
	showOverlay;
	shouldCloseOnEscape;
	shouldCloseOnOverlay;
	title;
	description;
	className;
	onOpenCallback;
	onCloseCallback;
	titleId;
	descId;
	overlayEl = null;
	sheetEl = null;
	opened = !1;
	destroyed = !1;
	handleKeyDown;
	handleOverlayClick;
	constructor(e) {
		this.side = e?.side ?? "right", this.size = e?.size ?? "md", this.showOverlay = e?.showOverlay !== !1, this.shouldCloseOnEscape = e?.closeOnEscape !== !1, this.shouldCloseOnOverlay = e?.closeOnOverlay !== !1, this.title = e?.title, this.description = e?.description, this.className = e?.className, this.onOpenCallback = e?.onOpen, this.onCloseCallback = e?.onClose, this.titleId = t("cmm-sheet-title"), this.descId = t("cmm-sheet-desc"), this.handleKeyDown = (e) => {
			this.onKeyDown(e);
		}, this.handleOverlayClick = (e) => {
			e.target === this.overlayEl && this.close();
		};
	}
	isOpen() {
		return this.opened;
	}
	open(e) {
		this.destroyed || this.opened || (this.opened = !0, this.createElements(e), this.sheetEl && this.sheetEl.offsetHeight, this.overlayEl && this.overlayEl.offsetHeight, requestAnimationFrame(() => {
			this.sheetEl && this.sheetEl.classList.add("cmm-sheet--open"), this.overlayEl && this.overlayEl.classList.add("cmm-sheet-overlay--open");
		}), this.shouldCloseOnEscape && document.addEventListener("keydown", this.handleKeyDown), requestAnimationFrame(() => {
			if (this.sheetEl) {
				let e = n(this.sheetEl)[0];
				e ? e.focus() : (this.sheetEl.setAttribute("tabindex", "-1"), this.sheetEl.focus());
			}
		}), this.onOpenCallback?.());
	}
	close() {
		if (!this.opened) return;
		this.opened = !1, document.removeEventListener("keydown", this.handleKeyDown), this.sheetEl && this.sheetEl.classList.remove("cmm-sheet--open"), this.overlayEl && this.overlayEl.classList.remove("cmm-sheet-overlay--open");
		let e = this.sheetEl, t = this.overlayEl;
		setTimeout(() => {
			e && e.remove(), t && t.remove();
		}, 300), this.sheetEl = null, this.overlayEl = null, this.onCloseCallback?.();
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.close());
	}
	createElements(t) {
		if (this.showOverlay) {
			let e = document.createElement("div");
			e.className = "cmm-sheet-overlay", this.overlayEl = e, this.shouldCloseOnOverlay && e.addEventListener("click", this.handleOverlayClick), document.body.appendChild(e);
		}
		let n = document.createElement("div");
		if (n.className = `cmm-sheet cmm-sheet--${this.side} cmm-sheet--${this.size}`, n.setAttribute("role", "dialog"), n.setAttribute("aria-modal", "true"), this.className && n.classList.add(this.className), this.title || this.description) {
			let e = document.createElement("div");
			e.className = "cmm-sheet__header";
			let t = document.createElement("div");
			if (t.className = "cmm-sheet__header-text", this.title) {
				let e = document.createElement("h2");
				e.className = "cmm-sheet__title", e.id = this.titleId, e.textContent = this.title, t.appendChild(e), n.setAttribute("aria-labelledby", this.titleId);
			}
			if (this.description) {
				let e = document.createElement("p");
				e.className = "cmm-sheet__description", e.id = this.descId, e.textContent = this.description, t.appendChild(e), n.setAttribute("aria-describedby", this.descId);
			}
			e.appendChild(t);
			let r = document.createElement("button");
			r.className = "cmm-sheet__close", r.type = "button", r.setAttribute("aria-label", "Close"), r.textContent = "×", r.addEventListener("click", () => {
				this.close();
			}), e.appendChild(r), n.appendChild(e);
		} else {
			let e = document.createElement("button");
			e.className = "cmm-sheet__close", e.type = "button", e.setAttribute("aria-label", "Close"), e.textContent = "×", e.style.position = "absolute", e.style.top = "0.75rem", e.style.right = "0.75rem", e.addEventListener("click", () => {
				this.close();
			}), n.appendChild(e);
		}
		let r = document.createElement("div");
		r.className = "cmm-sheet__body", typeof t == "string" ? r.innerHTML = e.sanitize(t) : r.appendChild(t), n.appendChild(r), this.sheetEl = n, document.body.appendChild(n);
	}
	onKeyDown(e) {
		if (e.key === "Escape") {
			e.preventDefault(), this.close();
			return;
		}
		if (e.key === "Tab" && this.sheetEl) {
			let t = n(this.sheetEl);
			if (t.length === 0) {
				e.preventDefault();
				return;
			}
			let r = t[0], i = t[t.length - 1];
			if (r === void 0 || i === void 0) {
				e.preventDefault();
				return;
			}
			e.shiftKey ? document.activeElement === r && (e.preventDefault(), i.focus()) : document.activeElement === i && (e.preventDefault(), r.focus());
		}
	}
};
//#endregion
export { r as Sheet };

//# sourceMappingURL=index.js.map