"use client";
import * as React from 'react';
import { cva } from 'class-variance-authority';
import { Slider as Slider$1 } from '@base-ui/react/slider';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import { jsxs, jsx } from 'react/jsx-runtime';

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
function cn(...inputs) {
  return twMerge(clsx(inputs));
}
var thumbVariants = cva(
  [
    "block shrink-0 origin-center rounded-full border-2 border-primary bg-background shadow-sm",
    "cursor-grab touch-manipulation transition-[box-shadow,transform] duration-200 ease-out",
    /* Slight “drawer handle” lift: hover / focus-within ~4%, press ~5%, drag ~8% */
    "hover:scale-[1.04] hover:ring-2 hover:ring-ring/30",
    "active:scale-[1.05] active:cursor-grabbing",
    "focus-within:scale-[1.04] focus-visible:outline-none focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 focus-within:ring-offset-background",
    "data-[dragging]:scale-[1.08] data-[dragging]:cursor-grabbing data-[dragging]:shadow-md",
    "motion-reduce:transition-none motion-reduce:hover:scale-100 motion-reduce:active:scale-100 motion-reduce:focus-within:scale-100 motion-reduce:data-[dragging]:scale-100",
    "disabled:pointer-events-none"
  ].join(" "),
  {
    variants: {
      size: {
        sm: "size-3",
        md: "size-4"
      }
    },
    defaultVariants: { size: "md" }
  }
);
function valueToPercent(value, min, max) {
  if (max === min) return 0;
  return (value - min) / (max - min) * 100;
}
function buildMarkValues(min, max, markStep) {
  if (markStep <= 0 || max < min) return [min, max];
  const out = [];
  for (let v = min; v < max; v += markStep) {
    out.push(Math.round((v + Number.EPSILON) * 1e3) / 1e3);
  }
  if (out[out.length - 1] !== max) out.push(max);
  return [...new Set(out)].sort((a, b) => a - b);
}
function defaultMarkStep(min, max, sliderStep) {
  const span = max - min;
  if (span <= 0) return sliderStep;
  if (span <= 10) return Math.max(1, sliderStep);
  if (span <= 40) return Math.max(2, sliderStep);
  if (span <= 100) return Math.max(5, sliderStep);
  return Math.max(10, sliderStep);
}
function resolveMarks(min, max, sliderStep, marks, markValues, markStep) {
  if (!marks && !(markValues == null ? void 0 : markValues.length)) return null;
  if (markValues == null ? void 0 : markValues.length) {
    return [...new Set(markValues.map((v) => clamp(v, min, max)))].sort(
      (a, b) => a - b
    );
  }
  const step = markStep != null ? markStep : defaultMarkStep(min, max, sliderStep);
  return buildMarkValues(min, max, step);
}
function clamp(n, min, max) {
  return Math.min(max, Math.max(min, n));
}
function SliderMarks({
  min,
  max,
  values,
  formatMark,
  size,
  insetClassName,
  showTicks = true
}) {
  const labelClass = size === "sm" ? "text-[11px] leading-4 text-[var(--theme-text-tertiary)]" : "text-xs leading-4 text-[var(--theme-text-tertiary)]";
  return /* @__PURE__ */ jsxs("div", { className: cn("flex w-full flex-col gap-1", insetClassName), children: [
    showTicks && /* @__PURE__ */ jsx(
      "div",
      {
        className: "relative h-2 w-full shrink-0",
        "aria-hidden": "true",
        "data-slot": "slider-mark-ticks",
        children: values.map((v) => /* @__PURE__ */ jsx(
          "span",
          {
            className: "absolute bottom-0 w-px translate-x-[-50%] bg-[var(--theme-stroke-default)]",
            style: {
              left: `${valueToPercent(v, min, max)}%`,
              height: size === "sm" ? 6 : 8
            }
          },
          `tick-${v}`
        ))
      }
    ),
    /* @__PURE__ */ jsx(
      "div",
      {
        className: "relative h-4 w-full shrink-0",
        "data-slot": "slider-mark-labels",
        "aria-hidden": "true",
        children: values.map((v) => /* @__PURE__ */ jsx(
          "span",
          {
            className: cn(
              "absolute top-0 -translate-x-1/2 tabular-nums",
              labelClass
            ),
            style: { left: `${valueToPercent(v, min, max)}%` },
            children: formatMark(v)
          },
          `label-${v}`
        ))
      }
    )
  ] });
}
function isExplicitMarksObject(marks) {
  return typeof marks === "object" && marks !== null && !Array.isArray(marks);
}
function normalizeMarksInput(marks) {
  if (marks == null) return { enabled: false };
  if (marks === true) return { enabled: true };
  if (Array.isArray(marks)) return { enabled: true, values: marks };
  if (isExplicitMarksObject(marks)) {
    return {
      enabled: true,
      values: marks.values,
      showTicks: marks.showTicks
    };
  }
  return { enabled: false };
}
function Slider(_a) {
  var _b = _a, {
    className,
    disabled,
    marks: marksProp,
    markStep,
    formatMark = (v) => String(v),
    size = "md",
    label,
    helperText,
    showMarkTicks,
    thumbClassName,
    min = 0,
    max = 100,
    step = 1
  } = _b, props = __objRest(_b, [
    "className",
    "disabled",
    "marks",
    "markStep",
    "formatMark",
    "size",
    "label",
    "helperText",
    "showMarkTicks",
    "thumbClassName",
    "min",
    "max",
    "step"
  ]);
  const marksNorm = normalizeMarksInput(marksProp);
  const markList = resolveMarks(
    min,
    max,
    step,
    marksNorm.enabled,
    marksNorm.values,
    markStep
  );
  const ticksVisible = showMarkTicks != null ? showMarkTicks : markList != null && marksNorm.showTicks !== false;
  const insetClass = size === "sm" ? "px-1.5" : "px-2";
  const trackH = size === "sm" ? "h-1" : "h-1.5";
  const controlH = size === "sm" ? "min-h-9 py-1.5" : "min-h-10 py-2";
  const id = React.useId();
  const labelId = label ? `${id}-label` : void 0;
  const helperId = helperText ? `${id}-helper` : void 0;
  return /* @__PURE__ */ jsxs(
    "div",
    {
      "data-slot": "slider-field",
      className: cn("flex w-full flex-col gap-1.5", className),
      children: [
        label && /* @__PURE__ */ jsx(
          "span",
          {
            id: labelId,
            className: "text-sm font-medium leading-5 text-[var(--theme-text-primary)]",
            children: label
          }
        ),
        /* @__PURE__ */ jsx(
          Slider$1.Root,
          __spreadProps(__spreadValues({
            className: cn(
              "relative flex w-full touch-none select-none flex-col gap-0",
              disabled && "pointer-events-none opacity-50"
            ),
            disabled,
            min,
            max,
            step,
            thumbAlignment: "edge",
            "aria-labelledby": labelId,
            "aria-describedby": helperId
          }, props), {
            children: /* @__PURE__ */ jsxs("div", { className: cn("flex w-full flex-col gap-0", insetClass), children: [
              /* @__PURE__ */ jsxs(
                Slider$1.Control,
                {
                  className: cn(
                    "relative flex w-full cursor-grab items-center active:cursor-grabbing",
                    controlH
                  ),
                  "data-slot": "slider-control",
                  children: [
                    /* @__PURE__ */ jsx(
                      Slider$1.Track,
                      {
                        className: cn(
                          "relative w-full grow overflow-hidden rounded-full bg-muted",
                          trackH
                        ),
                        "data-slot": "slider-track",
                        children: /* @__PURE__ */ jsx(
                          Slider$1.Indicator,
                          {
                            className: "absolute h-full rounded-full bg-primary",
                            "data-slot": "slider-indicator"
                          }
                        )
                      }
                    ),
                    /* @__PURE__ */ jsx(
                      Slider$1.Thumb,
                      {
                        index: 0,
                        className: cn(thumbVariants({ size }), thumbClassName),
                        "data-slot": "slider-thumb"
                      }
                    )
                  ]
                }
              ),
              markList && markList.length > 0 && /* @__PURE__ */ jsx(
                SliderMarks,
                {
                  min,
                  max,
                  values: markList,
                  formatMark,
                  size,
                  insetClassName: "",
                  showTicks: ticksVisible
                }
              )
            ] })
          })
        ),
        helperText && /* @__PURE__ */ jsx(
          "p",
          {
            id: helperId,
            className: "text-xs leading-4 text-[var(--theme-text-secondary)]",
            children: helperText
          }
        )
      ]
    }
  );
}
function RangeSlider(_a) {
  var _b = _a, {
    className,
    disabled,
    marks: marksProp,
    markStep,
    formatMark = (v) => String(v),
    size = "md",
    label,
    helperText,
    showMarkTicks,
    thumbClassName,
    min = 0,
    max = 100,
    step = 1
  } = _b, props = __objRest(_b, [
    "className",
    "disabled",
    "marks",
    "markStep",
    "formatMark",
    "size",
    "label",
    "helperText",
    "showMarkTicks",
    "thumbClassName",
    "min",
    "max",
    "step"
  ]);
  const marksNorm = normalizeMarksInput(marksProp);
  const markList = resolveMarks(
    min,
    max,
    step,
    marksNorm.enabled,
    marksNorm.values,
    markStep
  );
  const ticksVisible = showMarkTicks != null ? showMarkTicks : markList != null && marksNorm.showTicks !== false;
  const insetClass = size === "sm" ? "px-1.5" : "px-2";
  const trackH = size === "sm" ? "h-1" : "h-1.5";
  const controlH = size === "sm" ? "min-h-9 py-1.5" : "min-h-10 py-2";
  const id = React.useId();
  const labelId = label ? `${id}-label` : void 0;
  const helperId = helperText ? `${id}-helper` : void 0;
  return /* @__PURE__ */ jsxs(
    "div",
    {
      "data-slot": "slider-range-field",
      className: cn("flex w-full flex-col gap-1.5", className),
      children: [
        label && /* @__PURE__ */ jsx(
          "span",
          {
            id: labelId,
            className: "text-sm font-medium leading-5 text-[var(--theme-text-primary)]",
            children: label
          }
        ),
        /* @__PURE__ */ jsx(
          Slider$1.Root,
          __spreadProps(__spreadValues({
            className: cn(
              "relative flex w-full touch-none select-none flex-col gap-0",
              disabled && "pointer-events-none opacity-50"
            ),
            disabled,
            min,
            max,
            step,
            minStepsBetweenValues: 1,
            thumbAlignment: "edge",
            "aria-labelledby": labelId,
            "aria-describedby": helperId
          }, props), {
            children: /* @__PURE__ */ jsxs("div", { className: cn("flex w-full flex-col gap-0", insetClass), children: [
              /* @__PURE__ */ jsxs(
                Slider$1.Control,
                {
                  className: cn(
                    "relative flex w-full cursor-grab items-center active:cursor-grabbing",
                    controlH
                  ),
                  "data-slot": "slider-range-control",
                  children: [
                    /* @__PURE__ */ jsx(
                      Slider$1.Track,
                      {
                        className: cn(
                          "relative w-full grow overflow-hidden rounded-full bg-muted",
                          trackH
                        ),
                        "data-slot": "slider-range-track",
                        children: /* @__PURE__ */ jsx(
                          Slider$1.Indicator,
                          {
                            className: "absolute h-full rounded-full bg-primary",
                            "data-slot": "slider-range-indicator"
                          }
                        )
                      }
                    ),
                    /* @__PURE__ */ jsx(
                      Slider$1.Thumb,
                      {
                        index: 0,
                        className: cn(thumbVariants({ size }), thumbClassName),
                        "data-slot": "slider-range-thumb-0"
                      }
                    ),
                    /* @__PURE__ */ jsx(
                      Slider$1.Thumb,
                      {
                        index: 1,
                        className: cn(thumbVariants({ size }), thumbClassName),
                        "data-slot": "slider-range-thumb-1"
                      }
                    )
                  ]
                }
              ),
              markList && markList.length > 0 && /* @__PURE__ */ jsx(
                SliderMarks,
                {
                  min,
                  max,
                  values: markList,
                  formatMark,
                  size,
                  insetClassName: "",
                  showTicks: ticksVisible
                }
              )
            ] })
          })
        ),
        helperText && /* @__PURE__ */ jsx(
          "p",
          {
            id: helperId,
            className: "text-xs leading-4 text-[var(--theme-text-secondary)]",
            children: helperText
          }
        )
      ]
    }
  );
}

export { RangeSlider, Slider, buildMarkValues, valueToPercent };
//# sourceMappingURL=slider.js.map
//# sourceMappingURL=slider.js.map