function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

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

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/*
Copyright (c) 2018 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 React from 'react';
import Dropzone from 'react-dropzone';
import { Button, KIND } from '../button/index.js';
import { getOverrides } from '../helpers/overrides.js';
import { ProgressBar } from '../progress-bar/index.js';
import { StyledRoot, StyledFileDragAndDrop, StyledContentMessage, StyledErrorMessage, StyledHiddenInput } from './styled-components.js';

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

function makeOverride(Override, Source) {
  var OverriddenComponent = function OverriddenComponent(props) {
    var _getOverrides = getOverrides(Override, Source),
        _getOverrides2 = _slicedToArray(_getOverrides, 2),
        Overridden = _getOverrides2[0],
        overrideProps = _getOverrides2[1];

    return React.createElement(Overridden, _extends({}, props, overrideProps));
  };

  OverriddenComponent.displayName = Source.displayName;
  return OverriddenComponent;
}

function makeOverrides() {
  var overrides = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return {
    Root: makeOverride(overrides.Root, StyledRoot),
    FileDragAndDrop: makeOverride(overrides.FileDragAndDrop, StyledFileDragAndDrop),
    ContentMessage: makeOverride(overrides.ContentMessage, StyledContentMessage),
    ErrorMessage: makeOverride(overrides.ErrorMessage, StyledErrorMessage),
    HiddenInput: makeOverride(overrides.HiddenInput, StyledHiddenInput)
  };
}

function Unstable_FileUploader(props) {
  var _makeOverrides = makeOverrides(props.overrides),
      Root = _makeOverrides.Root,
      FileDragAndDrop = _makeOverrides.FileDragAndDrop,
      ContentMessage = _makeOverrides.ContentMessage,
      ErrorMessage = _makeOverrides.ErrorMessage,
      HiddenInput = _makeOverrides.HiddenInput;

  var afterFileDrop = !!(props.progressAmount || props.progressMessage || props.errorMessage);
  return React.createElement(Dropzone, _extends({}, props, {
    disabled: props.disabled || afterFileDrop
  }), function (renderProps) {
    var getRootProps = renderProps.getRootProps,
        getInputProps = renderProps.getInputProps,
        open = renderProps.open,
        styleProps = _objectWithoutProperties(renderProps, ["getRootProps", "getInputProps", "open"]);

    var prefixedStyledProps = prependStyleProps(_objectSpread({}, styleProps, {
      isDisabled: props.disabled,
      afterFileDrop: afterFileDrop
    }));
    return React.createElement(Root, prefixedStyledProps, React.createElement(FileDragAndDrop, _extends({}, getRootProps({
      refKey: '$ref'
    }), prefixedStyledProps), !afterFileDrop && React.createElement(React.Fragment, null, React.createElement(ContentMessage, prefixedStyledProps, "Drop files here to upload"), React.createElement(ContentMessage, prefixedStyledProps, "or"), React.createElement(Button, _extends({
      "aria-controls": "fileupload",
      disabled: props.disabled,
      kind: KIND.minimal,
      onClick: open,
      overrides: {
        BaseButton: {
          style: {
            outline: null,
            fontWeight: 'normal'
          }
        }
      },
      role: "button"
    }, prefixedStyledProps), "Browse files")), afterFileDrop && React.createElement(React.Fragment, null, !!props.progressAmount && React.createElement(ProgressBar, {
      value: props.progressAmount,
      overrides: {
        BarProgress: {
          style: function style(_ref) {
            var $theme = _ref.$theme;
            return {
              backgroundColor: props.errorMessage ? $theme.colors.negative : $theme.colors.primary
            };
          }
        }
      }
    }), (props.errorMessage || props.progressMessage) && props.errorMessage ? React.createElement(ErrorMessage, null, props.errorMessage) : React.createElement(ContentMessage, null, props.progressMessage), props.errorMessage ? React.createElement(Button, {
      kind: KIND.minimal,
      onClick: function onClick() {
        props.onRetry && props.onRetry();
      },
      overrides: {
        BaseButton: {
          style: {
            outline: null,
            fontWeight: 'normal'
          }
        }
      }
    }, "Retry Upload") : React.createElement(Button, {
      kind: KIND.minimal,
      onClick: function onClick() {
        props.onCancel && props.onCancel();
      },
      overrides: {
        BaseButton: {
          style: function style(_ref2) {
            var $theme = _ref2.$theme;
            return {
              outline: null,
              fontWeight: 'normal',
              color: $theme.colors.negative
            };
          }
        }
      }
    }, "Cancel"))), React.createElement(HiddenInput, _extends({}, getInputProps({
      refKey: '$ref'
    }), prefixedStyledProps)));
  });
}

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