"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Counter = _interopRequireDefault(require("./../Counter"));

var _excluded = ["barAttribute", "captionAttribute", "percentage", "max", "min", "value", "showPercentage", "children"];

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

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

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Progress = /*#__PURE__*/function (_Component) {
  _inheritsLoose(Progress, _Component);

  function Progress(props) {
    var _this;

    _this = _Component.call(this, props) || this;

    _defineProperty(_assertThisInitialized(_this), "componentDidMount", function () {});

    return _this;
  }

  var _proto = Progress.prototype;

  _proto.render = function render() {
    var _this2 = this;

    var _this$props = this.props,
        _this$props$barAttrib = _this$props.barAttribute,
        barAttribute = _this$props$barAttrib === void 0 ? {} : _this$props$barAttrib,
        _this$props$captionAt = _this$props.captionAttribute,
        captionAttribute = _this$props$captionAt === void 0 ? {} : _this$props$captionAt,
        percentage = _this$props.percentage,
        max = _this$props.max,
        min = _this$props.min,
        value = _this$props.value,
        showPercentage = _this$props.showPercentage,
        children = _this$props.children,
        others = _objectWithoutPropertiesLoose(_this$props, _excluded); // 计算百分比


    var barPercentage = 0;
    var textPercentage = 0;

    if (!isNaN(percentage)) {
      barPercentage = textPercentage = percentage;
    } else if (!isNaN(max) && !isNaN(value)) {
      barPercentage = textPercentage = value / (max - min) * 100;
    }

    if (barPercentage > 100) {
      barPercentage = 100;
    } else if (barPercentage < 0) {
      barPercentage = 0;
    }

    return /*#__PURE__*/_react["default"].createElement("div", _extends({}, others, {
      className: "progress" + (others.className ? ' ' + others.className : ''),
      style: style
    }), /*#__PURE__*/_react["default"].createElement("span", _extends({
      ref: function ref(el) {
        _this2.$bar = el;
      }
    }, barAttribute, {
      className: "progress-bar" + (barAttribute.className ? ' ' + barAttribute.className : ''),
      style: Object.assign({}, barAttribute.style || {}, {
        width: barPercentage + '%'
      })
    }), showPercentage && /*#__PURE__*/_react["default"].createElement(_Counter["default"], _extends({}, captionAttribute, {
      className: "progress-caption" + (captionAttribute.className ? ' ' + captionAttribute.className : ''),
      duration: 1000,
      to: textPercentage,
      from: 0,
      suffix: '%'
    })), !showPercentage && /*#__PURE__*/_react["default"].createElement(_Counter["default"], _extends({}, captionAttribute, {
      className: "progress-caption" + (captionAttribute.className ? ' ' + captionAttribute.className : ''),
      duration: 1000,
      to: Number(value),
      from: min,
      suffix: '/' + max
    }))), children);
  };

  return Progress;
}(_react.Component);

exports["default"] = Progress;

_defineProperty(Progress, "defaultProps", {
  showPercentage: true,
  max: 100,
  min: 0,
  value: 0
});

Progress.propTypes = process.env.NODE_ENV !== "production" ? {
  barAttribute: _propTypes["default"].object,
  captionAttribute: _propTypes["default"].object,
  percentage: _propTypes["default"].number,
  max: _propTypes["default"].number,
  min: _propTypes["default"].number,
  value: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
  showPercentage: _propTypes["default"].bool,
  children: _propTypes["default"].node
} : {};
module.exports = exports.default;