function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

/* eslint-disable  */
import React, { Component } from 'react';

var Finger =
/*#__PURE__*/
function (_Component) {
  _inherits(Finger, _Component);

  function Finger(props) {
    var _this;

    _classCallCheck(this, Finger);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Finger).call(this, props));
    _this.preV = {
      x: null,
      y: null
    };
    _this.pinchStartLen = null;
    _this.scale = 1;
    _this.isDoubleTap = false;
    _this.delta = null;
    _this.last = null;
    _this.now = null;
    _this.end = null;
    _this.multiTouch = false;
    _this.tapTimeout = null;
    _this.longTapTimeout = null;
    _this.singleTapTimeout = null;
    _this.swipeTimeout = null;
    _this.x1 = null;
    _this.x2 = null;
    _this.y1 = null;
    _this.y2 = null;
    _this.preTapPosition = {
      x: null,
      y: null
    };
    return _this;
  } // 获取长度


  _createClass(Finger, [{
    key: "getLen",
    value: function getLen(v) {
      return Math.sqrt(v.x * v.x + v.y * v.y);
    } // 获取点 - 来自获取角度

  }, {
    key: "dot",
    value: function dot(v1, v2) {
      return v1.x * v2.x + v1.y * v2.y;
    } // 获取角度

  }, {
    key: "getAngle",
    value: function getAngle(v1, v2) {
      var mr = this.getLen(v1) * this.getLen(v2);
      if (mr === 0) return 0;
      var r = this.dot(v1, v2) / mr;
      if (r > 1) r = 1;
      return Math.acos(r);
    } // 交叉计算 - 来自获取旋转角度

  }, {
    key: "cross",
    value: function cross(v1, v2) {
      return v1.x * v2.y - v2.x * v1.y;
    } // 获取转动角度

  }, {
    key: "getRotateAngle",
    value: function getRotateAngle(v1, v2) {
      var angle = this.getAngle(v1, v2);

      if (this.cross(v1, v2) > 0) {
        angle *= -1;
      }

      return angle * 180 / Math.PI;
    } // 事件监听器

  }, {
    key: "_emitEvent",
    value: function _emitEvent(name) {
      if (this.props[name]) {
        var _this$props;

        for (var _len = arguments.length, arg = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
          arg[_key - 1] = arguments[_key];
        }

        (_this$props = this.props)[name].apply(_this$props, arg);
      }
    } // 当按下手指时，触发ontouchstart

  }, {
    key: "_handleTouchStart",
    value: function _handleTouchStart(evt) {
      var _this2 = this;

      if (!evt.touches) return;
      this.now = Date.now();
      this.x1 = evt.touches[0].pageX;
      this.y1 = evt.touches[0].pageY;
      this.delta = this.now - (this.last || this.now);

      if (this.preTapPosition.x !== null) {
        this.isDoubleTap = this.delta > 0 && this.delta <= 250 && Math.abs(this.preTapPosition.x - this.x1) < 30 && Math.abs(this.preTapPosition.y - this.y1) < 30;
      }

      this.preTapPosition.x = this.x1;
      this.preTapPosition.y = this.y1;
      this.last = this.now;
      var preV = this.preV;
      var len = evt.touches.length;

      if (len > 1) {
        this._cancelLongTap();

        this._cancelSingleTap();

        var v = {
          x: evt.touches[1].pageX - this.x1,
          y: evt.touches[1].pageY - this.y1
        };
        preV.x = v.x;
        preV.y = v.y;
        this.pinchStartLen = this.getLen(preV);

        this._emitEvent('onMultipointStart', evt);
      }

      this.longTapTimeout = setTimeout(function () {
        _this2._emitEvent('onLongTap', evt);
      }, 750);
    } // 当移动手指时，触发ontouchmove

  }, {
    key: "_handleTouchMove",
    value: function _handleTouchMove(evt) {
      var preV = this.preV;
      var len = evt.touches.length;
      var currentX = evt.touches[0].pageX;
      var currentY = evt.touches[0].pageY;
      this.isDoubleTap = false;

      if (len > 1) {
        var v = {
          x: evt.touches[1].pageX - currentX,
          y: evt.touches[1].pageY - currentY
        };

        if (preV.x !== null) {
          if (this.pinchStartLen > 0) {
            evt.center = {
              x: (evt.touches[1].pageX + currentX) / 2,
              y: (evt.touches[1].pageY + currentY) / 2
            };
            evt.scale = this.getLen(v) / this.pinchStartLen;

            this._emitEvent('onPinch', evt);
          }

          evt.angle = this.getRotateAngle(v, preV);

          this._emitEvent('onRotate', evt);
        }

        preV.x = v.x;
        preV.y = v.y;
        this.multiTouch = true;
      } else {
        if (this.x2 !== null) {
          evt.deltaX = currentX - this.x2;
          evt.deltaY = currentY - this.y2;
        } else {
          evt.deltaX = 0;
          evt.deltaY = 0;
        }

        this._emitEvent('onPressMove', evt);
      }

      this._cancelLongTap();

      this.x2 = currentX;
      this.y2 = currentY;
    } // 当一些其他的事件发生的时候（如电话接入或者弹出信息）会取消当前的touch操作，即触发ontouchcancel

  }, {
    key: "_handleTouchCancel",
    value: function _handleTouchCancel() {
      clearInterval(this.singleTapTimeout);
      clearInterval(this.tapTimeout);
      clearInterval(this.longTapTimeout);
      clearInterval(this.swipeTimeout);
    } // 当移走手指时，触发ontouchend

  }, {
    key: "_handleTouchEnd",
    value: function _handleTouchEnd(evt) {
      var _this3 = this;

      this.end = Date.now();

      this._cancelLongTap();

      if (evt.touches.length < 2) {
        this._emitEvent('onMultipointEnd', evt);
      }

      evt.origin = [this.x1, this.y1];

      if (this.multiTouch === false) {
        if (this.x2 && Math.abs(this.x1 - this.x2) > 30 || this.y2 && Math.abs(this.preV.y - this.y2) > 30) {
          evt.direction = this._swipeDirection(this.x1, this.x2, this.y1, this.y2);
          evt.distance = Math.abs(this.x1 - this.x2);
          this.swipeTimeout = setTimeout(function () {
            _this3._emitEvent('onSwipe', evt);
          }, 0);
        } else {
          this.tapTimeout = setTimeout(function () {
            _this3._emitEvent('onTap', evt);

            if (_this3.isDoubleTap) {
              _this3._emitEvent('onDoubleTap', evt);

              clearTimeout(_this3.singleTapTimeout);
              _this3.isDoubleTap = false;
            } else {
              _this3.singleTapTimeout = setTimeout(function () {
                _this3._emitEvent('onSingleTap', evt);
              }, 250);
            }
          }, 0);
        }
      }

      this.preV.x = 0;
      this.preV.y = 0;
      this.scale = 1;
      this.pinchStartLen = null;
      this.x1 = null;
      this.x2 = null;
      this.y1 = null;
      this.y2 = null;
      this.multiTouch = false;
    } // 取消长按

  }, {
    key: "_cancelLongTap",
    value: function _cancelLongTap() {
      clearTimeout(this.longTapTimeout);
    } // 取消单按

  }, {
    key: "_cancelSingleTap",
    value: function _cancelSingleTap() {
      clearTimeout(this.singleTapTimeout);
    }
    /**
     * 切换的方向
     *
     * @param {number} x1 当触发ontouchstart时，鼠标指针相对于整个文档的X坐标
     * @param {number} x2 当触发ontouchmove时，鼠标指针相对于整个文档的X坐标
     * @param {number} y1 当触发ontouchstart时，鼠标指针相对于整个文档的Y坐标
     * @param {number} y2 当触发ontouchmove时，鼠标指针相对于整个文档的Y坐标
     * @return {string} 方向的值
     */

  }, {
    key: "_swipeDirection",
    value: function _swipeDirection(x1, x2, y1, y2) {
      if (Math.abs(x1 - x2) > 80 || this.end - this.now < 250) {
        return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? x1 - x2 > 0 ? 'Left' : 'Right' : y1 - y2 > 0 ? 'Up' : 'Down';
      }

      return 'Nochange';
    }
  }, {
    key: "render",
    value: function render() {
      return React.cloneElement(React.Children.only(this.props.children), {
        onTouchStart: this._handleTouchStart.bind(this),
        onTouchMove: this._handleTouchMove.bind(this),
        onTouchCancel: this._handleTouchCancel.bind(this),
        onTouchEnd: this._handleTouchEnd.bind(this)
      });
    }
  }]);

  return Finger;
}(Component);

export { Finger as default };