import { t as e } from "../../purify.es-Dsqc2y0I.js";
import { n as t } from "../../utils-C_mWfcFX.js";
import { n } from "../../a11y-CuHKgVSx.js";
//#region src/components/modal/index.ts
var r = class r {
	overlayEl = null;
	dialogEl = null;
	config;
	titleId;
	isLoading = !1;
	focusTrap = null;
	handleKeyDown;
	constructor(e) {
		this.config = e, this.titleId = t("cmm-modal-title"), this.handleKeyDown = (e) => {
			this.onKeyDown(e);
		};
	}
	static open(e) {
		let t = new r(e);
		return t.show(), t;
	}
	close() {
		if (document.removeEventListener("keydown", this.handleKeyDown), this.focusTrap?.deactivate(), this.focusTrap = null, this.overlayEl) {
			this.overlayEl.classList.remove("cmm-modal--open");
			let e = this.overlayEl;
			setTimeout(() => {
				e.remove();
			}, 200), this.overlayEl = null, this.dialogEl = null;
		}
		this.config.onCancel?.();
	}
	setBody(t) {
		if (!this.dialogEl) return;
		let n = this.dialogEl.querySelector(".cmm-modal__body");
		n && (typeof t == "string" ? n.innerHTML = e.sanitize(t) : (n.innerHTML = "", n.appendChild(t)));
	}
	setLoading(e) {
		this.isLoading = e, this.dialogEl && this.dialogEl.querySelectorAll(".cmm-modal__footer button").forEach((t) => {
			t.disabled = e;
		});
	}
	show() {
		let t = document.createElement("div");
		t.className = "cmm-modal", this.overlayEl = t, this.config.closeOnBackdrop !== !1 && t.addEventListener("click", (e) => {
			e.target === t && this.close();
		});
		let r = document.createElement("div");
		r.className = "cmm-modal__dialog", r.setAttribute("role", "dialog"), r.setAttribute("aria-modal", "true"), r.setAttribute("aria-labelledby", this.titleId), this.dialogEl = r;
		let i = document.createElement("div");
		i.className = "cmm-modal__header";
		let a = document.createElement("h2");
		a.className = "cmm-modal__title", a.id = this.titleId, a.textContent = this.config.title, i.appendChild(a);
		let o = document.createElement("button");
		o.className = "cmm-modal__close", o.textContent = "×", o.setAttribute("aria-label", "Close"), o.addEventListener("click", () => {
			this.close();
		}), i.appendChild(o), r.appendChild(i);
		let s = document.createElement("div");
		s.className = "cmm-modal__body", this.config.body !== void 0 && (typeof this.config.body == "string" ? s.innerHTML = e.sanitize(this.config.body) : s.appendChild(this.config.body)), r.appendChild(s);
		let c = document.createElement("div");
		if (c.className = "cmm-modal__footer", (this.config.variant ?? "confirm") === "alert") {
			let e = document.createElement("button");
			e.className = "cmm-modal__btn cmm-modal__btn--primary", e.textContent = this.config.confirmLabel ?? "OK", e.addEventListener("click", () => {
				this.close();
			}), c.appendChild(e);
		} else {
			let e = document.createElement("button");
			e.className = "cmm-modal__btn cmm-modal__btn--secondary", e.textContent = this.config.cancelLabel ?? "Cancel", e.addEventListener("click", () => {
				this.close();
			}), c.appendChild(e);
			let t = document.createElement("button");
			t.className = "cmm-modal__btn cmm-modal__btn--primary", t.textContent = this.config.confirmLabel ?? "Confirm", t.addEventListener("click", () => {
				this.handleConfirm();
			}), c.appendChild(t);
		}
		r.appendChild(c), t.appendChild(r), document.body.appendChild(t), requestAnimationFrame(() => {
			t.classList.add("cmm-modal--open");
		}), this.config.closeOnEscape !== !1 && document.addEventListener("keydown", this.handleKeyDown), r.setAttribute("tabindex", "-1"), this.focusTrap = n(r), this.focusTrap.activate();
	}
	async handleConfirm() {
		if (this.isLoading) return;
		if (this.config.onConfirm === void 0) {
			this.close();
			return;
		}
		let e = this.config.onConfirm();
		if (e !== void 0) {
			this.setLoading(!0);
			try {
				await e;
			} finally {
				this.setLoading(!1);
			}
		}
		this.close();
	}
	onKeyDown(e) {
		e.key === "Escape" && this.close();
	}
};
//#endregion
export { r as Modal };

//# sourceMappingURL=index.js.map