'use strict';

exports.__esModule = true;

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _lodash = require('lodash.tonumber');

var _lodash2 = _interopRequireDefault(_lodash);

var _helper = require('./Shared/helper.js');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

//component - CoreUI / CProgress

var CProgress = function CProgress(props) {
  var Tag = props.tag,
      children = props.children,
      className = props.className,
      cssModule = props.cssModule,
      innerRef = props.innerRef,
      barClassName = props.barClassName,
      barProps = props.barProps,
      height = props.height,
      value = props.value,
      max = props.max,
      animated = props.animated,
      striped = props.striped,
      color = props.color,
      bar = props.bar,
      multi = props.multi,
      showPercentage = props.showPercentage,
      showValue = props.showValue,
      precision = props.precision,
      size = props.size,
      attributes = _objectWithoutProperties(props, ['tag', 'children', 'className', 'cssModule', 'innerRef', 'barClassName', 'barProps', 'height', 'value', 'max', 'animated', 'striped', 'color', 'bar', 'multi', 'showPercentage', 'showValue', 'precision', 'size']);

  var valueNumber = (0, _lodash2.default)(value);
  var percent = valueNumber / (0, _lodash2.default)(max) * 100;

  // render

  var progressClasses = (0, _helper.mapToCssModules)((0, _classnames2.default)(className, size ? 'progress-' + size : null, 'progress'), cssModule);

  var progressBarClasses = (0, _helper.mapToCssModules)((0, _classnames2.default)('progress-bar', bar ? className || barClassName : barClassName, animated ? 'progress-bar-animated' : null, color ? 'bg-' + color : null, striped || animated ? 'progress-bar-striped' : null), cssModule);

  if (precision) percent = percent.toPrecision(precision);
  if (precision) valueNumber = valueNumber.toPrecision(precision);

  var ProgressBar = multi ? children : _react2.default.createElement('div', _extends({
    className: progressBarClasses,
    style: { width: percent + '%' },
    role: 'progressbar',
    'aria-valuenow': value,
    'aria-valuemin': '0',
    'aria-valuemax': max,
    children: showPercentage ? percent + '%' : showValue ? value : children
  }, barProps));

  if (bar) {
    return ProgressBar;
  }

  var style = void 0;
  if (height) style = { height: height };else style = {};

  return _react2.default.createElement(Tag, _extends({}, attributes, { style: style, className: progressClasses, children: ProgressBar, ref: innerRef }));
};

CProgress.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: _helper.tagPropType,
  children: _propTypes2.default.node,
  className: _propTypes2.default.string,
  cssModule: _propTypes2.default.object,
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  barClassName: _propTypes2.default.string,
  barProps: _propTypes2.default.object,
  height: _propTypes2.default.string,
  bar: _propTypes2.default.bool,
  multi: _propTypes2.default.bool,
  value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  max: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  animated: _propTypes2.default.bool,
  striped: _propTypes2.default.bool,
  color: _propTypes2.default.string,
  precision: _propTypes2.default.number,
  showPercentage: _propTypes2.default.bool,
  showValue: _propTypes2.default.bool,
  size: _propTypes2.default.string
} : {};

CProgress.defaultProps = {
  tag: 'div',
  value: 0,
  max: 100
};

exports.default = CProgress;
module.exports = exports['default'];