"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 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 useProps = require("../../core/MantineProvider/use-props/use-props.cjs");
const useStyles = require("../../core/styles-api/use-styles/use-styles.cjs");
const Box = require("../../core/Box/Box.cjs");
const factory = require("../../core/factory/factory.cjs");
const AngleSlider_module_css = require("./AngleSlider.module.css.cjs");
const useUncontrolled = require("../../../../../../../@mantine_hooks@7.17.8_react@18.3.1/node_modules/@mantine/hooks/esm/use-uncontrolled/use-uncontrolled.cjs");
const useRadialMove = require("../../../../../../../@mantine_hooks@7.17.8_react@18.3.1/node_modules/@mantine/hooks/esm/use-radial-move/use-radial-move.cjs");
const useMergedRef = require("../../../../../../../@mantine_hooks@7.17.8_react@18.3.1/node_modules/@mantine/hooks/esm/use-merged-ref/use-merged-ref.cjs");
const defaultProps = {
  step: 1,
  withLabel: true
};
const varsResolver = createVarsResolver.createVarsResolver((_, { size, thumbSize }) => ({
  root: {
    "--slider-size": rem.rem(size),
    "--thumb-size": rem.rem(thumbSize)
  }
}));
const AngleSlider = factory.factory((_props, ref) => {
  const props = useProps.useProps("AngleSlider", defaultProps, _props);
  const {
    classNames,
    className,
    style,
    styles,
    unstyled,
    vars,
    step,
    value,
    defaultValue,
    onChange,
    onMouseDown,
    withLabel,
    marks,
    thumbSize,
    restrictToMarks,
    formatLabel,
    onChangeEnd,
    disabled,
    onTouchStart,
    name,
    hiddenInputProps,
    "aria-label": ariaLabel,
    tabIndex,
    onScrubStart,
    onScrubEnd,
    ...others
  } = props;
  const [_value, setValue] = useUncontrolled.useUncontrolled({
    value,
    defaultValue,
    finalValue: 0,
    onChange
  });
  const update = (val) => {
    if (rootRef.current) {
      const newValue = restrictToMarks && Array.isArray(marks) ? findClosestNumber.findClosestNumber(
        val,
        marks.map((mark) => mark.value)
      ) : val;
      setValue(newValue);
    }
  };
  const { ref: rootRef } = useRadialMove.useRadialMove(update, {
    step,
    onChangeEnd,
    onScrubStart,
    onScrubEnd
  });
  const getStyles = useStyles.useStyles({
    name: "AngleSlider",
    classes: AngleSlider_module_css.default,
    props,
    className,
    style,
    classNames,
    styles,
    unstyled,
    vars,
    varsResolver
  });
  const handleKeyDown = (event) => {
    if (disabled) {
      return;
    }
    let newValue = _value;
    if (event.key === "ArrowLeft" || event.key === "ArrowDown") {
      newValue = useRadialMove.normalizeRadialValue(_value - step, step);
    }
    if (event.key === "ArrowRight" || event.key === "ArrowUp") {
      newValue = useRadialMove.normalizeRadialValue(_value + step, step);
    }
    if (event.key === "Home") {
      newValue = 0;
    }
    if (event.key === "End") {
      newValue = 359;
    }
    if (restrictToMarks && Array.isArray(marks)) {
      const markValues = marks.map((mark) => mark.value);
      const currentIndex = markValues.indexOf(_value);
      if (currentIndex !== -1) {
        if (event.key === "ArrowLeft" || event.key === "ArrowDown") {
          newValue = markValues[Math.max(0, currentIndex - 1)];
        } else if (event.key === "ArrowRight" || event.key === "ArrowUp") {
          newValue = markValues[Math.min(markValues.length - 1, currentIndex + 1)];
        } else {
          newValue = findClosestNumber.findClosestNumber(newValue, markValues);
        }
      } else {
        newValue = findClosestNumber.findClosestNumber(newValue, markValues);
      }
    }
    setValue(newValue);
    onChangeEnd == null ? void 0 : onChangeEnd(newValue);
  };
  const marksItems = marks == null ? void 0 : marks.map((mark, index) => /* @__PURE__ */ React.createElement(
    "div",
    {
      ...getStyles("mark", { style: { "--angle": `${mark.value}deg` } }),
      "data-label": mark.label || void 0,
      key: index
    }
  ));
  return /* @__PURE__ */ jsxRuntime.jsxs(Box.Box, { ref: useMergedRef.useMergedRef(ref, rootRef), ...getStyles("root", { focusable: true }), ...others, children: [
    marksItems && marksItems.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { ...getStyles("marks"), children: marksItems }),
    withLabel && /* @__PURE__ */ jsxRuntime.jsx("div", { ...getStyles("label"), children: typeof formatLabel === "function" ? formatLabel(_value) : _value }),
    /* @__PURE__ */ jsxRuntime.jsx(
      "div",
      {
        tabIndex: tabIndex ?? (disabled ? -1 : 0),
        role: "slider",
        "aria-valuemax": 360,
        "aria-valuemin": 0,
        "aria-valuenow": _value,
        onKeyDown: handleKeyDown,
        "aria-label": ariaLabel,
        ...getStyles("thumb", { style: { transform: `rotate(${_value}deg)` } })
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx("input", { type: "hidden", name, value: _value, ...hiddenInputProps })
  ] });
});
AngleSlider.displayName = "@mantine/core/AngleSlider";
AngleSlider.classes = AngleSlider_module_css.default;
exports.AngleSlider = AngleSlider;
