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

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

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import toNumber from 'lodash.tonumber';
import { tagPropType, mapToCssModules } from './Shared/helper.js';

//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 = toNumber(value);
  var percent = valueNumber / toNumber(max) * 100;

  // render

  var progressClasses = mapToCssModules(classNames(className, size ? 'progress-' + size : null, 'progress'), cssModule);

  var progressBarClasses = mapToCssModules(classNames('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 : React.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 React.createElement(Tag, _extends({}, attributes, { style: style, className: progressClasses, children: ProgressBar, ref: innerRef }));
};

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

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

export default CProgress;