import { i as e } from "./sanitize-CDxuPfMB.js";
import { n as t, t as n } from "./utils-C_mWfcFX.js";
//#region src/primitives/toggle/toggle-group.ts
var r = class {
	el;
	groupId;
	groupType;
	orientation;
	toggles = [];
	selectedValues;
	isDisabled = !1;
	onChangeCallback;
	constructor(e) {
		if (this.groupId = t("cmm-toggle-group"), this.groupType = e.type ?? "single", this.orientation = e.orientation ?? "horizontal", this.onChangeCallback = e.onChange, this.selectedValues = /* @__PURE__ */ new Set(), e.value !== void 0) if (Array.isArray(e.value)) for (let t of e.value) this.selectedValues.add(t);
		else this.selectedValues.add(e.value);
		this.el = document.createElement("div"), this.el.id = this.groupId, this.el.className = this.buildClassName(e.className), this.el.setAttribute("role", "group"), e.ariaLabel !== void 0 && this.el.setAttribute("aria-label", e.ariaLabel), e.disabled === !0 && (this.isDisabled = !0), e.container !== void 0 && n(e.container).appendChild(this.el);
	}
	addItem(t) {
		let n = document.createElement("button");
		if (n.type = "button", n.className = "cmm-toggle cmm-toggle--outline", n.dataset.value = t.value, n.setAttribute("aria-pressed", String(this.selectedValues.has(t.value))), t.ariaLabel !== void 0 && n.setAttribute("aria-label", t.ariaLabel), (t.disabled === !0 || this.isDisabled) && (n.disabled = !0), t.icon !== void 0) {
			let r = document.createElement("span");
			r.className = "cmm-toggle__icon", e(r, t.icon), n.appendChild(r);
		}
		if (t.label !== void 0) {
			let e = document.createElement("span");
			e.className = "cmm-toggle__label", e.textContent = t.label, n.appendChild(e);
		}
		this.selectedValues.has(t.value) && n.classList.add("cmm-toggle--pressed");
		let r = () => {
			this.handleToggleClick(t.value);
		};
		n.addEventListener("click", r);
		let i = {
			value: t.value,
			el: n,
			handler: r
		};
		this.toggles.push(i), this.el.appendChild(n);
	}
	setItems(e) {
		this.clearItems();
		for (let t of e) this.addItem(t);
	}
	getValue() {
		return this.groupType === "single" ? [...this.selectedValues][0] ?? "" : [...this.selectedValues];
	}
	setValue(e) {
		if (this.selectedValues.clear(), Array.isArray(e)) for (let t of e) this.selectedValues.add(t);
		else this.selectedValues.add(e);
		this.syncToggles();
	}
	setDisabled(e) {
		this.isDisabled = e;
		for (let t of this.toggles) t.el.disabled = e;
	}
	destroy() {
		for (let e of this.toggles) e.el.removeEventListener("click", e.handler);
		this.toggles.length = 0, this.el.remove();
	}
	handleToggleClick(e) {
		this.groupType === "single" ? this.selectedValues.has(e) ? this.selectedValues.delete(e) : (this.selectedValues.clear(), this.selectedValues.add(e)) : this.selectedValues.has(e) ? this.selectedValues.delete(e) : this.selectedValues.add(e), this.syncToggles(), this.onChangeCallback?.(this.getValue());
	}
	syncToggles() {
		for (let e of this.toggles) {
			let t = this.selectedValues.has(e.value);
			e.el.setAttribute("aria-pressed", String(t)), t ? e.el.classList.add("cmm-toggle--pressed") : e.el.classList.remove("cmm-toggle--pressed");
		}
	}
	clearItems() {
		for (let e of this.toggles) e.el.removeEventListener("click", e.handler), e.el.remove();
		this.toggles.length = 0;
	}
	buildClassName(e) {
		let t = ["cmm-toggle-group", `cmm-toggle-group--${this.orientation}`];
		return e && t.push(e), t.join(" ");
	}
}, i = class {
	el;
	toggleId;
	currentVariant;
	pressed;
	clickHandler;
	onChangeCallback;
	constructor(r) {
		if (this.toggleId = t("cmm-toggle"), this.currentVariant = r.variant ?? "default", this.pressed = r.pressed === !0, this.onChangeCallback = r.onChange, this.el = document.createElement("button"), this.el.type = "button", this.el.id = this.toggleId, this.el.className = this.buildClassName(r.className), this.el.setAttribute("aria-pressed", String(this.pressed)), r.ariaLabel !== void 0 && this.el.setAttribute("aria-label", r.ariaLabel), r.icon !== void 0) {
			let t = document.createElement("span");
			t.className = "cmm-toggle__icon", e(t, r.icon), this.el.appendChild(t);
		}
		if (r.label !== void 0) {
			let e = document.createElement("span");
			e.className = "cmm-toggle__label", e.textContent = r.label, this.el.appendChild(e);
		}
		r.disabled === !0 && (this.el.disabled = !0), this.clickHandler = () => {
			this.pressed = !this.pressed, this.updatePressedState(), this.onChangeCallback?.(this.pressed);
		}, this.el.addEventListener("click", this.clickHandler), r.container !== void 0 && n(r.container).appendChild(this.el);
	}
	isPressed() {
		return this.pressed;
	}
	setPressed(e) {
		e !== this.pressed && (this.pressed = e, this.updatePressedState());
	}
	setDisabled(e) {
		this.el.disabled = e;
	}
	setVariant(e) {
		this.el.classList.remove(`cmm-toggle--${this.currentVariant}`), this.currentVariant = e, this.el.classList.add(`cmm-toggle--${this.currentVariant}`);
	}
	destroy() {
		this.el.removeEventListener("click", this.clickHandler), this.el.remove();
	}
	updatePressedState() {
		this.el.setAttribute("aria-pressed", String(this.pressed)), this.pressed ? this.el.classList.add("cmm-toggle--pressed") : this.el.classList.remove("cmm-toggle--pressed");
	}
	buildClassName(e) {
		let t = ["cmm-toggle", `cmm-toggle--${this.currentVariant}`];
		return this.pressed && t.push("cmm-toggle--pressed"), e && t.push(e), t.join(" ");
	}
};
//#endregion
export { r as n, i as t };

//# sourceMappingURL=toggle-D-WMiAg3.js.map