import { n as e } from "../../utils-C_mWfcFX.js";
//#region src/components/toast/index.ts
var t = class {
	position;
	maxVisible;
	defaultDuration;
	containerEl = null;
	toasts = [];
	containerId;
	regionLabelId;
	constructor(t) {
		this.position = t?.position ?? "top-right", this.maxVisible = t?.maxVisible ?? 5, this.defaultDuration = t?.defaultDuration ?? 5e3, this.containerId = e("cmm-toast-container"), this.regionLabelId = e("cmm-toast-label");
	}
	success(e, t, n) {
		return this.show("success", e, t, n);
	}
	error(e, t, n) {
		return this.show("error", e, t, n);
	}
	warning(e, t, n) {
		return this.show("warning", e, t, n);
	}
	info(e, t, n) {
		return this.show("info", e, t, n);
	}
	dismiss(e) {
		let t = this.toasts.findIndex((t) => t.id === e);
		if (t < 0) return;
		let n = this.toasts[t];
		n !== void 0 && this.removeToast(n);
	}
	dismissAll() {
		let e = [...this.toasts];
		for (let t of e) this.removeToast(t);
	}
	destroy() {
		this.dismissAll(), this.containerEl &&= (this.containerEl.remove(), null);
	}
	ensureContainer() {
		if (this.containerEl) return this.containerEl;
		let e = document.createElement("span");
		e.id = this.regionLabelId, e.className = "cmm-sr-only", e.textContent = "Notifications", e.style.cssText = "\n      position: absolute;\n      width: 1px;\n      height: 1px;\n      padding: 0;\n      margin: -1px;\n      overflow: hidden;\n      clip: rect(0, 0, 0, 0);\n      white-space: nowrap;\n      border: 0;\n    ";
		let t = document.createElement("div");
		switch (t.id = this.containerId, t.className = `cmm-toast-container cmm-toast-container--${this.position}`, t.setAttribute("role", "region"), t.setAttribute("aria-labelledby", this.regionLabelId), t.setAttribute("aria-live", "polite"), t.setAttribute("aria-relevant", "additions removals"), t.style.position = "fixed", t.style.zIndex = "9999", t.style.display = "flex", t.style.flexDirection = "column", t.style.gap = "8px", t.style.pointerEvents = "none", t.style.maxHeight = "100vh", t.style.overflow = "hidden", this.position) {
			case "top-right":
				t.style.top = "16px", t.style.right = "16px";
				break;
			case "top-left":
				t.style.top = "16px", t.style.left = "16px";
				break;
			case "bottom-right":
				t.style.bottom = "16px", t.style.right = "16px";
				break;
			case "bottom-left":
				t.style.bottom = "16px", t.style.left = "16px";
				break;
		}
		return document.body.appendChild(e), document.body.appendChild(t), this.containerEl = t, t;
	}
	getAriaRole(e) {
		return e === "error" || e === "warning" ? "alert" : "status";
	}
	getAriaLive(e) {
		return e === "error" ? "assertive" : "polite";
	}
	show(t, n, r, i) {
		let a = this.ensureContainer(), o = i?.id ?? e("cmm-toast"), s = this.toasts.findIndex((e) => e.id === o);
		if (s >= 0) {
			let e = this.toasts[s];
			e !== void 0 && this.removeToast(e);
		}
		let c = i?.duration ?? this.defaultDuration, l = document.createElement("div");
		l.className = `cmm-toast cmm-toast--${t}`, l.id = o, l.dataset.toastId = o, l.style.pointerEvents = "auto", l.setAttribute("role", this.getAriaRole(t)), l.setAttribute("aria-live", this.getAriaLive(t)), l.setAttribute("aria-atomic", "true");
		let u = document.createElement("span");
		u.className = "cmm-toast__icon", u.setAttribute("aria-hidden", "true"), u.textContent = this.getLevelIcon(t), l.appendChild(u);
		let d = document.createElement("div");
		d.className = "cmm-toast__content";
		let f = document.createElement("div");
		if (f.className = "cmm-toast__title", f.textContent = n, d.appendChild(f), r !== void 0) {
			let e = document.createElement("div");
			e.className = "cmm-toast__message", e.textContent = r, d.appendChild(e);
		}
		l.appendChild(d);
		let p = document.createElement("button");
		p.className = "cmm-toast__close", p.type = "button", p.setAttribute("aria-label", "Dismiss notification"), p.innerHTML = "<span aria-hidden=\"true\">&times;</span>", p.addEventListener("click", () => {
			this.dismiss(o);
		}), l.appendChild(p);
		let m = i?.persistent === !0;
		if (!m) {
			let e = document.createElement("div");
			e.className = "cmm-toast__progress", e.setAttribute("role", "progressbar"), e.setAttribute("aria-valuemin", "0"), e.setAttribute("aria-valuemax", "100"), e.setAttribute("aria-valuenow", "100"), e.setAttribute("aria-label", "Time remaining before auto-dismiss");
			let t = document.createElement("div");
			t.className = "cmm-toast__progress-bar", t.style.animationDuration = `${String(c)}ms`, e.appendChild(t), l.appendChild(e);
		}
		let h = {
			id: o,
			element: l,
			timeoutId: null,
			isPaused: !1,
			remainingTime: c,
			startTime: Date.now(),
			duration: c
		};
		for (l.addEventListener("mouseenter", () => {
			this.pauseToast(h);
		}), l.addEventListener("mouseleave", () => {
			this.resumeToast(h);
		}), l.addEventListener("focusin", () => {
			this.pauseToast(h);
		}), l.addEventListener("focusout", (e) => {
			l.contains(e.relatedTarget) || this.resumeToast(h);
		}), l.addEventListener("keydown", (e) => {
			e.key === "Escape" && (e.preventDefault(), this.dismiss(o));
		}), m || (h.timeoutId = setTimeout(() => {
			this.dismiss(o);
		}, c)), this.toasts.push(h), a.appendChild(l); this.toasts.length > this.maxVisible;) {
			let e = this.toasts[0];
			if (e !== void 0) this.removeToast(e);
			else break;
		}
		return this.toasts.length === 1 && requestAnimationFrame(() => {
			p.focus();
		}), o;
	}
	getLevelIcon(e) {
		switch (e) {
			case "success": return "✓";
			case "error": return "✗";
			case "warning": return "⚠";
			case "info": return "ℹ";
			default: return "";
		}
	}
	pauseToast(e) {
		if (e.isPaused || e.timeoutId === null) return;
		e.isPaused = !0, clearTimeout(e.timeoutId), e.remainingTime -= Date.now() - e.startTime;
		let t = e.element.querySelector(".cmm-toast__progress-bar");
		t !== null && (t.style.animationPlayState = "paused");
	}
	resumeToast(e) {
		if (!e.isPaused || e.remainingTime <= 0) return;
		e.isPaused = !1, e.startTime = Date.now(), e.timeoutId = setTimeout(() => {
			this.dismiss(e.id);
		}, e.remainingTime);
		let t = e.element.querySelector(".cmm-toast__progress-bar");
		t !== null && (t.style.animationPlayState = "running");
	}
	removeToast(e) {
		e.timeoutId !== null && clearTimeout(e.timeoutId), e.element.classList.add("cmm-toast--exiting"), e.element.setAttribute("aria-hidden", "true");
		let t = window.matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : 300;
		setTimeout(() => {
			e.element.remove();
		}, t);
		let n = this.toasts.indexOf(e);
		n >= 0 && this.toasts.splice(n, 1);
	}
};
//#endregion
export { t as ToastManager };

//# sourceMappingURL=index.js.map