'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var Double = require('../Double/Double.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

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 = React.useState(defaultValue !== null && defaultValue !== void 0 ? defaultValue : [min, max]),
    _useState2 = _rollupPluginBabelHelpers.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 = React.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 = React.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 = React.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 = React.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.default.createElement(Double.DoubleSlider, _rollupPluginBabelHelpers.extends({}, props, {
    value: value,
    onChange: onChange
  })) : /*#__PURE__*/React__default.default.createElement(Double.DoubleSlider, _rollupPluginBabelHelpers.extends({
    value: sliderValue,
    onKeyDownTextField: onKeyDownTextField,
    onBlurTextField: onBlurTextField,
    onChangeCommitted: onChangeCommittedHandle
  }, props, {
    onChange: onChangeHandle
  }));
};

exports.DoubleUncontrolled = DoubleUncontrolled;
//# sourceMappingURL=DoubleUncontrolled.js.map
