'use strict';

exports.__esModule = true;

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var BiDoughnut = (0, _react.forwardRef)(function (_ref, ref) {
  var style = _ref.style,
      _ref$borderWidth = _ref.borderWidth,
      borderWidth = _ref$borderWidth === undefined ? 3 : _ref$borderWidth,
      _ref$size = _ref.size,
      size = _ref$size === undefined ? 50 : _ref$size,
      _ref$duration = _ref.duration,
      duration = _ref$duration === undefined ? 1000 : _ref$duration,
      _ref$rotate = _ref.rotate,
      rotate = _ref$rotate === undefined ? 0 : _ref$rotate,
      _ref$delay = _ref.delay,
      delay = _ref$delay === undefined ? 100 : _ref$delay,
      _ref$captionAttribute = _ref.captionAttribute,
      captionAttribute = _ref$captionAttribute === undefined ? {} : _ref$captionAttribute,
      children = _ref.children,
      others = _objectWithoutProperties(_ref, ['style', 'borderWidth', 'size', 'duration', 'rotate', 'delay', 'captionAttribute', 'children']);

  var refEl = (0, _react.useRef)(null);
  var refElLeftCircle = (0, _react.useRef)(null);
  var refElRightCircle = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });

  function getDuration() {
    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
    };
  }
  function getRotate() {
    var rotateLeft = -135; // 左circle旋转角度
    var rotateRight = -135 + rotate; // 右circle旋转角度
    if (rotate > 180) {
      rotateRight = 45;
      rotateLeft = -135 + (rotate - 180);
    }
    return {
      rotateLeft: rotateLeft,
      rotateRight: rotateRight
    };
  }
  // 只有延迟100毫秒动画才会生效
  function aniRotate() {
    // 时长与延时
    var duration = getDuration();
    var durationLeft = duration.durationLeft;
    var delayLeft = duration.delayLeft;
    var durationRight = duration.durationRight;
    // 旋转
    var rotate = getRotate();
    var rotateLeft = rotate.rotateLeft;
    var rotateRight = rotate.rotateRight;
    setTimeout(function () {
      if (refElLeftCircle.current) {
        refElLeftCircle.current.style.WebkitTransitionDuration = durationLeft + 'ms';
        refElLeftCircle.current.style.WebkitTransitionDelay = delayLeft + 'ms';
      }
      if (refElRightCircle.current) {
        refElRightCircle.current.style.WebkitTransitionDuration = durationRight + 'ms';
      }
      if (refElLeftCircle.current) {
        refElLeftCircle.current.style.WebkitTransform = 'rotate(' + rotateLeft + 'deg)';
      }
      if (refElRightCircle.current) {
        refElRightCircle.current.style.WebkitTransform = 'rotate(' + rotateRight + 'deg)';
      }
    }, delay);
  }
  // 动画旋转
  aniRotate();
  return _react2.default.createElement(
    'div',
    _extends({ ref: refEl }, 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: refElLeftCircle, className: 'bi-doughtut-circle left', style: { borderWidth: borderWidth + 'px', width: size - borderWidth * 2 + 'px', height: size - borderWidth * 2 + 'px' } })
    ),
    _react2.default.createElement(
      'div',
      { className: 'bi-doughtut-wrapper right' },
      _react2.default.createElement('div', { ref: refElRightCircle, className: 'bi-doughtut-circle right', style: { borderWidth: borderWidth + 'px', width: size - borderWidth * 2 + 'px', height: size - borderWidth * 2 + 'px' } })
    ),
    _react2.default.createElement(
      'div',
      _extends({}, captionAttribute, {
        className: 'bi-doughtut-caption' + (captionAttribute.className ? ' ' + captionAttribute.className : '')
      }),
      children
    )
  );
});

exports.default = BiDoughnut;
module.exports = exports['default'];