import { n as e, t } from "../../utils-C_mWfcFX.js";
//#region src/primitives/radio/index.ts
var n = class {
	el;
	radioEls = [];
	labelEls = [];
	config;
	groupName;
	selectedValue = null;
	isDestroyed = !1;
	handleChange;
	handleKeydown;
	constructor(n) {
		this.config = n, this.groupName = n.name ?? e("cmm-radio-group"), n.value !== void 0 && (this.selectedValue = n.value);
		let r = n.orientation ?? "vertical";
		this.el = document.createElement("div"), this.el.className = `cmm-radio-group cmm-radio-group--${r}`, this.el.setAttribute("role", "radiogroup"), this.el.id = e("cmm-radio-group"), n.ariaLabel && this.el.setAttribute("aria-label", n.ariaLabel), n.className && this.el.classList.add(n.className), this.buildRadios(n.options), this.handleChange = (e) => {
			if (this.isDestroyed) return;
			let t = e.target;
			this.selectedValue = t.value, this.config.onChange?.(t.value);
		}, this.handleKeydown = (e) => {
			this.isDestroyed || this.onKeydown(e);
		}, this.el.addEventListener("change", this.handleChange), this.el.addEventListener("keydown", this.handleKeydown), n.container !== void 0 && t(n.container).appendChild(this.el);
	}
	getValue() {
		return this.selectedValue;
	}
	setValue(e) {
		this.selectedValue = e;
		for (let t of this.radioEls) t.checked = t.value === e;
	}
	setDisabled(e) {
		for (let t = 0; t < this.radioEls.length; t++) {
			let n = this.radioEls[t], r = this.labelEls[t];
			n !== void 0 && (n.disabled = e), r !== void 0 && (e ? r.classList.add("cmm-radio--disabled") : r.classList.remove("cmm-radio--disabled"));
		}
	}
	destroy() {
		this.isDestroyed = !0, this.el.removeEventListener("change", this.handleChange), this.el.removeEventListener("keydown", this.handleKeydown), this.el.remove();
	}
	buildRadios(t) {
		for (let n of t) {
			let t = document.createElement("label");
			t.className = "cmm-radio";
			let r = this.config.disabled === !0 || n.disabled === !0;
			r && t.classList.add("cmm-radio--disabled");
			let i = document.createElement("input");
			i.type = "radio", i.className = "cmm-radio__input", i.name = this.groupName, i.value = n.value, i.id = e("cmm-radio"), n.value === this.selectedValue && (i.checked = !0), r && (i.disabled = !0);
			let a = document.createElement("span");
			a.className = "cmm-radio__circle", a.setAttribute("aria-hidden", "true");
			let o = document.createElement("span");
			o.className = "cmm-radio__label", o.textContent = n.label, t.appendChild(i), t.appendChild(a), t.appendChild(o), this.el.appendChild(t), this.radioEls.push(i), this.labelEls.push(t);
		}
	}
	onKeydown(e) {
		let t = this.radioEls.filter((e) => !e.disabled);
		if (t.length === 0) return;
		let n = -1, r = document.activeElement;
		r !== null && (n = t.indexOf(r));
		let i;
		switch (e.key) {
			case "ArrowDown":
			case "ArrowRight":
				e.preventDefault(), i = n < 0 ? 0 : n < t.length - 1 ? n + 1 : 0;
				break;
			case "ArrowUp":
			case "ArrowLeft":
				e.preventDefault(), i = n < 0 ? t.length - 1 : n > 0 ? n - 1 : t.length - 1;
				break;
			default: return;
		}
		let a = t[i];
		a !== void 0 && (a.checked = !0, a.focus(), this.selectedValue = a.value, this.config.onChange?.(a.value));
	}
};
//#endregion
export { n as RadioGroup };

//# sourceMappingURL=index.js.map