"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _length2 = _interopRequireDefault(require("ramda/src/length"));
var _always2 = _interopRequireDefault(require("ramda/src/always"));
var _isNil2 = _interopRequireDefault(require("ramda/src/isNil"));
var _ifElse2 = _interopRequireDefault(require("ramda/src/ifElse"));
var _nth2 = _interopRequireDefault(require("ramda/src/nth"));
var _split2 = _interopRequireDefault(require("ramda/src/split"));
var _toString2 = _interopRequireDefault(require("ramda/src/toString"));
var _pipe2 = _interopRequireDefault(require("ramda/src/pipe"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var getNumberOfDecimals = (0, _pipe2.default)(_toString2.default, (0, _split2.default)('.'), (0, _nth2.default)(1), (0, _ifElse2.default)(_isNil2.default, (0, _always2.default)(0), _length2.default));
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);
};
var _default = exports.default = adjustValue;