"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx = _interopRequireDefault(require("clsx"));

var _utils = require("../utils");

var _raf = require("../utils/raf");

var _useMergedState2 = _interopRequireDefault(require("../hooks/use-merged-state"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

var uid = 0;

function format(rate) {
  return Math.min(Math.max(+rate, 0), 100);
}

function getPath(clockwise, viewBoxSize) {
  var sweepFlag = clockwise ? 1 : 0;
  return "M " + viewBoxSize / 2 + " " + viewBoxSize / 2 + " m 0, -500 a 500, 500 0 1, " + sweepFlag + " 0, 1000 a 500, 500 0 1, " + sweepFlag + " 0, -1000";
}

var ROTATE_ANGLE_MAP = {
  top: 0,
  right: 90,
  bottom: 180,
  left: 270
};

var Circle = function Circle(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('circle', prefixCls),
      bem = _createNamespace[0]; // eslint-disable-next-line no-plusplus


  var id = "van-circle-" + uid++;

  var _useState = (0, _react.useState)(function () {
    return props.defaultRate || 0;
  }),
      currentRate = _useState[0],
      setCurrentRate = _useState[1];

  var _useMergedState = (0, _useMergedState2.default)({
    defaultValue: props.defaultRate,
    value: props.rate
  }),
      current = _useMergedState[0];

  var viewBoxSize = (0, _react.useMemo)(function () {
    return +props.strokeWidth + 1000;
  }, [props.strokeWidth]);
  var path = (0, _react.useMemo)(function () {
    return getPath(props.clockwise, viewBoxSize);
  }, [props.clockwise, viewBoxSize]);
  var svgStyle = (0, _react.useMemo)(function () {
    var angleValue = ROTATE_ANGLE_MAP[props.startPosition];

    if (angleValue) {
      return {
        transform: "rotate(" + angleValue + "deg)"
      };
    }

    return undefined;
  }, [props.startPosition]);
  (0, _react.useEffect)(function () {
    var rafId;
    var startTime = Date.now();
    var startRate = currentRate;
    var endRate = format(current);
    var duration = Math.abs((startRate - endRate) * 1000 / +props.speed);

    var animate = function animate() {
      var now = Date.now();
      var progress = Math.min((now - startTime) / duration, 1);
      var rate = progress * (endRate - startRate) + startRate;
      var crate = format(parseFloat(rate.toFixed(1)));
      setCurrentRate(crate);

      if (endRate > startRate ? rate < endRate : rate > endRate) {
        rafId = (0, _raf.raf)(animate);
      } else {
        props.onChange == null ? void 0 : props.onChange(crate);
      }
    };

    if (props.speed) {
      if (rafId) {
        (0, _raf.cancelRaf)(rafId);
      }

      rafId = (0, _raf.raf)(animate);
    } else {
      setCurrentRate(endRate);
    }
  }, [current]);

  var renderHover = function renderHover() {
    var PERIMETER = 3140;
    var strokeWidth = props.strokeWidth;
    var offset = PERIMETER * currentRate / 100;
    var color = (0, _utils.isObject)(props.color) ? "url(#" + id + ")" : props.color;
    var style = {
      stroke: color,
      strokeWidth: +strokeWidth + 1 + "px",
      strokeLinecap: props.strokeLinecap,
      strokeDasharray: offset + "px " + PERIMETER + "px"
    };
    return /*#__PURE__*/_react.default.createElement("path", {
      d: path,
      style: style,
      className: (0, _clsx.default)(bem('hover')),
      stroke: color
    });
  };

  var renderLayer = function renderLayer() {
    var style = {
      fill: props.fill,
      stroke: props.layerColor,
      strokeWidth: props.strokeWidth + "px"
    };
    return /*#__PURE__*/_react.default.createElement("path", {
      className: (0, _clsx.default)(bem('layer')),
      style: style,
      d: path
    });
  };

  var renderGradient = function renderGradient() {
    var color = props.color;

    if (!(0, _utils.isObject)(color)) {
      return null;
    }

    var Stops = Object.keys(color).sort(function (a, b) {
      return parseFloat(a) - parseFloat(b);
    }) // eslint-disable-next-line react/no-array-index-key
    .map(function (key, index) {
      return /*#__PURE__*/_react.default.createElement("stop", {
        key: index,
        offset: key,
        stopColor: color[key]
      });
    });
    return /*#__PURE__*/_react.default.createElement("defs", null, /*#__PURE__*/_react.default.createElement("linearGradient", {
      id: id,
      x1: "100%",
      y1: "0%",
      x2: "0%",
      y2: "0%"
    }, Stops));
  };

  var renderText = function renderText() {
    if (props.text) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('text'))
      }, props.text);
    }

    return props.children;
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem(), props.className),
    style: _extends({}, props.style, (0, _utils.getSizeStyle)(props.size))
  }, /*#__PURE__*/_react.default.createElement("svg", {
    viewBox: "0 0 " + viewBoxSize + " " + viewBoxSize,
    style: svgStyle
  }, renderGradient(), renderLayer(), renderHover()), renderText());
};

Circle.defaultProps = {
  clockwise: true,
  speed: 100,
  fill: 'none',
  strokeWidth: 40,
  startPosition: 'top'
};
var _default = Circle;
exports.default = _default;