"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("../button/index.js");

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

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

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

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

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

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 = (0, _overrides.getOverrides)(Override, Source),
        _getOverrides2 = _slicedToArray(_getOverrides, 2),
        Overridden = _getOverrides2[0],
        overrideProps = _getOverrides2[1];

    return _react.default.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, _styledComponents.StyledRoot),
    FileDragAndDrop: makeOverride(overrides.FileDragAndDrop, _styledComponents.StyledFileDragAndDrop),
    ContentMessage: makeOverride(overrides.ContentMessage, _styledComponents.StyledContentMessage),
    ErrorMessage: makeOverride(overrides.ErrorMessage, _styledComponents.StyledErrorMessage),
    HiddenInput: makeOverride(overrides.HiddenInput, _styledComponents.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.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
    }));
    return _react.default.createElement(Root, prefixedStyledProps, _react.default.createElement(FileDragAndDrop, _extends({}, getRootProps({
      refKey: '$ref'
    }), prefixedStyledProps), !afterFileDrop && _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(ContentMessage, prefixedStyledProps, "Drop files here to upload"), _react.default.createElement(ContentMessage, prefixedStyledProps, "or"), _react.default.createElement(_index.Button, _extends({
      "aria-controls": "fileupload",
      disabled: props.disabled,
      kind: _index.KIND.minimal,
      onClick: open,
      overrides: {
        BaseButton: {
          style: {
            outline: null,
            fontWeight: 'normal'
          }
        }
      },
      role: "button"
    }, prefixedStyledProps), "Browse files")), afterFileDrop && _react.default.createElement(_react.default.Fragment, null, !!props.progressAmount && _react.default.createElement(_index2.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.default.createElement(ErrorMessage, null, props.errorMessage) : _react.default.createElement(ContentMessage, null, props.progressMessage), props.errorMessage ? _react.default.createElement(_index.Button, {
      kind: _index.KIND.minimal,
      onClick: function onClick() {
        props.onRetry && props.onRetry();
      },
      overrides: {
        BaseButton: {
          style: {
            outline: null,
            fontWeight: 'normal'
          }
        }
      }
    }, "Retry Upload") : _react.default.createElement(_index.Button, {
      kind: _index.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.default.createElement(HiddenInput, _extends({}, getInputProps({
      refKey: '$ref'
    }), prefixedStyledProps)));
  });
}

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