var _excluded = ["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 BiGauge = forwardRef(function (_ref, ref) {
  var _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,
      captionAttribute = _ref.captionAttribute,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var rootRef = useRef(null);
  var pointerRef = useRef(null);
  useImperativeHandle(ref, function () {
    return rootRef.current;
  });

  function getDuration() {
    return duration / 270 * rotate;
  }

  function getRotate() {
    return rotate > 270 ? 270 : rotate;
  }

  function getBgLvl() {
    var lvl = Math.round(rotate / 27);
    return lvl > 10 ? 10 : 1;
  } // 只有延迟100毫秒动画才会生效


  function aniRotate() {
    // 时长
    var duration = getDuration(); // 旋转

    var rotate = getRotate(); // 背景

    var bgLvl = 'bg' + getBgLvl();
    setTimeout(function () {
      if (rootRef.current) {
        rootRef.current.style.WebkitAnimationDuration = duration + "ms";
        rootRef.current.classList.add(bgLvl);
      }

      if (pointerRef.current) {
        pointerRef.current.style.WebkitTransitionDuration = duration + "ms";
        pointerRef.current.style.WebkitTransform = "rotate(" + rotate + "deg)";
      }
    }, delay);
  } // 动画旋转


  aniRotate();
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: rootRef
  }, others, {
    className: "bi-gauge-box" + (others.className ? ' ' + others.className : '')
  }), /*#__PURE__*/React.createElement("div", {
    className: "bi-gauge"
  }, /*#__PURE__*/React.createElement("div", {
    ref: pointerRef,
    className: "bi-gauge-pointer"
  }), /*#__PURE__*/React.createElement("div", _extends({}, captionAttribute, {
    className: "bi-gauge-caption" + (captionAttribute.className ? ' ' + captionAttribute.className : '')
  }), children)));
});
export default BiGauge;