import { slicedToArray as _slicedToArray, extends as _extends } from '../../../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { useState, useCallback } from 'react';
import { DoubleSlider } from '../Double/Double.js';

var DoubleUncontrolled = function DoubleUncontrolled(props) {
  var defaultValue = props.defaultValue,
    min = props.min,
    max = props.max,
    value = props.value,
    name = props.name,
    onChange = props.onChange;
  var _useState = useState(defaultValue !== null && defaultValue !== void 0 ? defaultValue : [min, max]),
    _useState2 = _slicedToArray(_useState, 2),
    sliderValue = _useState2[0],
    setSliderValue = _useState2[1];
  var sortValues = function sortValues(values) {
    return values.map(function (val) {
      if (val < props.min) {
        return props.min;
      }
      if (val > props.max) {
        return props.max;
      }
      return val;
    }).sort(function (a, b) {
      return a - b;
    });
  };
  var onChangeHandle = useCallback(function (values) {
    var sortValue = sortValues(values);
    if (onChange && !value && name) {
      onChange({
        target: {
          value: sortValue.join(' - '),
          name: name
        }
      });
    }
    setSliderValue(sortValue);
  }, [name, value, onChange, sortValues]);
  var onChangeCommittedHandle = useCallback(function (values) {
    var sortValue = sortValues(values);
    if (onChange && !value && name) {
      onChange({
        target: {
          value: sortValue.join(' - '),
          name: name
        }
      });
    }
    setSliderValue(sortValue);
  }, [name, value, onChange, sortValues]);
  var onBlurTextField = useCallback(function (values) {
    var sortValue = sortValues(values);
    if (onChange && !value && name) {
      onChange({
        target: {
          value: sortValue.join(' - '),
          name: name
        }
      });
    }
    setSliderValue(sortValue);
  }, [name, value, onChange, sortValues]);
  var onKeyDownTextField = useCallback(function (values, e) {
    if (e.key === 'Enter') {
      var sortValue = sortValues(values);
      if (onChange && !value && name) {
        onChange({
          target: {
            value: sortValue.join(' - '),
            name: name
          }
        });
      }
      setSliderValue(sortValue);
    }
  }, [name, value, onChange, sortValues]);
  return value ? /*#__PURE__*/React__default.createElement(DoubleSlider, _extends({}, props, {
    value: value,
    onChange: onChange
  })) : /*#__PURE__*/React__default.createElement(DoubleSlider, _extends({
    value: sliderValue,
    onKeyDownTextField: onKeyDownTextField,
    onBlurTextField: onBlurTextField,
    onChangeCommitted: onChangeCommittedHandle
  }, props, {
    onChange: onChangeHandle
  }));
};

export { DoubleUncontrolled };
//# sourceMappingURL=DoubleUncontrolled.js.map
