"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const rem = require("../../../core/utils/units-converters/rem.cjs");
const getSize = require("../../../core/utils/get-size/get-size.cjs");
const findClosestNumber = require("../../../core/utils/find-closest-number/find-closest-number.cjs");
const createVarsResolver = require("../../../core/styles-api/create-vars-resolver/create-vars-resolver.cjs");
require("clsx");
const getThemeColor = require("../../../core/MantineProvider/color-functions/get-theme-color/get-theme-color.cjs");
const useProps = require("../../../core/MantineProvider/use-props/use-props.cjs");
const useStyles = require("../../../core/styles-api/use-styles/use-styles.cjs");
const factory = require("../../../core/factory/factory.cjs");
const DirectionProvider = require("../../../core/DirectionProvider/DirectionProvider.cjs");
const Slider_context = require("../Slider.context.cjs");
const SliderRoot = require("../SliderRoot/SliderRoot.cjs");
const Thumb = require("../Thumb/Thumb.cjs");
const Track = require("../Track/Track.cjs");
const getChangeValue = require("../utils/get-change-value/get-change-value.cjs");
const getGloatingValue = require("../utils/get-floating-value/get-gloating-value.cjs");
const getPosition = require("../utils/get-position/get-position.cjs");
const getPrecision = require("../utils/get-precision/get-precision.cjs");
const getStepMarkValue = require("../utils/get-step-mark-value/get-step-mark-value.cjs");
const Slider_module_css = require("../Slider.module.css.cjs");
const useUncontrolled = require("../../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-uncontrolled/use-uncontrolled.cjs");
const clamp = require("../../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/utils/clamp/clamp.cjs");
const useMove = require("../../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-move/use-move.cjs");
const useMergedRef = require("../../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-merged-ref/use-merged-ref.cjs");
const defaultProps = {
  radius: "xl",
  min: 0,
  max: 100,
  step: 1,
  marks: [],
  label: (f) => f,
  labelTransitionProps: { transition: "fade", duration: 0 },
  labelAlwaysOn: false,
  thumbLabel: "",
  showLabelOnHover: true,
  disabled: false,
  scale: (v) => v
};
const varsResolver = createVarsResolver.createVarsResolver(
  (theme, { size, color, thumbSize, radius }) => ({
    root: {
      "--slider-size": getSize.getSize(size, "slider-size"),
      "--slider-color": color ? getThemeColor.getThemeColor(color, theme) : void 0,
      "--slider-radius": radius === void 0 ? void 0 : getSize.getRadius(radius),
      "--slider-thumb-size": thumbSize !== void 0 ? rem.rem(thumbSize) : "calc(var(--slider-size) * 2)"
    }
  })
);
const Slider = factory.factory((_props, ref) => {
  const props = useProps.useProps("Slider", defaultProps, _props);
  const {
    classNames,
    styles,
    value,
    onChange,
    onChangeEnd,
    size,
    min,
    max,
    step,
    precision: _precision,
    defaultValue,
    name,
    marks,
    label,
    labelTransitionProps,
    labelAlwaysOn,
    thumbLabel,
    showLabelOnHover,
    thumbChildren,
    disabled,
    unstyled,
    scale,
    inverted,
    className,
    style,
    vars,
    hiddenInputProps,
    restrictToMarks,
    thumbProps,
    ...others
  } = props;
  const getStyles = useStyles.useStyles({
    name: "Slider",
    props,
    classes: Slider_module_css.default,
    classNames,
    className,
    styles,
    style,
    vars,
    varsResolver,
    unstyled
  });
  const { dir } = DirectionProvider.useDirection();
  const [hovered, setHovered] = React.useState(false);
  const [_value, setValue] = useUncontrolled.useUncontrolled({
    value: typeof value === "number" ? clamp.clamp(value, min, max) : value,
    defaultValue: typeof defaultValue === "number" ? clamp.clamp(defaultValue, min, max) : defaultValue,
    finalValue: clamp.clamp(0, min, max),
    onChange
  });
  const valueRef = React.useRef(_value);
  const root = React.useRef(null);
  const thumb = React.useRef(null);
  const position = getPosition.getPosition({ value: _value, min, max });
  const scaledValue = scale(_value);
  const _label = typeof label === "function" ? label(scaledValue) : label;
  const precision = _precision ?? getPrecision.getPrecision(step);
  const handleChange = React.useCallback(
    ({ x }) => {
      if (!disabled) {
        const nextValue = getChangeValue.getChangeValue({
          value: x,
          min,
          max,
          step,
          precision
        });
        setValue(
          restrictToMarks && (marks == null ? void 0 : marks.length) ? findClosestNumber.findClosestNumber(
            nextValue,
            marks.map((mark) => mark.value)
          ) : nextValue
        );
        valueRef.current = nextValue;
      }
    },
    [disabled, min, max, step, precision, setValue, marks, restrictToMarks]
  );
  const { ref: container, active } = useMove.useMove(
    handleChange,
    {
      onScrubEnd: () => onChangeEnd == null ? void 0 : onChangeEnd(
        restrictToMarks && (marks == null ? void 0 : marks.length) ? findClosestNumber.findClosestNumber(
          valueRef.current,
          marks.map((mark) => mark.value)
        ) : valueRef.current
      )
    },
    dir
  );
  const handleTrackKeydownCapture = (event) => {
    var _a, _b, _c, _d, _e, _f;
    if (!disabled) {
      switch (event.key) {
        case "ArrowUp": {
          event.preventDefault();
          (_a = thumb.current) == null ? void 0 : _a.focus();
          if (restrictToMarks && marks) {
            const nextValue2 = getStepMarkValue.getNextMarkValue(_value, marks);
            setValue(nextValue2);
            onChangeEnd == null ? void 0 : onChangeEnd(nextValue2);
            break;
          }
          const nextValue = getGloatingValue.getFloatingValue(
            Math.min(Math.max(_value + step, min), max),
            precision
          );
          setValue(nextValue);
          onChangeEnd == null ? void 0 : onChangeEnd(nextValue);
          break;
        }
        case "ArrowRight": {
          event.preventDefault();
          (_b = thumb.current) == null ? void 0 : _b.focus();
          if (restrictToMarks && marks) {
            const nextValue2 = dir === "rtl" ? getStepMarkValue.getPreviousMarkValue(_value, marks) : getStepMarkValue.getNextMarkValue(_value, marks);
            setValue(nextValue2);
            onChangeEnd == null ? void 0 : onChangeEnd(nextValue2);
            break;
          }
          const nextValue = getGloatingValue.getFloatingValue(
            Math.min(Math.max(dir === "rtl" ? _value - step : _value + step, min), max),
            precision
          );
          setValue(nextValue);
          onChangeEnd == null ? void 0 : onChangeEnd(nextValue);
          break;
        }
        case "ArrowDown": {
          event.preventDefault();
          (_c = thumb.current) == null ? void 0 : _c.focus();
          if (restrictToMarks && marks) {
            const nextValue2 = getStepMarkValue.getPreviousMarkValue(_value, marks);
            setValue(nextValue2);
            onChangeEnd == null ? void 0 : onChangeEnd(nextValue2);
            break;
          }
          const nextValue = getGloatingValue.getFloatingValue(
            Math.min(Math.max(_value - step, min), max),
            precision
          );
          setValue(nextValue);
          onChangeEnd == null ? void 0 : onChangeEnd(nextValue);
          break;
        }
        case "ArrowLeft": {
          event.preventDefault();
          (_d = thumb.current) == null ? void 0 : _d.focus();
          if (restrictToMarks && marks) {
            const nextValue2 = dir === "rtl" ? getStepMarkValue.getNextMarkValue(_value, marks) : getStepMarkValue.getPreviousMarkValue(_value, marks);
            setValue(nextValue2);
            onChangeEnd == null ? void 0 : onChangeEnd(nextValue2);
            break;
          }
          const nextValue = getGloatingValue.getFloatingValue(
            Math.min(Math.max(dir === "rtl" ? _value + step : _value - step, min), max),
            precision
          );
          setValue(nextValue);
          onChangeEnd == null ? void 0 : onChangeEnd(nextValue);
          break;
        }
        case "Home": {
          event.preventDefault();
          (_e = thumb.current) == null ? void 0 : _e.focus();
          if (restrictToMarks && marks) {
            setValue(getStepMarkValue.getFirstMarkValue(marks));
            onChangeEnd == null ? void 0 : onChangeEnd(getStepMarkValue.getFirstMarkValue(marks));
            break;
          }
          setValue(min);
          onChangeEnd == null ? void 0 : onChangeEnd(min);
          break;
        }
        case "End": {
          event.preventDefault();
          (_f = thumb.current) == null ? void 0 : _f.focus();
          if (restrictToMarks && marks) {
            setValue(getStepMarkValue.getLastMarkValue(marks));
            onChangeEnd == null ? void 0 : onChangeEnd(getStepMarkValue.getLastMarkValue(marks));
            break;
          }
          setValue(max);
          onChangeEnd == null ? void 0 : onChangeEnd(max);
          break;
        }
      }
    }
  };
  return /* @__PURE__ */ jsxRuntime.jsx(Slider_context.SliderProvider, { value: { getStyles }, children: /* @__PURE__ */ jsxRuntime.jsxs(
    SliderRoot.SliderRoot,
    {
      ...others,
      ref: useMergedRef.useMergedRef(ref, root),
      onKeyDownCapture: handleTrackKeydownCapture,
      onMouseDownCapture: () => {
        var _a;
        return (_a = root.current) == null ? void 0 : _a.focus();
      },
      size,
      disabled,
      children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          Track.Track,
          {
            inverted,
            offset: 0,
            filled: position,
            marks,
            min,
            max,
            value: scaledValue,
            disabled,
            containerProps: {
              ref: container,
              onMouseEnter: showLabelOnHover ? () => setHovered(true) : void 0,
              onMouseLeave: showLabelOnHover ? () => setHovered(false) : void 0
            },
            children: /* @__PURE__ */ jsxRuntime.jsx(
              Thumb.Thumb,
              {
                max,
                min,
                value: scaledValue,
                position,
                dragging: active,
                label: _label,
                ref: thumb,
                labelTransitionProps,
                labelAlwaysOn,
                thumbLabel,
                showLabelOnHover,
                isHovered: hovered,
                disabled,
                ...thumbProps,
                children: thumbChildren
              }
            )
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsx("input", { type: "hidden", name, value: scaledValue, ...hiddenInputProps })
      ]
    }
  ) });
});
Slider.classes = Slider_module_css.default;
Slider.displayName = "@mantine/core/Slider";
exports.Slider = Slider;
