var clamp = function clamp(value, min, max) {
  return Math.max(min, Math.min(value, max));
};
var useRangeHandlers = function useRangeHandlers(_ref) {
  var min = _ref.min,
    max = _ref.max,
    value = _ref.value,
    step = _ref.step,
    multipleStepSize = _ref.multipleStepSize,
    isControlled = _ref.isControlled,
    onSetValue = _ref.onSetValue,
    onEmit = _ref.onEmit,
    onCommit = _ref.onCommit;
  var handleChange = function handleChange(e) {
    var newValue = Number(e.target.value);
    if (Number.isNaN(newValue)) {
      return;
    }
    if (!isControlled) {
      onSetValue(newValue);
    }
    onEmit(newValue);
  };
  var handleChangeCommitted = function handleChangeCommitted(e) {
    var newValue = Number(e.target.value);
    if (Number.isNaN(newValue)) {
      return;
    }
    if (onCommit) {
      onCommit(newValue);
    }
  };
  var handleKeyDown = function handleKeyDown(e) {
    if (e.key !== 'PageUp' && e.key !== 'PageDown') {
      return;
    }
    e.preventDefault();
    var pageStep = Math.round((max - min) * multipleStepSize / 100);
    var raw = clamp(e.key === 'PageUp' ? value + pageStep : value - pageStep, min, max);
    var newValue = step ? clamp(min + Math.round((raw - min) / step) * step, min, max) : raw;
    if (Number.isNaN(newValue)) {
      return;
    }
    if (!isControlled) {
      onSetValue(newValue);
    }
    onEmit(newValue);
    if (onCommit) {
      onCommit(newValue);
    }
  };
  return {
    handleChange: handleChange,
    handleChangeCommitted: handleChangeCommitted,
    handleKeyDown: handleKeyDown
  };
};

export { useRangeHandlers };
