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

/*
Copyright (c) Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { getOverrides } from '../helpers/overrides.js';
import { SIZE } from './constants.js';
import { StyledRoot, StyledBarContainer, StyledBar, StyledLabel, StyledBarProgress, StyledInfiniteBar } from './styled-components.js';

class ProgressBar extends React.Component {
  componentDidMount() {
    // TODO(v11): remove warning when switching default Spinner
    if (process.env.NODE_ENV !== "production") {
      if (this.props.errorMessage) {
        console.warn('baseui:ProgressBar The `errorMessage` prop is deprecated in WAI-ARIA v1.2.');
      }
    }
  }

  render() {
    const {
      ariaLabel,
      overrides = {},
      getProgressLabel,
      value,
      size,
      steps,
      successValue,
      showLabel,
      infinite,
      errorMessage,
      forwardedRef,
      ...restProps
    } = this.props;
    const [Root, rootProps] = getOverrides(overrides.Root, StyledRoot);
    const [BarContainer, barContainerProps] = getOverrides(overrides.BarContainer, StyledBarContainer);
    const [Bar, barProps] = getOverrides(overrides.Bar, StyledBar);
    const [BarProgress, barProgressProps] = getOverrides(overrides.BarProgress, StyledBarProgress);
    const [Label, labelProps] = getOverrides(overrides.Label, StyledLabel);
    const [InfiniteBar, infiniteBarProps] = getOverrides(overrides.InfiniteBar, StyledInfiniteBar);
    const sharedProps = {
      $infinite: infinite,
      $size: size,
      $steps: steps,
      $successValue: successValue,
      $value: value
    };

    function renderProgressBar() {
      const children = [];

      for (let i = 0; i < steps; i++) {
        children.push( /*#__PURE__*/React.createElement(Bar, _extends({
          key: i
        }, sharedProps, barProps), /*#__PURE__*/React.createElement(BarProgress, _extends({
          $index: i
        }, sharedProps, barProgressProps))));
      }

      return children;
    }

    return (
      /*#__PURE__*/
      // eslint-disable-next-line jsx-a11y/role-supports-aria-props
      React.createElement(Root, _extends({
        ref: forwardedRef,
        "data-baseweb": "progress-bar",
        role: "progressbar",
        "aria-label": ariaLabel || getProgressLabel(value, successValue),
        "aria-valuenow": infinite ? null : value,
        "aria-valuemin": infinite ? null : 0,
        "aria-valuemax": infinite ? null : successValue,
        "aria-invalid": errorMessage ? true : null,
        "aria-errormessage": errorMessage
      }, restProps, sharedProps, rootProps), /*#__PURE__*/React.createElement(BarContainer, _extends({}, sharedProps, barContainerProps), infinite ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(InfiniteBar, _extends({
        $isLeft: true,
        $size: sharedProps.$size
      }, infiniteBarProps)), /*#__PURE__*/React.createElement(InfiniteBar, _extends({
        $size: sharedProps.$size
      }, infiniteBarProps))) : renderProgressBar()), showLabel && /*#__PURE__*/React.createElement(Label, _extends({}, sharedProps, labelProps), getProgressLabel(value, successValue)))
    );
  }

}

_defineProperty(ProgressBar, "defaultProps", {
  getProgressLabel: (value, successValue) => `${Math.round(value / successValue * 100)}% Loaded`,
  infinite: false,
  overrides: {},
  showLabel: false,
  size: SIZE.medium,
  steps: 1,
  successValue: 100,
  value: 0
});

const ForwardedProgressBar = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(ProgressBar, _extends({
  forwardedRef: ref
}, props)));
ForwardedProgressBar.displayName = 'ProgressBar';
export default ForwardedProgressBar;