"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ensureValueInRange = ensureValueInRange;
exports.ensureValuePrecision = ensureValuePrecision;
exports.getClosestPoint = getClosestPoint;
exports.getHandleCenterPosition = getHandleCenterPosition;
exports.getKeyboardValueMutator = getKeyboardValueMutator;
exports.getMousePosition = getMousePosition;
exports.getPrecision = getPrecision;
exports.getTouchPosition = getTouchPosition;
exports.isEventFromHandle = isEventFromHandle;
exports.isNotTouchEvent = isNotTouchEvent;
exports.isValueOutOfRange = isValueOutOfRange;
exports.pauseEvent = pauseEvent;

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _reactDom = require("react-dom");

var _KeyCode = _interopRequireDefault(require("../../_util/KeyCode"));

function isEventFromHandle(e, handles) {
  return Object.keys(handles).some(function (key) {
    return e.target === (0, _reactDom.findDOMNode)(handles[key]);
  });
}

function isValueOutOfRange(value, _ref) {
  var min = _ref.min,
      max = _ref.max;
  return value < min || value > max;
}

function isNotTouchEvent(e) {
  return e.touches.length > 1 || e.type.toLowerCase() === 'touchend' && e.touches.length > 0;
}

function getClosestPoint(val, _ref2) {
  var marks = _ref2.marks,
      step = _ref2.step,
      min = _ref2.min;
  var points = Object.keys(marks).map(parseFloat);

  if (step !== null) {
    var closestStep = Math.round((val - min) / step) * step + min;
    points.push(closestStep);
  }

  var diffs = points.map(function (point) {
    return Math.abs(val - point);
  });
  return points[diffs.indexOf(Math.min.apply(Math, (0, _toConsumableArray2["default"])(diffs)))];
}

function getPrecision(step) {
  var stepString = step.toString();
  var precision = 0;

  if (stepString.indexOf('.') >= 0) {
    precision = stepString.length - stepString.indexOf('.') - 1;
  }

  return precision;
}

function getMousePosition(vertical, e) {
  return vertical ? e.clientY : e.pageX;
}

function getTouchPosition(vertical, e) {
  return vertical ? e.touches[0].clientY : e.touches[0].pageX;
}

function getHandleCenterPosition(vertical, handle) {
  var coords = handle.getBoundingClientRect();
  return vertical ? coords.top + coords.height * 0.5 : coords.left + coords.width * 0.5;
}

function ensureValueInRange(val, _ref3) {
  var max = _ref3.max,
      min = _ref3.min;

  if (val <= min) {
    return min;
  }

  if (val >= max) {
    return max;
  }

  return val;
}

function ensureValuePrecision(val, props) {
  var step = props.step;
  var closestPoint = getClosestPoint(val, props);
  return step === null ? closestPoint : parseFloat(closestPoint.toFixed(getPrecision(step)));
}

function pauseEvent(e) {
  e.stopPropagation();
  e.preventDefault();
}

function getKeyboardValueMutator(e) {
  switch (e.keyCode) {
    case _KeyCode["default"].UP:
    case _KeyCode["default"].RIGHT:
      return function (value, props) {
        return value + props.step;
      };

    case _KeyCode["default"].DOWN:
    case _KeyCode["default"].LEFT:
      return function (value, props) {
        return value - props.step;
      };

    case _KeyCode["default"].END:
      return function (value, props) {
        return props.max;
      };

    case _KeyCode["default"].HOME:
      return function (value, props) {
        return props.min;
      };

    case _KeyCode["default"].PAGE_UP:
      return function (value, props) {
        return value + props.step * 2;
      };

    case _KeyCode["default"].PAGE_DOWN:
      return function (value, props) {
        return value - props.step * 2;
      };

    default:
      return undefined;
  }
}
//# sourceMappingURL=utils.js.map
