'use strict';

exports.__esModule = true;
exports.default = validate;

var _lodash = require('lodash.tonumber');

var _lodash2 = _interopRequireDefault(_lodash);

var _utils = require('./utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// http://stackoverflow.com/a/31711034/1873485
function floatSafeRemainder(val, step) {
  var valDecCount = (val.toString().split('.')[1] || '').length;
  var stepDecCount = (step.toString().split('.')[1] || '').length;
  var decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
  var valInt = parseInt(val.toFixed(decCount).replace('.', ''), 10);
  var stepInt = parseInt(step.toFixed(decCount).replace('.', ''), 10);
  return valInt % stepInt / Math.pow(10, decCount);
}

function validate(value, context) {
  var constraint = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

  if ((0, _utils.isEmpty)(value)) return true;

  return floatSafeRemainder((0, _lodash2.default)(value), (0, _lodash2.default)(constraint.value)) === 0 || constraint.errorMessage || false;
}