"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

var React = _interopRequireWildcard(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 _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } 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.ContentSeparator, _styledComponents.StyledContentSeparator),
      _getOverrides8 = _slicedToArray(_getOverrides7, 2),
      ContentSeparator = _getOverrides8[0],
      contentSeparatorProps = _getOverrides8[1];

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

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

  var _getOverrides13 = (0, _overrides.getOverrides)(overrides.ButtonComponent, _index3.Button),
      _getOverrides14 = _slicedToArray(_getOverrides13, 2),
      ButtonComponent = _getOverrides14[0],
      buttonProps = _getOverrides14[1];

  var _getOverrides15 = (0, _overrides.getOverrides)(overrides.Spinner, _index5.StyledSpinnerNext),
      _getOverrides16 = _slicedToArray(_getOverrides15, 2),
      SpinnerComponent = _getOverrides16[0],
      spinnerProps = _getOverrides16[1];

  var afterFileDrop = !!(props.progressAmount || props.progressMessage || props.errorMessage);
  return React.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 = _objectSpread({}, props.disableClick ? {
      onClick: function onClick(evt) {
        return evt.preventDefault();
      }
    } : {}, {
      tabIndex: '-1'
    });

    return React.createElement(_index.LocaleContext.Consumer, null, function (locale) {
      return React.createElement(Root, _extends({
        "data-baseweb": "file-uploader"
      }, prefixedStyledProps, rootProps), React.createElement(FileDragAndDrop, _extends({}, getRootProps(getRootPropsArgs), prefixedStyledProps, fileDragAndDropProps), !afterFileDrop && React.createElement(React.Fragment, null, React.createElement(ContentMessage, _extends({}, prefixedStyledProps, contentMessageProps), locale.fileuploader.dropFilesToUpload), React.createElement(ContentSeparator, _extends({}, prefixedStyledProps, contentSeparatorProps), locale.fileuploader.or), React.createElement(ButtonComponent, _extends({
        "aria-controls": "fileupload",
        disabled: props.disabled,
        kind: _index3.KIND.minimal,
        onClick: open,
        overrides: {
          BaseButton: {
            style: {
              fontWeight: 'normal'
            }
          }
        },
        role: "button"
      }, prefixedStyledProps, buttonProps), locale.fileuploader.browseFiles)), afterFileDrop && React.createElement(React.Fragment, null, typeof props.progressAmount === 'number' ? React.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
              };
            }
          }
        }
      }) : props.errorMessage ? null : React.createElement(_index2.Block, {
        marginBottom: "scale300"
      }, React.createElement(SpinnerComponent, _extends({
        $size: _index5.SIZE.medium
      }, spinnerProps))), (props.errorMessage || props.progressMessage) && props.errorMessage ? React.createElement(ErrorMessage, _extends({}, prefixedStyledProps, errorMessageProps), props.errorMessage) : React.createElement(ContentMessage, _extends({}, prefixedStyledProps, contentMessageProps), props.progressMessage), props.errorMessage ? React.createElement(ButtonComponent, {
        kind: _index3.KIND.minimal,
        onClick: function onClick() {
          props.onRetry && props.onRetry();
        },
        "aria-invalid": Boolean(props.errorMessage),
        "aria-describedby": props['aria-describedby'],
        "aria-errormessage": props.errorMessage,
        overrides: {
          BaseButton: {
            style: {
              outline: null,
              fontWeight: 'normal'
            }
          }
        }
      }, locale.fileuploader.retry) : React.createElement(ButtonComponent, {
        kind: _index3.KIND.minimal,
        onClick: function onClick() {
          props.onCancel && props.onCancel();
        },
        "aria-describedby": props['aria-describedby'],
        overrides: {
          BaseButton: {
            style: {
              outline: null,
              fontWeight: 'normal'
            }
          }
        }
      }, locale.fileuploader.cancel))), 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: {}
};
var _default = FileUploader;
exports.default = _default;