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) 2018-2019 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 { BaseButton as StyledBaseButton, LoadingSpinner as StyledLoadingSpinner, LoadingSpinnerContainer as StyledLoadingSpinnerContainer } from './styled-components.js';
import { getSharedProps } from './utils.js';
import ButtonInternals from './button-internals.js';
import { defaultProps } from './default-props.js';
import { getOverrides } from '../helpers/overrides.js';

// eslint-disable-next-line flowtype/no-weak-types
class Button extends React.Component {
  constructor(..._args) {
    super(..._args);

    _defineProperty(this, "internalOnClick", (...args) => {
      const {
        isLoading,
        onClick
      } = this.props;

      if (isLoading) {
        return;
      }

      onClick && onClick(...args);
    });
  }

  render() {
    const {
      overrides = {},
      size,
      kind,
      shape,
      isLoading,
      isSelected,
      // Removing from restProps
      startEnhancer,
      endEnhancer,
      children,
      forwardedRef,
      ...restProps
    } = this.props; // Get overrides

    const [BaseButton, baseButtonProps] = getOverrides(overrides.BaseButton, StyledBaseButton);
    const [LoadingSpinner, loadingSpinnerProps] = getOverrides(overrides.LoadingSpinner, StyledLoadingSpinner);
    const [LoadingSpinnerContainer, loadingSpinnerContainerProps] = getOverrides(overrides.LoadingSpinnerContainer, StyledLoadingSpinnerContainer);
    const sharedProps = getSharedProps(this.props);
    return React.createElement(BaseButton, _extends({
      ref: forwardedRef,
      "data-baseweb": "button"
    }, sharedProps, restProps, baseButtonProps, {
      // Applies last to override passed in onClick
      onClick: this.internalOnClick
    }), isLoading ? React.createElement(React.Fragment, null, React.createElement("div", {
      style: {
        opacity: 0,
        display: 'flex'
      }
    }, React.createElement(ButtonInternals, this.props)), React.createElement(LoadingSpinnerContainer, loadingSpinnerContainerProps, React.createElement(LoadingSpinner, _extends({}, sharedProps, loadingSpinnerProps)))) : React.createElement(ButtonInternals, this.props));
  }

}

_defineProperty(Button, "defaultProps", defaultProps);

const ForwardedButton = React.forwardRef((props, ref) => React.createElement(Button, _extends({
  forwardedRef: ref
}, props)));
ForwardedButton.displayName = 'Button';
export default ForwardedButton;