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