import React, { useMemo, useEffect } from 'react';
import 'rheostat/initialize';
import Rheostat from 'rheostat';
import classnames from 'classnames';
import utils from './utils';
import { DefaultHandle } from './default-handle';
import { AriaContext } from './context';
import { jsx as _jsx } from "react/jsx-runtime";
export const RANGE_SLIDER_SIZES = {
  MD: 'md',
  LG: 'lg',
  XL: 'xl'
};
const hasMultipleValues = values => values.length > 1;
const isInRange = (values, children) => hasMultipleValues(values) ? children >= values[0] && children <= values[values.length - 1] : children <= values[0];
const DefaultPitComponent = ({
  style,
  children,
  values
}) => /*#__PURE__*/_jsx("div", {
  className: `pit-component${isInRange(values, children) ? ' in-range' : ''}`
  /* eslint-disable-next-line react/forbid-dom-props */,
  style: style
});

/** Customization logic for DR deployments */
function CustomizedDefaultProgressBar({
  defaultProps,
  customOptions
}) {
  return /*#__PURE__*/_jsx("div", {
    ...customOptions,
    children: /*#__PURE__*/_jsx(Rheostat.defaultProps.progressBar, {
      ...defaultProps
    })
  });
}

/**
 * RangeSlider - A range slider component, built on https://github.com/airbnb/rheostat
 * @midnight-gray-supported
 * @alpine-light-supported
 */
export default function RangeSlider({
  values,
  onValuesUpdated,
  min,
  max,
  pitPoints,
  snapPoints,
  snap,
  pitComponent,
  progressBar,
  handle = DefaultHandle,
  onChange,
  onSliderDragStart,
  onSliderDragEnd,
  isDisabled,
  algorithm,
  customizedDefaultProgressBar,
  size = RANGE_SLIDER_SIZES.MD,
  testId,
  handleAriaLabel,
  handleAriaValueText,
  ariaLabelledBy,
  ...rest
}) {
  useEffect(() => {
    if (snap && !snapPoints && process.env.NODE_ENV === 'development') {
      // eslint-disable-next-line no-console
      console.error(`[RangeSlider] snap is enabled without snapPoints, keyboard navigation is broken`);
    }
  }, []);
  const ariaContext = useMemo(() => ({
    handleAriaLabel,
    handleAriaValueText,
    ariaLabelledBy,
    testId
  }), [handleAriaLabel, handleAriaValueText, testId]);
  return /*#__PURE__*/_jsx("div", {
    "test-id": "range-slider",
    className: classnames('range-slider', {
      disabled: isDisabled
    }, size),
    children: /*#__PURE__*/_jsx(AriaContext.Provider, {
      value: ariaContext,
      children: /*#__PURE__*/_jsx(Rheostat, {
        handle: handle,
        pitComponent: utils.withProps(pitComponent || DefaultPitComponent, {
          values
        }),
        progressBar: customizedDefaultProgressBar ? defaultProps => CustomizedDefaultProgressBar({
          defaultProps,
          customOptions: customizedDefaultProgressBar
        }) : progressBar,
        values: values,
        algorithm: algorithm,
        onValuesUpdated: onValuesUpdated,
        min: min,
        max: max,
        pitPoints: pitPoints,
        snap: snap,
        snapPoints: snapPoints,
        onChange: onChange,
        onSliderDragStart: onSliderDragStart,
        onSliderDragEnd: onSliderDragEnd,
        disabled: isDisabled,
        ...rest
      })
    })
  });
}