"use client";
const require_rem = require("../../../core/utils/units-converters/rem.cjs");
const require_get_size = require("../../../core/utils/get-size/get-size.cjs");
const require_find_closest_number = require("../../../core/utils/find-closest-number/find-closest-number.cjs");
const require_create_vars_resolver = require("../../../core/styles-api/create-vars-resolver/create-vars-resolver.cjs");
const require_get_theme_color = require("../../../core/MantineProvider/color-functions/get-theme-color/get-theme-color.cjs");
const require_use_props = require("../../../core/MantineProvider/use-props/use-props.cjs");
const require_use_styles = require("../../../core/styles-api/use-styles/use-styles.cjs");
const require_factory = require("../../../core/factory/factory.cjs");
const require_DirectionProvider = require("../../../core/DirectionProvider/DirectionProvider.cjs");
const require_Slider_context = require("../Slider.context.cjs");
const require_SliderRoot = require("../SliderRoot/SliderRoot.cjs");
const require_Thumb = require("../Thumb/Thumb.cjs");
const require_get_position = require("../utils/get-position/get-position.cjs");
const require_Track = require("../Track/Track.cjs");
const require_get_change_value = require("../utils/get-change-value/get-change-value.cjs");
const require_get_floating_value = require("../utils/get-floating-value/get-floating-value.cjs");
const require_get_precision = require("../utils/get-precision/get-precision.cjs");
const require_get_step_mark_value = require("../utils/get-step-mark-value/get-step-mark-value.cjs");
const require_Slider_module = require("../Slider.module.cjs");
let react = require("react");
let _mantine_hooks = require("@mantine/hooks");
let react_jsx_runtime = require("react/jsx-runtime");
//#region packages/@mantine/core/src/components/Slider/Slider/Slider.tsx
const defaultProps = {
	radius: "xl",
	min: 0,
	max: 100,
	step: 1,
	marks: [],
	label: (f) => f,
	labelTransitionProps: {
		transition: "fade",
		duration: 0
	},
	thumbLabel: "",
	showLabelOnHover: true,
	scale: (v) => v,
	size: "md"
};
const varsResolver = require_create_vars_resolver.createVarsResolver((theme, { size, color, thumbSize, radius }) => ({ root: {
	"--slider-size": require_get_size.getSize(size, "slider-size"),
	"--slider-color": color ? require_get_theme_color.getThemeColor(color, theme) : void 0,
	"--slider-radius": radius === void 0 ? void 0 : require_get_size.getRadius(radius),
	"--slider-thumb-size": thumbSize !== void 0 ? require_rem.rem(thumbSize) : "calc(var(--slider-size) * 2)"
} }));
const Slider = require_factory.factory((_props) => {
	const props = require_use_props.useProps("Slider", defaultProps, _props);
	const { classNames, styles, value, onChange, onChangeEnd, size, min, max, domain, step, precision: _precision, defaultValue, name, marks, label, labelTransitionProps, labelAlwaysOn, thumbLabel, thumbValueText, showLabelOnHover, thumbChildren, disabled, unstyled, scale, inverted, startPointValue, orientation, className, style, vars, hiddenInputProps, restrictToMarks, thumbProps, attributes, ref, ...others } = props;
	const getStyles = require_use_styles.useStyles({
		name: "Slider",
		props,
		classes: require_Slider_module.default,
		classNames,
		className,
		styles,
		style,
		attributes,
		vars,
		varsResolver,
		unstyled
	});
	const { dir } = require_DirectionProvider.useDirection();
	const [hovered, setHovered] = (0, react.useState)(false);
	const [_value, setValue] = (0, _mantine_hooks.useUncontrolled)({
		value: typeof value === "number" ? (0, _mantine_hooks.clamp)(value, min, max) : value,
		defaultValue: typeof defaultValue === "number" ? (0, _mantine_hooks.clamp)(defaultValue, min, max) : defaultValue,
		finalValue: (0, _mantine_hooks.clamp)(0, min, max),
		onChange
	});
	const valueRef = (0, react.useRef)(_value);
	const onChangeEndRef = (0, react.useRef)(onChangeEnd);
	(0, react.useEffect)(() => {
		onChangeEndRef.current = onChangeEnd;
	}, [onChangeEnd]);
	const root = (0, react.useRef)(null);
	const thumb = (0, react.useRef)(null);
	const [domainMin, domainMax] = domain || [min, max];
	const position = require_get_position.getPosition({
		value: _value,
		min: domainMin,
		max: domainMax
	});
	const scaledValue = scale(_value);
	const _label = typeof label === "function" ? label(scaledValue) : label;
	const precision = _precision ?? require_get_precision.getPrecision(step);
	const hasStartPoint = typeof startPointValue === "number" && !inverted;
	const startPosition = hasStartPoint ? require_get_position.getPosition({
		value: startPointValue,
		min: domainMin,
		max: domainMax
	}) : 0;
	const trackOffset = hasStartPoint ? Math.min(position, startPosition) : 0;
	const trackFilled = hasStartPoint ? Math.abs(position - startPosition) : position;
	const handleChange = (0, react.useCallback)(({ x }) => {
		if (!disabled) {
			const clampedValue = (0, _mantine_hooks.clamp)(require_get_change_value.getChangeValue({
				value: x,
				min: domainMin,
				max: domainMax,
				step,
				precision
			}), min, max);
			setValue(restrictToMarks && marks?.length ? require_find_closest_number.findClosestNumber(clampedValue, marks.map((mark) => mark.value)) : clampedValue);
			valueRef.current = clampedValue;
		}
	}, [
		disabled,
		min,
		max,
		domainMin,
		domainMax,
		step,
		precision,
		setValue,
		marks,
		restrictToMarks
	]);
	const { ref: container, active } = (0, _mantine_hooks.useMove)(({ x, y }) => handleChange({ x: orientation === "vertical" ? 1 - y : x }), { onScrubEnd: (0, react.useCallback)(() => {
		if (!disabled && onChangeEndRef.current) {
			const finalValue = restrictToMarks && marks?.length ? require_find_closest_number.findClosestNumber(valueRef.current, marks.map((mark) => mark.value)) : valueRef.current;
			onChangeEndRef.current(finalValue);
		}
	}, [
		disabled,
		marks,
		restrictToMarks
	]) }, dir);
	const callOnChangeEnd = (0, react.useCallback)((value) => {
		if (!disabled && onChangeEndRef.current) onChangeEndRef.current(value);
	}, [disabled]);
	const handleTrackKeydownCapture = (event) => {
		if (!disabled) switch (event.key) {
			case "ArrowUp": {
				event.preventDefault();
				thumb.current?.focus();
				if (restrictToMarks && marks) {
					const nextValue = require_get_step_mark_value.getNextMarkValue(_value, marks);
					setValue(nextValue);
					callOnChangeEnd(nextValue);
					break;
				}
				const nextValue = require_get_floating_value.getFloatingValue(Math.min(Math.max(_value + step, domainMin), domainMax), precision);
				setValue(nextValue);
				callOnChangeEnd(nextValue);
				break;
			}
			case "ArrowRight": {
				event.preventDefault();
				thumb.current?.focus();
				if (restrictToMarks && marks) {
					const nextValue = dir === "rtl" ? require_get_step_mark_value.getPreviousMarkValue(_value, marks) : require_get_step_mark_value.getNextMarkValue(_value, marks);
					setValue(nextValue);
					callOnChangeEnd(nextValue);
					break;
				}
				const nextValue = require_get_floating_value.getFloatingValue(Math.min(Math.max(dir === "rtl" ? _value - step : _value + step, domainMin), domainMax), precision);
				setValue(nextValue);
				callOnChangeEnd(nextValue);
				break;
			}
			case "ArrowDown": {
				event.preventDefault();
				thumb.current?.focus();
				if (restrictToMarks && marks) {
					const nextValue = require_get_step_mark_value.getPreviousMarkValue(_value, marks);
					setValue(nextValue);
					callOnChangeEnd(nextValue);
					break;
				}
				const nextValue = require_get_floating_value.getFloatingValue(Math.min(Math.max(_value - step, domainMin), domainMax), precision);
				setValue(nextValue);
				callOnChangeEnd(nextValue);
				break;
			}
			case "ArrowLeft": {
				event.preventDefault();
				thumb.current?.focus();
				if (restrictToMarks && marks) {
					const nextValue = dir === "rtl" ? require_get_step_mark_value.getNextMarkValue(_value, marks) : require_get_step_mark_value.getPreviousMarkValue(_value, marks);
					setValue(nextValue);
					callOnChangeEnd(nextValue);
					break;
				}
				const nextValue = require_get_floating_value.getFloatingValue(Math.min(Math.max(dir === "rtl" ? _value + step : _value - step, domainMin), domainMax), precision);
				setValue(nextValue);
				callOnChangeEnd(nextValue);
				break;
			}
			case "Home":
				event.preventDefault();
				thumb.current?.focus();
				if (restrictToMarks && marks) {
					setValue(require_get_step_mark_value.getFirstMarkValue(marks));
					callOnChangeEnd(require_get_step_mark_value.getFirstMarkValue(marks));
					break;
				}
				setValue(min);
				callOnChangeEnd(min);
				break;
			case "End":
				event.preventDefault();
				thumb.current?.focus();
				if (restrictToMarks && marks) {
					setValue(require_get_step_mark_value.getLastMarkValue(marks));
					callOnChangeEnd(require_get_step_mark_value.getLastMarkValue(marks));
					break;
				}
				setValue(max);
				callOnChangeEnd(max);
				break;
			default: break;
		}
	};
	return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Slider_context.SliderProvider, {
		value: { getStyles },
		children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(require_SliderRoot.SliderRoot, {
			...others,
			ref: (0, _mantine_hooks.useMergedRef)(ref, root),
			onKeyDownCapture: handleTrackKeydownCapture,
			onMouseDownCapture: () => root.current?.focus(),
			size,
			disabled,
			orientation,
			children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Track.Track, {
				inverted,
				offset: trackOffset,
				filled: trackFilled,
				marks,
				min: domainMin,
				max: domainMax,
				value: scaledValue,
				startPointValue: hasStartPoint ? startPointValue : void 0,
				disabled,
				containerProps: {
					ref: container,
					onMouseEnter: showLabelOnHover ? () => setHovered(true) : void 0,
					onMouseLeave: showLabelOnHover ? () => setHovered(false) : void 0
				},
				children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Thumb.Thumb, {
					max: domainMax,
					min: domainMin,
					value: scaledValue,
					position,
					dragging: active,
					label: _label,
					ref: thumb,
					labelTransitionProps,
					labelAlwaysOn,
					thumbLabel,
					thumbValueText,
					showLabelOnHover,
					isHovered: hovered,
					disabled,
					orientation,
					...thumbProps,
					children: thumbChildren
				})
			}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
				type: "hidden",
				name,
				value: scaledValue,
				...hiddenInputProps
			})]
		})
	});
});
Slider.classes = require_Slider_module.default;
Slider.varsResolver = varsResolver;
Slider.displayName = "@mantine/core/Slider";
//#endregion
exports.Slider = Slider;

//# sourceMappingURL=Slider.cjs.map