function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }

/*
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 Dropzone from 'react-dropzone';
import { LocaleContext } from '../locale';
import { useStyletron } from '../styles';
import { Button, KIND, SHAPE, SIZE as BUTTON_SIZE } from '../button';
import { getOverrides } from '../helpers/overrides';
import { ProgressBar } from '../progress-bar';
import { Spinner, SIZE as SPINNER_SIZE } from '../spinner';
import { StyledRoot, StyledFileDragAndDrop, StyledContentMessage, StyledContentSeparator, StyledErrorMessage, StyledHiddenInput } from './styled-components';

function prependStyleProps(styleProps) {
  return Object.keys(styleProps).reduce((nextStyleProps, currentKey) => {
    nextStyleProps[`$${currentKey}`] = styleProps[currentKey];
    return nextStyleProps;
  }, {});
}

function FileUploader(props) {
  const {
    overrides = {}
  } = props;
  const [, theme] = useStyletron();
  const [Root, rootProps] = getOverrides(overrides.Root, StyledRoot);
  const [FileDragAndDrop, fileDragAndDropProps] = getOverrides(overrides.FileDragAndDrop, StyledFileDragAndDrop);
  const [ContentMessage, contentMessageProps] = getOverrides(overrides.ContentMessage, StyledContentMessage);
  const [ContentSeparator, contentSeparatorProps] = getOverrides(overrides.ContentSeparator, StyledContentSeparator);
  const [ErrorMessage, errorMessageProps] = getOverrides(overrides.ErrorMessage, StyledErrorMessage);
  const [HiddenInput, hiddenInputProps] = getOverrides(overrides.HiddenInput, StyledHiddenInput);
  const [ButtonComponent, buttonProps] = getOverrides(overrides.ButtonComponent, Button);
  const [RetryButtonComponent, retryButtonProps] = getOverrides(overrides.RetryButtonComponent, Button);
  const [CancelButtonComponent, cancelButtonProps] = getOverrides(overrides.CancelButtonComponent, Button);
  const [SpinnerComponent, spinnerProps] = getOverrides(overrides.Spinner, Spinner);
  const [ProgressBarComponent, progressBarProps] = getOverrides(overrides.ProgressBar, ProgressBar);
  const afterFileDrop = !!(props.progressAmount || props.progressMessage || props.errorMessage);
  return /*#__PURE__*/React.createElement(Dropzone, _extends({}, props, {
    disabled: props.disabled || afterFileDrop
  }), renderProps => {
    const {
      getRootProps,
      getInputProps,
      open,
      ...styleProps
    } = renderProps;
    const prefixedStyledProps = prependStyleProps({ ...styleProps,
      isDisabled: props.disabled,
      afterFileDrop
    });
    const getRootPropsArgs = { ...(props.disableClick ? {
        onClick: evt => evt.preventDefault()
      } : {}),
      tabIndex: -1
    };
    return /*#__PURE__*/React.createElement(LocaleContext.Consumer, null, locale => /*#__PURE__*/React.createElement(Root, _extends({
      "data-baseweb": "file-uploader"
    }, prefixedStyledProps, rootProps), /*#__PURE__*/React.createElement(FileDragAndDrop, _extends({}, getRootProps(getRootPropsArgs), prefixedStyledProps, fileDragAndDropProps), !afterFileDrop && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ContentMessage, _extends({}, prefixedStyledProps, contentMessageProps), locale.fileuploader.dropFilesToUpload), /*#__PURE__*/React.createElement(ContentSeparator, _extends({}, prefixedStyledProps, contentSeparatorProps), locale.fileuploader.or), /*#__PURE__*/React.createElement(ButtonComponent, _extends({
      disabled: props.disabled,
      kind: KIND.secondary,
      shape: SHAPE.pill,
      size: BUTTON_SIZE.compact,
      onClick: open,
      role: "button",
      overrides: {
        BaseButton: {
          style: ({
            $theme
          }) => ({
            marginTop: $theme.sizing.scale500
          })
        }
      }
    }, prefixedStyledProps, buttonProps), locale.fileuploader.browseFiles)), afterFileDrop && /*#__PURE__*/React.createElement(React.Fragment, null, typeof props.progressAmount === 'number' ? /*#__PURE__*/React.createElement(ProgressBarComponent, _extends({
      value: props.progressAmount,
      overrides: {
        BarProgress: {
          style: ({
            $theme
          }) => ({
            backgroundColor: props.errorMessage ? $theme.colors.negative : $theme.colors.accent
          })
        }
      }
    }, progressBarProps)) : props.errorMessage ? null : /*#__PURE__*/React.createElement(SpinnerComponent, _extends({
      $size: SPINNER_SIZE.medium,
      $style: {
        marginBottom: theme.sizing.scale300
      }
    }, spinnerProps)), (props.errorMessage || props.progressMessage) && props.errorMessage ? /*#__PURE__*/React.createElement(ErrorMessage, _extends({}, prefixedStyledProps, errorMessageProps), props.errorMessage) : /*#__PURE__*/React.createElement(ContentMessage, _extends({}, prefixedStyledProps, contentMessageProps), props.progressMessage), props.errorMessage ? /*#__PURE__*/React.createElement(RetryButtonComponent, _extends({
      kind: KIND.tertiary,
      onClick: () => {
        props.onRetry && props.onRetry();
      },
      "aria-invalid": Boolean(props.errorMessage),
      "aria-describedby": props['aria-describedby'],
      "aria-errormessage": props.errorMessage
    }, retryButtonProps), locale.fileuploader.retry) : /*#__PURE__*/React.createElement(CancelButtonComponent, _extends({
      kind: KIND.tertiary,
      onClick: () => {
        props.onCancel && props.onCancel();
      },
      "aria-describedby": props['aria-describedby'],
      overrides: {
        BaseButton: {
          style: ({
            $theme
          }) => ({
            color: $theme.colors.contentNegative
          })
        }
      }
    }, cancelButtonProps), locale.fileuploader.cancel))), /*#__PURE__*/React.createElement(HiddenInput, _extends({
      "aria-invalid": Boolean(props.errorMessage) || null,
      "aria-describedby": props['aria-describedby'],
      "aria-errormessage": props.errorMessage || null
    }, getInputProps(), prefixedStyledProps, hiddenInputProps))));
  });
}

FileUploader.defaultProps = {
  disableClick: true,
  overrides: {}
};
export default FileUploader;