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

//# sourceMappingURL=index.js.map