"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _UploadIcon = _interopRequireDefault(require("mdi-react/UploadIcon"));

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

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

var _Box = require("../Box");

var _PseudoBox = require("../PseudoBox");

var _Text = require("../Text");

var _styles = _interopRequireDefault(require("./styles"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }

var __jsx = _react["default"].createElement;

var Dropzone = function Dropzone(props) {
  var disabled = props.disabled,
      disabledMessage = props.disabledMessage,
      _props$uploadMessage = props.uploadMessage,
      uploadMessage = _props$uploadMessage === void 0 ? 'Drag or click to upload images' : _props$uploadMessage,
      _props$accept = props.accept,
      accept = _props$accept === void 0 ? [] : _props$accept,
      _props$showIcon = props.showIcon,
      showIcon = _props$showIcon === void 0 ? true : _props$showIcon,
      rest = (0, _objectWithoutProperties2["default"])(props, ["disabled", "disabledMessage", "uploadMessage", "accept", "showIcon"]);
  var onDrop = (0, _react.useCallback)(function (acceptedFiles) {
    if (acceptedFiles && acceptedFiles.length > 0) {
      acceptedFiles.forEach(function (file) {
        var _window;

        var fileUrl = (_window = window) === null || _window === void 0 ? void 0 : _window.URL.createObjectURL(file);
        props.dropEvent(file, fileUrl);
      });
    }
  }, []);

  var _useDropzone = (0, _reactDropzone.useDropzone)({
    onDrop: onDrop,
    accept: accept && accept.toString(),
    disabled: disabled
  }),
      getRootProps = _useDropzone.getRootProps,
      getInputProps = _useDropzone.getInputProps,
      isDragActive = _useDropzone.isDragActive,
      isDragAccept = _useDropzone.isDragAccept,
      isDragReject = _useDropzone.isDragReject;

  var dropzoneStyleProps = (0, _styles["default"])({
    state: isDragAccept && 'accept' || isDragReject && 'reject' || isDragActive && 'active',
    disabled: disabled
  });
  var content = uploadMessage;

  if (disabled && disabledMessage) {
    content = disabledMessage;
  } else if (isDragActive) {
    content = 'Drop the file here ...';
  }

  return (// container
    __jsx(_PseudoBox.PseudoBox, (0, _extends2["default"])({}, getRootProps(), {
      disabled: disabled
    }, dropzoneStyleProps, rest), __jsx("input", getInputProps()), showIcon && __jsx(_Box.Box, {
      mb: "spacing-xs"
    }, __jsx(_UploadIcon["default"], null)), __jsx(_Text.Text, {
      color: "faintText"
    }, content))
  );
};

exports.Dropzone = Dropzone;
//# sourceMappingURL=Dropzone.js.map