import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import React, { useImperativeHandle, useRef } from 'react';
import { createBEM } from '@zarm-design/bem';
import { ConfigContext } from '../config-provider';
import { useSizeStyle, useSVGStrokeWidth, useSVG, useStrokeWidthWithBackup, useRoundSVGAttributes, useRectStyles, useIndicator } from './hooks';
var Progress = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var _ref2;

  var theme = _ref.theme,
      shape = _ref.shape,
      size = _ref.size,
      percent = _ref.percent,
      strokeShape = _ref.strokeShape,
      text = _ref.text,
      className = _ref.className,
      style = _ref.style,
      children = _ref.children,
      strokeWidth = _ref.strokeWidth,
      htmlAttributes = _objectWithoutProperties(_ref, ["theme", "shape", "size", "percent", "strokeShape", "text", "className", "style", "children", "strokeWidth"]);

  var progressElement = useRef(null);

  var _useSizeStyle = useSizeStyle({
    shape: shape,
    size: size
  }),
      sizeStyle = _useSizeStyle.sizeStyle,
      hasKnownSize = _useSizeStyle.hasKnownSize;

  var shouldUseSVG = useSVG({
    shape: shape
  });
  var strokeWidthWithBackup = useStrokeWidthWithBackup({
    strokeWidth: strokeWidth
  });

  var _useSVGStrokeWidth = useSVGStrokeWidth({
    strokeWidth: strokeWidthWithBackup,
    size: size,
    shape: shape,
    progressElement: progressElement,
    shouldUseSVG: shouldUseSVG
  }),
      svgStrokeWidth = _useSVGStrokeWidth.svgStrokeWidth;

  useImperativeHandle(ref, function () {
    return progressElement.current;
  });
  var finalStrokeWidth = shouldUseSVG ? svgStrokeWidth : strokeWidthWithBackup;

  var _useRoundSVGAttribute = useRoundSVGAttributes({
    shape: shape,
    strokeShape: strokeShape,
    percent: percent,
    finalStrokeWidth: finalStrokeWidth
  }),
      viewBox = _useRoundSVGAttribute.viewBox,
      dasharray = _useRoundSVGAttribute.dasharray,
      strokeLinecap = _useRoundSVGAttribute.strokeLinecap,
      path = _useRoundSVGAttribute.path,
      dashoffset = _useRoundSVGAttribute.dashoffset;

  var _useRectStyles = useRectStyles({
    percent: percent,
    strokeWidthWithBackup: strokeWidthWithBackup,
    finalStrokeWidth: finalStrokeWidth,
    strokeShape: strokeShape
  }),
      lineThumbStyle = _useRectStyles.lineThumbStyle,
      lineTrackStyle = _useRectStyles.lineTrackStyle;

  var indicator = useIndicator({
    percent: percent,
    children: children,
    text: text
  });

  var _React$useContext = React.useContext(ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = createBEM('progress', {
    prefixCls: prefixCls
  });
  var cls = bem([(_ref2 = {}, _defineProperty(_ref2, "".concat(shape), !!shape), _defineProperty(_ref2, "".concat(theme), !!theme), _defineProperty(_ref2, "".concat(size), hasKnownSize), _ref2), className]);
  var roundInner = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("svg", {
    viewBox: viewBox
  }, /*#__PURE__*/React.createElement("path", {
    className: bem('track'),
    d: path,
    strokeWidth: finalStrokeWidth,
    strokeLinecap: strokeLinecap
  }), /*#__PURE__*/React.createElement("path", {
    className: bem('thumb'),
    d: path,
    strokeWidth: finalStrokeWidth,
    strokeLinecap: strokeLinecap,
    strokeDasharray: dasharray,
    strokeDashoffset: dashoffset
  })), indicator && /*#__PURE__*/React.createElement("div", {
    className: bem('text')
  }, indicator));
  var rectInner = /*#__PURE__*/React.createElement("div", {
    className: bem('outer')
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('track'),
    style: lineTrackStyle
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('thumb'),
    style: lineThumbStyle
  })), indicator && /*#__PURE__*/React.createElement("div", {
    className: bem('text')
  }, indicator));
  return /*#__PURE__*/React.createElement("div", _extends({
    className: cls,
    style: _objectSpread(_objectSpread({}, sizeStyle), style),
    ref: progressElement
  }, htmlAttributes), shape === 'line' && rectInner, (shape === 'circle' || shape === 'semi-circle') && roundInner);
});
Progress.displayName = 'Progress';
Progress.defaultProps = {
  theme: 'primary',
  shape: 'line',
  size: 'md',
  percent: 0,
  strokeShape: 'round',
  text: function text(val) {
    return "".concat(val, "%");
  }
};
export default Progress;