'use strict';

exports.__esModule = true;
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class, _temp;

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 }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var BiDoughnut = (_temp = _class = function (_Component) {
  _inherits(BiDoughnut, _Component);

  function BiDoughnut(props, context) {
    _classCallCheck(this, BiDoughnut);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));

    _this.getDuration = function () {
      var _this$props = _this.props,
          duration = _this$props.duration,
          rotate = _this$props.rotate;

      var correctRotate = rotate > 360 ? 360 : rotate;
      var duration1 = duration / 360;
      var durationRotate = duration1 * correctRotate;
      var durationLeft = 0;
      var durationRight = durationRotate;
      if (correctRotate > 180) {
        durationRight = duration1 * 180;
        durationLeft = duration1 * (correctRotate - 180);
      }
      return {
        durationRotate: durationRotate,
        delayLeft: durationRight,
        durationLeft: durationLeft,
        durationRight: durationRight
      };
    };

    _this.getRotate = function () {
      var rotate = _this.props.rotate;

      var rotateLeft = -135; // 左circle旋转角度
      var rotateRight = -135 + rotate; // 右circle旋转角度
      if (rotate > 180) {
        rotateRight = 45;
        rotateLeft = -135 + (rotate - 180);
      }
      return {
        rotateLeft: rotateLeft,
        rotateRight: rotateRight
      };
    };

    _this.aniRotate = function () {
      // 时长与延时
      var duration = _this.getDuration();
      var durationLeft = duration.durationLeft;
      var delayLeft = duration.delayLeft;
      var durationRight = duration.durationRight;
      // 旋转
      var rotate = _this.getRotate();
      var rotateLeft = rotate.rotateLeft;
      var rotateRight = rotate.rotateRight;
      setTimeout(function () {
        if (_this.$elLeftCircle) {
          _this.$elLeftCircle.style.WebkitTransitionDuration = durationLeft + 'ms';
          _this.$elLeftCircle.style.WebkitTransitionDelay = delayLeft + 'ms';
        }
        if (_this.$elRightCircle) {
          _this.$elRightCircle.style.WebkitTransitionDuration = durationRight + 'ms';
        }
        if (_this.$elLeftCircle) {
          _this.$elLeftCircle.style.WebkitTransform = 'rotate(' + rotateLeft + 'deg)';
        }
        if (_this.$elRightCircle) {
          _this.$elRightCircle.style.WebkitTransform = 'rotate(' + rotateRight + 'deg)';
        }
      }, _this.props.delay);
    };

    return _this;
  }
  // 只有延迟100毫秒动画才会生效


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

    var _props = this.props,
        style = _props.style,
        lineWidth = _props.lineWidth,
        size = _props.size,
        duration = _props.duration,
        rotate = _props.rotate,
        delay = _props.delay,
        className = _props.className,
        _props$captionAttribu = _props.captionAttribute,
        captionAttribute = _props$captionAttribu === undefined ? {} : _props$captionAttribu,
        children = _props.children,
        others = _objectWithoutProperties(_props, ['style', 'lineWidth', 'size', 'duration', 'rotate', 'delay', 'className', 'captionAttribute', 'children']);
    // 动画旋转


    this.aniRotate();
    return _react2.default.createElement(
      'div',
      _extends({}, others, { className: 'bi-doughtut' + (others.className ? ' ' + others.className : ''), style: Object.assign({ width: size + 'px', height: size + 'px' }, style || {}) }),
      _react2.default.createElement(
        'div',
        { className: 'bi-doughtut-wrapper left' },
        _react2.default.createElement('div', { ref: function ref(el) {
            _this2.$elLeftCircle = el;
          }, className: 'bi-doughtut-circle left', style: { borderWidth: lineWidth + 'px', width: size - lineWidth * 2 + 'px', height: size - lineWidth * 2 + 'px' } })
      ),
      _react2.default.createElement(
        'div',
        { className: 'bi-doughtut-wrapper right' },
        _react2.default.createElement('div', { ref: function ref(el) {
            _this2.$elRightCircle = el;
          }, className: 'bi-doughtut-circle right', style: { borderWidth: lineWidth + 'px', width: size - lineWidth * 2 + 'px', height: size - lineWidth * 2 + 'px' } })
      ),
      _react2.default.createElement(
        'div',
        _extends({}, captionAttribute, { className: 'bi-doughtut-caption' + (captionAttribute.className ? ' ' + captionAttribute.className : '') }),
        children
      )
    );
  };

  return BiDoughnut;
}(_react.Component), _class.defaultProps = {
  lineWidth: 3,
  size: 50,
  duration: 1000,
  rotate: 0,
  delay: 100,
  className: 'disabled'
}, _temp);
exports.default = BiDoughnut;
BiDoughnut.propTypes = process.env.NODE_ENV !== "production" ? {
  style: _propTypes2.default.object,
  lineWidth: _propTypes2.default.number, // 边框宽度
  size: _propTypes2.default.number, // 大小,px
  duration: _propTypes2.default.number, // 时长
  rotate: _propTypes2.default.number, // 最大360
  delay: _propTypes2.default.number, // 延时
  captionAttribute: _propTypes2.default.object,
  children: _propTypes2.default.node
} : {};
module.exports = exports['default'];