"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

var _bem = require("@zarm-design/bem");

var _configProvider = require("../config-provider");

var _hooks = require("./hooks");

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) { (0, _defineProperty2.default)(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; }

var Progress = /*#__PURE__*/_react.default.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 = (0, _objectWithoutProperties2.default)(_ref, ["theme", "shape", "size", "percent", "strokeShape", "text", "className", "style", "children", "strokeWidth"]);
  var progressElement = (0, _react.useRef)(null);

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

  var shouldUseSVG = (0, _hooks.useSVG)({
    shape: shape
  });
  var strokeWidthWithBackup = (0, _hooks.useStrokeWidthWithBackup)({
    strokeWidth: strokeWidth
  });

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

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

  var _useRoundSVGAttribute = (0, _hooks.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 = (0, _hooks.useRectStyles)({
    percent: percent,
    strokeWidthWithBackup: strokeWidthWithBackup,
    finalStrokeWidth: finalStrokeWidth,
    strokeShape: strokeShape
  }),
      lineThumbStyle = _useRectStyles.lineThumbStyle,
      lineTrackStyle = _useRectStyles.lineTrackStyle;

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

  var _React$useContext = _react.default.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = (0, _bem.createBEM)('progress', {
    prefixCls: prefixCls
  });
  var cls = bem([(_ref2 = {}, (0, _defineProperty2.default)(_ref2, "".concat(shape), !!shape), (0, _defineProperty2.default)(_ref2, "".concat(theme), !!theme), (0, _defineProperty2.default)(_ref2, "".concat(size), hasKnownSize), _ref2), className]);

  var roundInner = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("svg", {
    viewBox: viewBox
  }, /*#__PURE__*/_react.default.createElement("path", {
    className: bem('track'),
    d: path,
    strokeWidth: finalStrokeWidth,
    strokeLinecap: strokeLinecap
  }), /*#__PURE__*/_react.default.createElement("path", {
    className: bem('thumb'),
    d: path,
    strokeWidth: finalStrokeWidth,
    strokeLinecap: strokeLinecap,
    strokeDasharray: dasharray,
    strokeDashoffset: dashoffset
  })), indicator && /*#__PURE__*/_react.default.createElement("div", {
    className: bem('text')
  }, indicator));

  var rectInner = /*#__PURE__*/_react.default.createElement("div", {
    className: bem('outer')
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('track'),
    style: lineTrackStyle
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('thumb'),
    style: lineThumbStyle
  })), indicator && /*#__PURE__*/_react.default.createElement("div", {
    className: bem('text')
  }, indicator));

  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    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, "%");
  }
};
var _default = Progress;
exports.default = _default;