import _length from "ramda/src/length";
import _always from "ramda/src/always";
import _isNil from "ramda/src/isNil";
import _ifElse from "ramda/src/ifElse";
import _nth from "ramda/src/nth";
import _split from "ramda/src/split";
import _toString from "ramda/src/toString";
import _pipe from "ramda/src/pipe";
var getNumberOfDecimals = _pipe(_toString, _split('.'), _nth(1), _ifElse(_isNil, _always(0), _length));
var adjustValue = function adjustValue(_ref) {
  var originalValue = _ref.originalValue,
    step = _ref.step,
    shouldIncrement = _ref.shouldIncrement,
    _ref$min = _ref.min,
    min = _ref$min === void 0 ? -Infinity : _ref$min,
    _ref$max = _ref.max,
    max = _ref$max === void 0 ? Infinity : _ref$max;
  var originalValueOrZero = originalValue === null ? 0 : originalValue;
  var numberOfDecimals = getNumberOfDecimals(step);
  var multiplier = Math.pow(10, numberOfDecimals);
  var stepCents = multiplier * step;
  var getAdjustedValueCents = function getAdjustedValueCents() {
    var valueCents = Math.round(originalValueOrZero * multiplier);
    var reminder = valueCents % stepCents;
    if (reminder === 0) {
      return valueCents;
    }
    if (shouldIncrement) {
      return valueCents - reminder;
    }
    return valueCents + stepCents - reminder;
  };
  var adjustedValueCents = getAdjustedValueCents();
  var getAdjustedValue = function getAdjustedValue(value) {
    var adjustedPrice = shouldIncrement ? (value + stepCents) / multiplier : (value - stepCents) / multiplier;
    if (adjustedPrice < min) {
      adjustedPrice = min;
    }
    if (adjustedPrice > max) {
      adjustedPrice = max;
    }
    return adjustedPrice;
  };
  return getAdjustedValue(adjustedValueCents);
};
export default adjustValue;