'use strict';

exports.__esModule = true;

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

/** Child component to display the slider pointer */
var SliderPointer = function (_React$PureComponent) {
  (0, _inherits3.default)(SliderPointer, _React$PureComponent);

  function SliderPointer() {
    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, SliderPointer);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args))), _this), _this.state = {
      previousPosition: null
    }, _this.getDirections = function (currentPos) {
      var previousPosition = _this.state.previousPosition;

      if (currentPos > previousPosition) {
        return 1;
      }
      return -1;
    }, _this.getPosition = function (event, isTouch) {
      return isTouch ? event.touches[0].clientX : event.clientX;
    }, _this.onMouseDown = function (event) {
      var isTouch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

      if (!isTouch) {
        document.addEventListener('mousemove', _this.onMouseMove);
        document.addEventListener('mouseup', _this.onMouseUp);
      }

      _this.setState({
        previousPosition: _this.getPosition(event, isTouch)
      });
    }, _this.onMouseUp = function () {
      document.removeEventListener('mousemove', _this.onMouseMove);
      document.removeEventListener('mouseup', _this.onMouseUp);
    }, _this.onMouseMove = function (event) {
      var isTouch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

      var xPos = _this.getPosition(event, isTouch);
      var direction = _this.getDirections(xPos);

      _this.props.onMove({
        from: _this.sliderRef.getBoundingClientRect().x,
        to: xPos,
        direction: direction,
        isKeyBoard: false
      });
    }, _this.onKeyDown = function (event) {
      var KEYS = {
        LEFT: 37,
        UP: 38,
        RIGHT: 39,
        DOWN: 40
      };

      switch (event.keyCode) {
        case KEYS.UP:
        case KEYS.RIGHT:
          _this.props.onMove({
            isKeyBoard: true,
            direction: 1
          });
          event.preventDefault();
          break;

        case KEYS.DOWN:
        case KEYS.LEFT:
          _this.props.onMove({
            isKeyBoard: true,
            direction: -1
          });
          event.preventDefault();
          break;
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  SliderPointer.prototype.render = function render() {
    var _this2 = this;

    var position = this.props.position;


    var pointerStyle = { left: 'calc(' + position + '%' };

    return _react2.default.createElement('div', {
      className: 'cui-slider__pointer',
      onMouseDown: function onMouseDown(e) {
        return _this2.onMouseDown(e);
      },
      onTouchStart: function onTouchStart(e) {
        return _this2.onMouseDown(e, true);
      },
      onTouchMove: function onTouchMove(e) {
        return _this2.onMouseMove(e, true);
      },
      onKeyDown: function onKeyDown(e) {
        return _this2.onKeyDown(e);
      },
      role: 'button',
      tabIndex: 0,
      style: pointerStyle,
      ref: function ref(_ref) {
        return _this2.sliderRef = _ref;
      }
    });
  };

  return SliderPointer;
}(_react2.default.PureComponent);

exports.default = SliderPointer;


SliderPointer.defaultProps = {
  position: 0,
  onMove: null
};
module.exports = exports['default'];