import { n as e, r as t } from "../chunks/popover-xJJqy1v6.js";
import { t as n } from "../chunks/ui.module-BisoTevy.js";
import { i as r, l as i, o as a, s as o } from "../chunks/input-CZLUUggm.js";
import { l as s } from "../chunks/picker-beNX-cW9.js";
import { useCallback as c, useEffect as l, useEffectEvent as u, useMemo as d, useRef as f, useState as p } from "react";
import { cn as ee } from "@ryuzaki13/react-foundation-lib/utils";
import { toFiniteNumber as m } from "@ryuzaki13/react-foundation-lib/formatters";
import { Fragment as h, jsx as g, jsxs as _ } from "react/jsx-runtime";
import { findClosestNumberScaleMark as v, findClosestNumberScaleMarkByPercent as y, getNumberScaleMarkPercent as b, offsetNumberScaleValue as x, percentToNumberScaleValue as S, prepareNumberScaleMarks as C, resolveNumberScaleBounds as w, snapNumberScaleValue as T, snapNumberScaleValueToStep as E, valueToNumberScalePercent as D } from "@ryuzaki13/react-foundation-lib/number-scale";
import "@ryuzaki13/react-foundation-lib/range-output";
//#region src/slider/lib/sliderMath.ts
function O(e, t, n) {
	return C(e, t, n);
}
function k(e, t, n) {
	let r = t.findIndex((t) => t.value === e.value);
	return b(Math.max(0, r), t, n.min, n.max, n.marksPosition);
}
function A(e, t) {
	let { min: n, max: r } = w(t.min, t.max), i = O(t.marks, n, r);
	if (i.length > 0) {
		let a = i.find((t) => t.value === e) ?? v(e, i);
		if (a) return k(a, i, {
			...t,
			min: n,
			max: r,
			marks: i
		});
	}
	return D(e, n, r);
}
function j(e, t) {
	let { min: n, max: r } = w(t.min, t.max);
	return y(e, O(t.marks, n, r), n, r, t.marksPosition);
}
function M(e, t) {
	return T(e, t);
}
function N(e, t) {
	let { min: n, max: r } = w(t.min, t.max);
	return e === void 0 ? n : M(e, {
		...t,
		min: n,
		max: r
	});
}
function P(e) {
	if (e) return !Object.hasOwn(e, "outputValue") || e.outputValue === void 0 ? e.value : e.outputValue;
}
function F(e, t, n) {
	let r = t.filter((t) => P(t) === e);
	if (r.length) return e === null ? n === 0 ? r[0] : r[r.length - 1] : r[0];
}
function I(e, t, n) {
	let r = F(e, t, n);
	if (r) return r;
	let i, a = Infinity;
	for (let n of t) {
		let t = P(n);
		if (typeof t != "number" || !Number.isFinite(t)) continue;
		let r = Math.abs(e - t);
		r < a && (i = n, a = r);
	}
	return i;
}
function L(e, t, n, r) {
	if (e === null) return null;
	let { min: i, max: a } = w(r.min, r.max), o = O(r.marks, i, a), s = Number.isFinite(e) ? e : t;
	if (o.length > 0) {
		let e = I(s, o, n), r = P(e);
		return r === void 0 ? e?.value ?? t : r;
	}
	return E(s, i, a, r.step);
}
function R(e, t) {
	let { min: n, max: r } = w(t.min, t.max);
	if (!e) return [null, null];
	let i = L(e[0], n, 0, {
		...t,
		min: n,
		max: r
	}), a = L(e[1], r, 1, {
		...t,
		min: n,
		max: r
	});
	return i !== null && a !== null && i > a ? [a, i] : [i, a];
}
function z(e, t, n, r) {
	if (e === null) return t;
	let { min: i, max: a } = w(r.min, r.max), o = O(r.marks, i, a);
	return o.length > 0 ? (F(e, o, n) ?? (typeof e == "number" ? I(e, o, n) : void 0))?.value ?? t : E(e, i, a, r.step);
}
function te(e, t) {
	let { min: n, max: r } = w(t.min, t.max), i = R(e, {
		...t,
		min: n,
		max: r
	}), a = z(i[0], n, 0, {
		...t,
		min: n,
		max: r
	}), o = z(i[1], r, 1, {
		...t,
		min: n,
		max: r
	});
	return a <= o ? [a, o] : [o, a];
}
function B(e, t) {
	let { min: n, max: r } = w(t.min, t.max), i = O(t.marks, n, r);
	if (i.length > 0) {
		let t = v(e, i), n = P(t);
		return n === void 0 ? t?.value ?? e : n;
	}
	return E(e, n, r, t.step);
}
function V(e, t) {
	let [n, r] = t;
	return Math.abs(e - n) <= Math.abs(e - r) ? 0 : 1;
}
function H(e, t) {
	let { min: n, max: r } = w(t.min, t.max);
	return Array.isArray(e) ? {
		startPercent: A(e[0], {
			...t,
			min: n,
			max: r
		}),
		endPercent: A(e[1], {
			...t,
			min: n,
			max: r
		})
	} : {
		startPercent: 0,
		endPercent: A(e, {
			...t,
			min: n,
			max: r
		})
	};
}
var U = {
	slider: "fA7zne",
	sliderCompact: "TMbaqW",
	trackShell: "_77qn1L",
	track: "cxJQqh",
	rail: "gg-atO",
	fill: "bTLJZl",
	thumb: "vDAmRy",
	marks: "iNrV0y",
	mark: "PpaxP2",
	compactMark: "_9K78Fi",
	markDot: "vzEDBe",
	markLabel: "_0OWJh0",
	popoverContent: "Mk4xi2",
	rangeInputControl: "SL0IDQ",
	rangeSegmentInput: "mdezg5",
	rangeSeparator: "a4R18g"
};
//#endregion
//#region src/slider/Slider.tsx
function ne(e) {
	return e === "ArrowLeft" || e === "ArrowDown" ? -1 : e === "ArrowRight" || e === "ArrowUp" ? 1 : e === "PageDown" ? -10 : e === "PageUp" ? 10 : null;
}
function W(e, t) {
	return !!e && e[0] === t[0] && e[1] === t[1];
}
function re(e, t) {
	return e === null ? t === 0 ? "-∞" : "∞" : String(e);
}
function ie(e, t) {
	return t.find((t) => t.value === e)?.label ?? String(e);
}
function ae(e) {
	return typeof e == "string" || typeof e == "number" ? /* @__PURE__ */ g("span", {
		className: "textNoWrap",
		children: e
	}) : e;
}
function G(t) {
	let { min: n, max: r, step: a, marks: s, marksPosition: c = "value", marksDisplay: u = "compact", className: m, error: h, onClearError: v, label: y, description: b, disabled: C, size: T } = t, { min: E, max: D } = d(() => w(n, r), [n, r]), M = d(() => O(s, E, D), [
		s,
		E,
		D
	]), P = d(() => ({
		min: E,
		max: D,
		step: a,
		marks: M,
		marksPosition: c
	}), [
		E,
		D,
		a,
		M,
		c
	]), F = t.mode === "range", I = F ? void 0 : N(t.value, P), L = F ? R(t.value, P) : void 0, z = F ? te(t.value, P) : void 0, G = F ? z : [I], K = H(F ? z : I, P), q = f(null), J = f([]), Y = f(null), X = f(null), [oe, Z] = p(null), { controlId: Q, labelId: se, descriptionId: ce, errorId: le, describedBy: ue } = i({
		hasLabel: y != null,
		hasDescription: !!b,
		hasError: !!h
	}), de = (e) => {
		C || t.mode !== "single" || t.value !== void 0 && t.value === e || (t.onChange(e), v?.());
	}, fe = (e) => {
		C || t.mode !== "range" || W(t.value, e) || (t.onChange(e), v?.());
	}, $ = (e, n) => {
		if (!C) {
			if (t.mode === "range") {
				let [t, r] = z, [i, a] = L, o = R(n === 0 ? [B(Math.min(e, r), P), a] : [i, B(Math.max(e, t), P)], P);
				fe(o);
				return;
			}
			de(N(e, P));
		}
	}, pe = (e) => {
		let t = q.current?.getBoundingClientRect();
		if (!t) return E;
		let n = (e - t.left) / t.width * 100;
		return (M.length > 0 ? j(n, P) : void 0)?.value ?? S(n, E, D);
	}, me = f($), he = f(pe);
	l(() => {
		me.current = $, he.current = pe;
	}), l(() => {
		let e = (e) => {
			X.current !== null && e && e.pointerId !== X.current || (X.current = null, Y.current = null, Z(null));
		}, t = (e) => {
			Y.current !== null && (X.current !== null && e.pointerId !== X.current || me.current(he.current(e.clientX), Y.current));
		};
		return window.addEventListener("pointermove", t), window.addEventListener("pointerup", e), window.addEventListener("pointercancel", e), () => {
			window.removeEventListener("pointermove", t), window.removeEventListener("pointerup", e), window.removeEventListener("pointercancel", e);
		};
	}, []);
	let ge = (e, t) => {
		X.current = e, Y.current = t, Z(t), J.current[t]?.focus();
	}, _e = (e) => {
		if (C) return;
		e.preventDefault();
		let t = pe(e.clientX), n = F ? V(t, z) : 0;
		$(t, n), ge(e.pointerId, n);
	}, ve = (e) => (t) => {
		C || (t.preventDefault(), t.stopPropagation(), ge(t.pointerId, e));
	}, ye = (e) => (t) => {
		if (C) return;
		let n = F && e === 1 ? z[0] : E, r = F && e === 0 ? z[1] : D, i = ne(t.key);
		if (t.key === "Home") {
			t.preventDefault(), $(n, e);
			return;
		}
		if (t.key === "End") {
			t.preventDefault(), $(r, e);
			return;
		}
		if (i === null) return;
		t.preventDefault();
		let a = G[e], o = x(a, i, P, n, r);
		$(o, e);
	}, be = (e) => {
		if (C) return;
		let t = F ? V(e, z) : 0;
		$(e, t), J.current[t]?.focus();
	}, xe = (e, t) => {
		if (F) {
			let n = L[t];
			return M.find((t) => t.value === e)?.label ?? re(n, t);
		}
		return ie(e, M);
	};
	return /* @__PURE__ */ g(o, {
		label: y,
		description: b,
		disabled: C,
		className: m,
		size: T,
		error: h,
		controlId: Q,
		labelId: se,
		descriptionId: ce,
		errorId: le,
		children: /* @__PURE__ */ _("div", {
			id: Q,
			className: ee(U.slider, u === "compact" && U.sliderCompact),
			children: [/* @__PURE__ */ g("div", {
				className: U.trackShell,
				children: /* @__PURE__ */ _("div", {
					ref: q,
					"data-slot": "track",
					className: U.track,
					role: "presentation",
					onPointerDown: _e,
					children: [
						/* @__PURE__ */ g("div", { className: U.rail }),
						/* @__PURE__ */ g("div", {
							"data-slot": "fill",
							className: U.fill,
							style: {
								left: `${K.startPercent}%`,
								width: `${K.endPercent - K.startPercent}%`
							}
						}),
						u === "compact" && M.map((t) => {
							let n = k(t, M, P), r = F ? t.value >= z[0] && t.value <= z[1] : t.value <= I;
							return /* @__PURE__ */ g(e, {
								tooltip: !0,
								placement: "top",
								content: ae(t.label),
								children: /* @__PURE__ */ g("button", {
									type: "button",
									"data-slot": "mark",
									"data-active": r ? "" : void 0,
									className: U.compactMark,
									style: { left: `${n}%` },
									"aria-label": t.ariaLabel,
									disabled: C,
									onClick: () => be(t.value),
									children: /* @__PURE__ */ g("span", {
										className: U.markDot,
										"aria-hidden": "true"
									})
								})
							}, t.value);
						}),
						G.map((t, n) => {
							let r = xe(t, n);
							return /* @__PURE__ */ g(e, {
								tooltip: !0,
								placement: "top",
								content: ae(r),
								openDelay: 0,
								closeDelay: 100,
								children: /* @__PURE__ */ g("button", {
									ref: (e) => {
										J.current[n] = e;
									},
									type: "button",
									role: "slider",
									"data-slot": "thumb",
									"data-thumb-index": n,
									"data-dragging": oe === n ? "" : void 0,
									className: U.thumb,
									disabled: C,
									style: { left: `${A(t, P)}%` },
									"aria-disabled": C || void 0,
									"aria-orientation": "horizontal",
									"aria-valuemin": F && n === 1 ? z[0] : E,
									"aria-valuemax": F && n === 0 ? z[1] : D,
									"aria-valuenow": t,
									"aria-valuetext": typeof r == "string" || typeof r == "number" ? String(r) : void 0,
									"aria-labelledby": se,
									"aria-describedby": ue,
									"aria-label": se ? void 0 : F ? n === 0 ? "Минимальное значение" : "Максимальное значение" : "Значение",
									onPointerDown: ve(n),
									onKeyDown: ye(n)
								})
							}, n);
						})
					]
				})
			}), u === "full" && M.length > 0 && /* @__PURE__ */ g("div", {
				className: U.marks,
				children: M.map((e) => /* @__PURE__ */ _("button", {
					type: "button",
					"data-slot": "mark",
					"data-active": (F ? e.value >= z[0] && e.value <= z[1] : e.value <= I) ? "" : void 0,
					className: U.mark,
					style: { left: `${k(e, M, P)}%` },
					"aria-label": e.ariaLabel,
					disabled: C,
					onClick: () => be(e.value),
					children: [/* @__PURE__ */ g("span", {
						className: U.markDot,
						"aria-hidden": "true"
					}), /* @__PURE__ */ g("span", {
						className: U.markLabel,
						children: e.label
					})]
				}, e.value))
			})]
		})
	});
}
function K(e) {
	return /* @__PURE__ */ g(G, {
		...e,
		mode: "single",
		marksDisplay: "compact"
	});
}
function q(e) {
	return /* @__PURE__ */ g(G, {
		...e,
		mode: "range",
		marksDisplay: "compact"
	});
}
//#endregion
//#region src/slider/SliderInput.tsx
function J({ open: e, disabled: n, setOpen: r }) {
	return /* @__PURE__ */ g(t.Trigger, {
		passive: !0,
		children: /* @__PURE__ */ g("div", { children: /* @__PURE__ */ g(s, {
			open: e,
			disabled: n,
			openAriaLabel: "Открыть слайдер",
			closeAriaLabel: "Закрыть слайдер",
			onToggleMouseDown: (e) => e.preventDefault(),
			onToggleClick: () => r((e) => !e)
		}) })
	});
}
function Y(e, t) {
	let n = R(e, t);
	return {
		start: n[0] === null ? "-∞" : String(n[0]),
		end: n[1] === null ? "∞" : String(n[1])
	};
}
function X(e, t) {
	let n = e.trim();
	return n === "" || n === t ? null : m(n);
}
function oe(e, t, n) {
	let r = t.filter((t) => P(t) === e);
	if (r.length) return e === null && n === "end" ? r[r.length - 1] : r[0];
}
function Z(e, t) {
	return e?.readonlyLabel?.trim() ? e.readonlyLabel.trim() : typeof e?.label == "string" || typeof e?.label == "number" ? String(e.label) : t === null ? "" : String(t);
}
function Q(e, t, n) {
	return Z(oe(e, t, n), e);
}
function se(e, t, n) {
	let [r, i] = e, a = r === null, o = i === null;
	return a && o ? n ?? "" : a ? `Меньше ${Q(i, t, "end")}` : o ? `Больше ${Q(r, t, "start")}` : r === i ? `Равно ${Q(r, t, "start")}` : `От ${Q(r, t, "start")} до ${Q(i, t, "end")}`;
}
function ce({ value: e, onChange: n, min: r, max: i, step: o, marks: s, marksPosition: f = "value", className: ee, error: h, onClearError: v, label: y, description: b, placeholder: x, disabled: S, size: C }) {
	let { min: T, max: E } = d(() => w(r, i), [r, i]), D = d(() => O(s, T, E), [
		s,
		T,
		E
	]), k = d(() => ({
		min: T,
		max: E,
		step: o,
		marks: D,
		marksPosition: f
	}), [
		T,
		E,
		o,
		D,
		f
	]), A = N(e, k), [j, M] = p(!1), [P, F] = p(String(A)), I = u((e) => {
		F(e);
	});
	l(() => {
		I(String(A));
	}, [A]);
	let L = c(() => {
		if (S) {
			F(String(A));
			return;
		}
		let t = m(P);
		if (t === void 0) {
			F(String(A));
			return;
		}
		let r = N(t, k);
		e !== r && (n(r), v?.()), F(String(r));
	}, [
		S,
		P,
		A,
		n,
		v,
		k,
		e
	]);
	return /* @__PURE__ */ _(t, {
		open: j,
		onOpenChange: M,
		children: [/* @__PURE__ */ g(a, {
			label: y,
			description: b,
			placeholder: x,
			disabled: S,
			size: C,
			className: ee,
			error: h,
			value: P,
			onChange: F,
			onBlur: L,
			onKeyDown: (e) => {
				e.key === "Enter" && (e.preventDefault(), L());
			},
			endAdornmentWidth: "var(--control-height)",
			endAdornment: J({
				open: j,
				disabled: S,
				setOpen: M
			})
		}), /* @__PURE__ */ g(t.Content, {
			background: "primary",
			children: /* @__PURE__ */ g("div", {
				className: U.popoverContent,
				children: /* @__PURE__ */ g(G, {
					mode: "single",
					min: T,
					max: E,
					step: o,
					marks: D,
					marksPosition: f,
					marksDisplay: "full",
					value: e,
					onChange: (e) => {
						n(e), F(String(e)), v?.();
					},
					disabled: S
				})
			})
		})]
	});
}
function le({ value: e, onChange: a, min: s, max: f, step: m, marks: v, marksPosition: y = "value", className: b, error: x, onClearError: S, label: C, description: T, readonlyValueText: E, placeholder: D, disabled: k, size: A }) {
	let { min: j, max: M } = d(() => w(s, f), [s, f]), N = d(() => O(v, j, M), [
		v,
		j,
		M
	]), P = d(() => ({
		min: j,
		max: M,
		step: m,
		marks: N,
		marksPosition: y
	}), [
		j,
		M,
		m,
		N,
		y
	]), F = d(() => R(e, P), [P, e]), I = d(() => Y(F, P), [F, P]), [L, z] = p(!1), [te, B] = p(I.start), [V, H] = p(I.end), [ne, W] = p(F), re = u((e, t) => {
		B(e), H(t);
	});
	l(() => {
		L || re(I.start, I.end);
	}, [
		I.start,
		I.end,
		F,
		L
	]);
	let { controlId: ie, labelId: ae, descriptionId: K, errorId: q, describedBy: oe } = i({
		hasLabel: C != null,
		hasDescription: !!T,
		hasError: !!x
	}), Z = c(() => {
		B(I.start), H(I.end);
	}, [I.end, I.start]), Q = c(() => {
		if (k) {
			Z();
			return;
		}
		let e = X(te, "-∞"), t = X(V, "∞");
		if (e === void 0 || t === void 0) {
			Z();
			return;
		}
		let n = R([e, t], P);
		ue(F, n) || (a(n), S?.());
		let r = Y(n, P);
		B(r.start), H(r.end), W(n);
	}, [
		k,
		V,
		te,
		F,
		a,
		S,
		Z,
		P
	]), ce = c((e) => {
		let t = R(e, P);
		ue(F, t) || (a(t), S?.());
		let n = Y(t, P);
		B(n.start), H(n.end), W(t);
	}, [
		F,
		a,
		S,
		P
	]), le = (e) => {
		!L && e && (W(F), B(I.start), H(I.end)), L && !e && !k && ce(ne), z(e);
	}, de = (e) => {
		e.key === "Enter" && (e.preventDefault(), Q());
	}, fe = (e) => {
		let t = R(e, P);
		W(t);
		let n = Y(t, P);
		B(n.start), H(n.end);
	}, $ = E ? se(L ? ne : F, N, D) : void 0;
	return /* @__PURE__ */ _(t, {
		open: L,
		onOpenChange: le,
		children: [/* @__PURE__ */ g(o, {
			label: C,
			description: T,
			disabled: k,
			className: b,
			size: A,
			error: x,
			controlId: ie,
			labelId: ae,
			descriptionId: K,
			errorId: q,
			children: /* @__PURE__ */ g(r, {
				endAdornmentWidth: "var(--control-height)",
				endAdornment: J({
					open: L,
					disabled: k,
					setOpen: (e) => {
						le(typeof e == "function" ? e(L) : e);
					}
				}),
				children: ({ controlClassName: e }) => /* @__PURE__ */ g("div", {
					id: ie,
					className: ee(n.uiInputControlFake, U.rangeInputControl, e),
					"data-invalid": x ? "" : void 0,
					"data-disabled": k || void 0,
					role: "group",
					"aria-invalid": !!x || void 0,
					"aria-label": typeof C == "string" ? C : "Диапазон",
					"aria-labelledby": typeof C == "string" ? void 0 : ae,
					"aria-describedby": oe,
					children: E ? /* @__PURE__ */ g("span", {
						className: ee("flexEllipsis", !$ && n.uiPlaceholder),
						children: $ || D
					}) : /* @__PURE__ */ _(h, { children: [
						/* @__PURE__ */ g("input", {
							type: "text",
							inputMode: "decimal",
							className: U.rangeSegmentInput,
							value: te,
							disabled: k,
							"aria-label": "Начало диапазона",
							onChange: (e) => B(e.target.value),
							onBlur: Q,
							onKeyDown: de
						}),
						/* @__PURE__ */ g("span", {
							className: U.rangeSeparator,
							"data-disabled": k || void 0,
							children: "-"
						}),
						/* @__PURE__ */ g("input", {
							type: "text",
							inputMode: "decimal",
							className: U.rangeSegmentInput,
							value: V,
							disabled: k,
							"aria-label": "Конец диапазона",
							onChange: (e) => H(e.target.value),
							onBlur: Q,
							onKeyDown: de
						})
					] })
				})
			})
		}), /* @__PURE__ */ g(t.Content, {
			background: "primary",
			children: /* @__PURE__ */ g("div", {
				className: U.popoverContent,
				children: /* @__PURE__ */ g(G, {
					mode: "range",
					min: j,
					max: M,
					step: m,
					marks: N,
					marksPosition: y,
					marksDisplay: "full",
					value: ne,
					onChange: fe,
					disabled: k
				})
			})
		})]
	});
}
function ue(e, t) {
	return e[0] === t[0] && e[1] === t[1];
}
//#endregion
export { K as Slider, ce as SliderInput, q as SliderRange, le as SliderRangeInput };

//# sourceMappingURL=index.js.map