var _excluded = ["style", "borderWidth", "size", "duration", "rotate", "delay", "captionAttribute", "children"];

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { forwardRef, useRef, useImperativeHandle } from 'react';
var BiDoughnut = forwardRef(function (_ref, ref) {
  var style = _ref.style,
      _ref$borderWidth = _ref.borderWidth,
      borderWidth = _ref$borderWidth === void 0 ? 3 : _ref$borderWidth,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 50 : _ref$size,
      _ref$duration = _ref.duration,
      duration = _ref$duration === void 0 ? 1000 : _ref$duration,
      _ref$rotate = _ref.rotate,
      rotate = _ref$rotate === void 0 ? 0 : _ref$rotate,
      _ref$delay = _ref.delay,
      delay = _ref$delay === void 0 ? 100 : _ref$delay,
      _ref$captionAttribute = _ref.captionAttribute,
      captionAttribute = _ref$captionAttribute === void 0 ? {} : _ref$captionAttribute,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var rootRef = useRef(null);
  var leftCircleRef = useRef(null);
  var rightCircleRef = useRef(null);
  useImperativeHandle(ref, function () {
    return rootRef.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 (leftCircleRef.current) {
        leftCircleRef.current.style.WebkitTransitionDuration = durationLeft + "ms";
        leftCircleRef.current.style.WebkitTransitionDelay = delayLeft + "ms";
      }

      if (rightCircleRef.current) {
        rightCircleRef.current.style.WebkitTransitionDuration = durationRight + "ms";
      }

      if (leftCircleRef.current) {
        leftCircleRef.current.style.WebkitTransform = "rotate(" + rotateLeft + "deg)";
      }

      if (rightCircleRef.current) {
        rightCircleRef.current.style.WebkitTransform = "rotate(" + rotateRight + "deg)";
      }
    }, delay);
  } // 动画旋转


  aniRotate();
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: rootRef
  }, others, {
    className: "bi-doughtut" + (others.className ? ' ' + others.className : ''),
    style: Object.assign({
      width: size + "px",
      height: size + "px"
    }, style || {})
  }), /*#__PURE__*/React.createElement("div", {
    className: "bi-doughtut-wrapper left"
  }, /*#__PURE__*/React.createElement("div", {
    ref: leftCircleRef,
    className: "bi-doughtut-circle left",
    style: {
      borderWidth: borderWidth + "px",
      width: size - borderWidth * 2 + "px",
      height: size - borderWidth * 2 + "px"
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "bi-doughtut-wrapper right"
  }, /*#__PURE__*/React.createElement("div", {
    ref: rightCircleRef,
    className: "bi-doughtut-circle right",
    style: {
      borderWidth: borderWidth + "px",
      width: size - borderWidth * 2 + "px",
      height: size - borderWidth * 2 + "px"
    }
  })), /*#__PURE__*/React.createElement("div", _extends({}, captionAttribute, {
    className: "bi-doughtut-caption" + (captionAttribute.className ? ' ' + captionAttribute.className : '')
  }), children));
});
export default BiDoughnut;