import { n as e, t } from "../../utils-C_mWfcFX.js";
//#region src/primitives/slider/index.ts
var n = class {
	el;
	trackEl;
	fillEl;
	thumbEl;
	tooltipEl = null;
	ticksEl = null;
	sliderId;
	min;
	max;
	step;
	currentValue;
	isDragging = !1;
	showTooltip;
	showTicks;
	onChangeCallback;
	onChangeEndCallback;
	handlePointerDown;
	handlePointerMove;
	handlePointerUp;
	handleKeyDown;
	handleTrackClick;
	constructor(n) {
		this.sliderId = e("cmm-slider"), this.min = n.min ?? 0, this.max = n.max ?? 100, this.step = n.step ?? 1, this.showTooltip = n.showTooltip === !0, this.showTicks = n.showTicks === !0, this.onChangeCallback = n.onChange, this.onChangeEndCallback = n.onChangeEnd, this.max <= this.min && (this.max = this.min + 1), this.currentValue = this.clamp(n.value ?? this.min), this.handlePointerDown = this.onPointerDown.bind(this), this.handlePointerMove = this.onPointerMove.bind(this), this.handlePointerUp = this.onPointerUp.bind(this), this.handleKeyDown = this.onKeyDown.bind(this), this.handleTrackClick = this.onTrackClick.bind(this), this.el = document.createElement("div"), this.el.className = this.buildClassName(n.className), this.el.id = this.sliderId, this.trackEl = document.createElement("div"), this.trackEl.className = "cmm-slider__track", this.fillEl = document.createElement("div"), this.fillEl.className = "cmm-slider__fill", this.thumbEl = document.createElement("div"), this.thumbEl.className = "cmm-slider__thumb", this.thumbEl.tabIndex = 0, this.thumbEl.setAttribute("role", "slider"), this.thumbEl.setAttribute("aria-valuemin", String(this.min)), this.thumbEl.setAttribute("aria-valuemax", String(this.max)), this.thumbEl.setAttribute("aria-valuenow", String(this.currentValue)), n.ariaLabel !== void 0 && this.thumbEl.setAttribute("aria-label", n.ariaLabel), this.trackEl.appendChild(this.fillEl), this.trackEl.appendChild(this.thumbEl), this.el.appendChild(this.trackEl), this.showTooltip && (this.tooltipEl = document.createElement("div"), this.tooltipEl.className = "cmm-slider__tooltip", this.tooltipEl.textContent = String(this.currentValue), this.thumbEl.appendChild(this.tooltipEl)), this.showTicks && this.buildTicks(), this.updatePosition(), n.disabled === !0 && this.setDisabled(!0), this.thumbEl.addEventListener("pointerdown", this.handlePointerDown), this.trackEl.addEventListener("pointerdown", this.handleTrackClick), this.thumbEl.addEventListener("keydown", this.handleKeyDown), n.container !== void 0 && t(n.container).appendChild(this.el);
	}
	getValue() {
		return this.currentValue;
	}
	setValue(e) {
		let t = this.clamp(e);
		t !== this.currentValue && (this.currentValue = t, this.updatePosition(), this.onChangeCallback?.(this.currentValue));
	}
	setDisabled(e) {
		e ? (this.el.classList.add("cmm-slider--disabled"), this.thumbEl.setAttribute("aria-disabled", "true"), this.thumbEl.tabIndex = -1) : (this.el.classList.remove("cmm-slider--disabled"), this.thumbEl.removeAttribute("aria-disabled"), this.thumbEl.tabIndex = 0);
	}
	setRange(e, t) {
		this.min = e, this.max = t > e ? t : e + 1, this.currentValue = this.clamp(this.currentValue), this.thumbEl.setAttribute("aria-valuemin", String(this.min)), this.thumbEl.setAttribute("aria-valuemax", String(this.max)), this.updatePosition(), this.showTicks && this.buildTicks();
	}
	destroy() {
		document.removeEventListener("pointermove", this.handlePointerMove), document.removeEventListener("pointerup", this.handlePointerUp), this.thumbEl.removeEventListener("pointerdown", this.handlePointerDown), this.trackEl.removeEventListener("pointerdown", this.handleTrackClick), this.thumbEl.removeEventListener("keydown", this.handleKeyDown), this.el.remove();
	}
	buildClassName(e) {
		let t = ["cmm-slider"];
		return e && t.push(e), t.join(" ");
	}
	getPercent() {
		let e = this.max - this.min;
		return e <= 0 ? 0 : (this.currentValue - this.min) / e * 100;
	}
	updatePosition() {
		let e = this.getPercent();
		this.fillEl.style.width = `${String(e)}%`, this.thumbEl.style.left = `${String(e)}%`, this.thumbEl.setAttribute("aria-valuenow", String(this.currentValue)), this.tooltipEl !== null && (this.tooltipEl.textContent = String(this.currentValue));
	}
	clamp(e) {
		let t = Math.round((e - this.min) / this.step) * this.step + this.min;
		return Math.min(Math.max(t, this.min), this.max);
	}
	getValueFromPointer(e) {
		let t = this.trackEl.getBoundingClientRect(), n = e.clientX - t.left, r = Math.min(Math.max(n / t.width, 0), 1);
		return this.clamp(this.min + r * (this.max - this.min));
	}
	onPointerDown(e) {
		e.preventDefault(), e.stopPropagation(), this.isDragging = !0, this.thumbEl.setPointerCapture(e.pointerId), document.addEventListener("pointermove", this.handlePointerMove), document.addEventListener("pointerup", this.handlePointerUp), this.tooltipEl !== null && this.tooltipEl.classList.add("cmm-slider__tooltip--visible");
	}
	onPointerMove(e) {
		if (!this.isDragging) return;
		let t = this.getValueFromPointer(e);
		t !== this.currentValue && (this.currentValue = t, this.updatePosition(), this.onChangeCallback?.(this.currentValue));
	}
	onPointerUp(e) {
		this.isDragging && (this.isDragging = !1, document.removeEventListener("pointermove", this.handlePointerMove), document.removeEventListener("pointerup", this.handlePointerUp), this.tooltipEl !== null && this.tooltipEl.classList.remove("cmm-slider__tooltip--visible"), this.onChangeEndCallback?.(this.currentValue));
	}
	onTrackClick(e) {
		if (e.target === this.thumbEl) return;
		let t = this.getValueFromPointer(e);
		t !== this.currentValue && (this.currentValue = t, this.updatePosition(), this.onChangeCallback?.(this.currentValue), this.onChangeEndCallback?.(this.currentValue));
	}
	onKeyDown(e) {
		let t;
		switch (e.key) {
			case "ArrowRight":
			case "ArrowUp":
				t = this.clamp(this.currentValue + this.step);
				break;
			case "ArrowLeft":
			case "ArrowDown":
				t = this.clamp(this.currentValue - this.step);
				break;
			case "Home":
				t = this.min;
				break;
			case "End":
				t = this.max;
				break;
			default: return;
		}
		e.preventDefault(), t !== this.currentValue && (this.currentValue = t, this.updatePosition(), this.onChangeCallback?.(this.currentValue), this.onChangeEndCallback?.(this.currentValue));
	}
	buildTicks() {
		this.ticksEl !== null && this.ticksEl.remove(), this.ticksEl = document.createElement("div"), this.ticksEl.className = "cmm-slider__ticks";
		let e = this.max - this.min;
		if (e <= 0) return;
		let t = Math.floor(e / this.step);
		for (let e = 0; e <= t; e++) {
			let n = document.createElement("div");
			n.className = "cmm-slider__tick";
			let r = e / t * 100;
			n.style.left = `${String(r)}%`, this.ticksEl.appendChild(n);
		}
		this.el.appendChild(this.ticksEl);
	}
};
//#endregion
export { n as Slider };

//# sourceMappingURL=index.js.map