"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _reactDropzone = _interopRequireDefault(require("react-dropzone"));

var _index = require("../locale/index.js");

var _index2 = require("../block/index.js");

var _index3 = require("../button/index.js");

var _overrides = require("../helpers/overrides.js");

var _index4 = require("../progress-bar/index.js");

var _index5 = require("../spinner/index.js");

var _styledComponents = require("./styled-components.js");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

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

function FileUploader(props) {
  var _props$overrides = props.overrides,
      overrides = _props$overrides === void 0 ? {} : _props$overrides;

  var _getOverrides = (0, _overrides.getOverrides)(overrides.Root, _styledComponents.StyledRoot),
      _getOverrides2 = _slicedToArray(_getOverrides, 2),
      Root = _getOverrides2[0],
      rootProps = _getOverrides2[1];

  var _getOverrides3 = (0, _overrides.getOverrides)(overrides.FileDragAndDrop, _styledComponents.StyledFileDragAndDrop),
      _getOverrides4 = _slicedToArray(_getOverrides3, 2),
      FileDragAndDrop = _getOverrides4[0],
      fileDragAndDropProps = _getOverrides4[1];

  var _getOverrides5 = (0, _overrides.getOverrides)(overrides.ContentMessage, _styledComponents.StyledContentMessage),
      _getOverrides6 = _slicedToArray(_getOverrides5, 2),
      ContentMessage = _getOverrides6[0],
      contentMessageProps = _getOverrides6[1];

  var _getOverrides7 = (0, _overrides.getOverrides)(overrides.ErrorMessage, _styledComponents.StyledErrorMessage),
      _getOverrides8 = _slicedToArray(_getOverrides7, 2),
      ErrorMessage = _getOverrides8[0],
      errorMessageProps = _getOverrides8[1];

  var _getOverrides9 = (0, _overrides.getOverrides)(overrides.HiddenInput, _styledComponents.StyledHiddenInput),
      _getOverrides10 = _slicedToArray(_getOverrides9, 2),
      HiddenInput = _getOverrides10[0],
      hiddenInputProps = _getOverrides10[1];

  var afterFileDrop = !!(props.progressAmount || props.progressMessage || props.errorMessage);
  return _react.default.createElement(_reactDropzone.default, _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
    }));
    var getRootPropsArgs = {
      refKey: '$ref',
      onClick: props.disableClick ? function (evt) {
        return evt.preventDefault();
      } : undefined
    };
    return _react.default.createElement(_index.LocaleContext.Consumer, null, function (locale) {
      return _react.default.createElement(Root, _extends({
        "data-baseweb": "file-uploader"
      }, prefixedStyledProps, rootProps), _react.default.createElement(FileDragAndDrop, _extends({}, getRootProps(getRootPropsArgs), prefixedStyledProps, fileDragAndDropProps), !afterFileDrop && _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(ContentMessage, _extends({}, prefixedStyledProps, contentMessageProps), locale.fileuploader.dropFilesToUpload), _react.default.createElement(ContentMessage, _extends({}, prefixedStyledProps, contentMessageProps), locale.fileuploader.or), _react.default.createElement(_index3.Button, _extends({
        "aria-controls": "fileupload",
        disabled: props.disabled,
        kind: _index3.KIND.minimal,
        onClick: open,
        overrides: {
          BaseButton: {
            style: {
              outline: null,
              fontWeight: 'normal'
            }
          }
        },
        role: "button"
      }, prefixedStyledProps), locale.fileuploader.browseFiles)), afterFileDrop && _react.default.createElement(_react.default.Fragment, null, typeof props.progressAmount === 'number' ? _react.default.createElement(_index4.ProgressBar, {
        value: props.progressAmount,
        overrides: {
          BarProgress: {
            style: function style(_ref) {
              var $theme = _ref.$theme;
              return {
                backgroundColor: props.errorMessage ? $theme.colors.negative : $theme.colors.primary
              };
            }
          }
        }
      }) : _react.default.createElement(_index2.Block, {
        marginBottom: "scale300"
      }, _react.default.createElement(_index5.Spinner, {
        size: 40
      })), (props.errorMessage || props.progressMessage) && props.errorMessage ? _react.default.createElement(ErrorMessage, _extends({}, prefixedStyledProps, errorMessageProps), props.errorMessage) : _react.default.createElement(ContentMessage, _extends({}, prefixedStyledProps, contentMessageProps), props.progressMessage), props.errorMessage ? _react.default.createElement(_index3.Button, {
        kind: _index3.KIND.minimal,
        onClick: function onClick() {
          props.onRetry && props.onRetry();
        },
        overrides: {
          BaseButton: {
            style: {
              outline: null,
              fontWeight: 'normal'
            }
          }
        }
      }, locale.fileuploader.retry) : _react.default.createElement(_index3.Button, {
        kind: _index3.KIND.minimal,
        onClick: function onClick() {
          props.onCancel && props.onCancel();
        },
        overrides: {
          BaseButton: {
            style: {
              outline: null,
              fontWeight: 'normal'
            }
          }
        }
      }, locale.fileuploader.cancel))), _react.default.createElement(HiddenInput, _extends({}, getInputProps({
        refKey: '$ref'
      }), prefixedStyledProps, hiddenInputProps)));
    });
  });
}

FileUploader.defaultProps = {
  disableClick: true,
  overrides: {}
};
var _default = FileUploader;
exports.default = _default;