var _excluded = ["value", "disabled", "multiple", "noClick", "noKeyboard", "onDragEnter", "onDragOver", "onDragLeave", "onDropRejected", "maxFileSize", "minFileSize", "maxFileNumber", "onDrop", "onDropAccepted"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
import { useDropzone as useDropzoneBase } from 'react-dropzone';
import { useI18nContext } from '../_hooks/I18n';
import { partition } from '../_utils/partition';
import { MultipleErrors } from './Dropzone';
import { StyledErrorBannerDetails } from './Dropzone.styles';
import { dropErrors, fileRejectionReason } from './Dropzone.types';
import { getFileDetail, isZeroSize } from './Dropzone.utils';
var oneDropAtATimeRejectionReason = 'one-drop-at-a-time';
var dropErrorsByReason = _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, fileRejectionReason.maxFiles, dropErrors.fileAmount), fileRejectionReason.maxFileSize, dropErrors.maxFileSize), fileRejectionReason.minFileSize, dropErrors.minFileSize), fileRejectionReason.fileType, dropErrors.fileType);
var getSizeInMB = function getSizeInMB(maxSize) {
  return maxSize / 10e5;
};

/**
 * WARNING:
 * Use ACCEPT prop carefully, because mime type determination is not reliable across platforms.
 * Details: https://github.com/react-dropzone/react-dropzone/tree/master/examples/accept#browser-limitations
 */
export var useDropzone = function useDropzone() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var _ref$value = _ref.value,
    value = _ref$value === void 0 ? [] : _ref$value,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    _ref$multiple = _ref.multiple,
    multiple = _ref$multiple === void 0 ? false : _ref$multiple,
    _ref$noClick = _ref.noClick,
    noClick = _ref$noClick === void 0 ? true : _ref$noClick,
    _ref$noKeyboard = _ref.noKeyboard,
    noKeyboard = _ref$noKeyboard === void 0 ? true : _ref$noKeyboard,
    onDragEnter = _ref.onDragEnter,
    onDragOver = _ref.onDragOver,
    onDragLeave = _ref.onDragLeave,
    onDropRejected = _ref.onDropRejected,
    maxSize = _ref.maxFileSize,
    minSize = _ref.minFileSize,
    _ref$maxFileNumber = _ref.maxFileNumber,
    maxFileNumber = _ref$maxFileNumber === void 0 ? Infinity : _ref$maxFileNumber,
    _onDrop = _ref.onDrop,
    onDropAccepted = _ref.onDropAccepted,
    props = _objectWithoutProperties(_ref, _excluded);
  var I18n = useI18nContext();
  var _React$useState = React.useState([]),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    acceptedFiles = _React$useState2[0],
    setAcceptedFiles = _React$useState2[1];
  var _React$useState3 = React.useState([]),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    fileRejections = _React$useState4[0],
    setFileRejections = _React$useState4[1];
  var maxFilesRemaining = maxFileNumber - value.length;
  var defaultDropError = {
    type: dropErrors.reset,
    title: '',
    message: ''
  };
  var dropErrorReducer = function dropErrorReducer(state, action) {
    var rejectedFiles = /*#__PURE__*/React.createElement(StyledErrorBannerDetails, null, fileRejections.map(function (_ref2) {
      var file = _ref2.file;
      return /*#__PURE__*/React.createElement("li", {
        key: file.name
      }, getFileDetail(file));
    }));
    var title = I18n.t('core.dropzone.errorTitle');
    switch (action) {
      case dropErrors.oneFileAtATime:
        return {
          type: dropErrors.oneFileAtATime,
          title: '',
          message: I18n.t('core.dropzone.oneFileAtATimeError')
        };
      case dropErrors.fileAmount:
        return {
          type: dropErrors.fileAmount,
          title: title,
          message: I18n.t('core.dropzone.standaloneMaxFileNumberError', {
            maxFiles: maxFileNumber
          }),
          body: rejectedFiles
        };
      case dropErrors.fileType:
        return {
          type: dropErrors.fileType,
          title: title,
          message: I18n.t('core.dropzone.standaloneUnsupportedFileTypeError'),
          body: rejectedFiles
        };
      case dropErrors.maxFileSize:
        return {
          type: dropErrors.maxFileSize,
          title: title,
          message: I18n.t('core.dropzone.standaloneMaxFileSizeError', {
            sizeInMegabytes: getSizeInMB(maxSize)
          }),
          body: rejectedFiles
        };
      case dropErrors.minFileSize:
        var message = isZeroSize(minSize) ? I18n.t('core.dropzone.standaloneZeroFileSizeError') : I18n.t('core.dropzone.standaloneMinFileSizeError', {
          sizeInMegabytes: getSizeInMB(minSize)
        });
        return {
          type: dropErrors.minFileSize,
          title: title,
          message: message,
          body: rejectedFiles
        };
      case dropErrors.multipleErrors:
        return {
          type: dropErrors.multipleErrors,
          title: title,
          message: I18n.t('core.dropzone.multipleErrorsMessage'),
          body: /*#__PURE__*/React.createElement(MultipleErrors, {
            fileRejections: fileRejections,
            maxFiles: maxFileNumber,
            maxSize: maxSize,
            minSize: minSize
          })
        };
      case dropErrors.reset:
        return _objectSpread({}, defaultDropError);
      default:
        return state;
    }
  };
  var withOneDropAtATimeError = function withOneDropAtATimeError(file) {
    return {
      file: file,
      errors: [{
        message: I18n.t('core.dropzone.oneDropAtATimeError'),
        code: oneDropAtATimeRejectionReason
      }]
    };
  };
  var _React$useReducer = React.useReducer(dropErrorReducer, _objectSpread({}, defaultDropError)),
    _React$useReducer2 = _slicedToArray(_React$useReducer, 2),
    dropError = _React$useReducer2[0],
    dispatchDropError = _React$useReducer2[1];
  var onDrop = React.useCallback(function (acceptedFiles, rejectedFiles, event) {
    dispatchDropError(dropErrors.reset);
    var droppedFiles = [].concat(_toConsumableArray(acceptedFiles), _toConsumableArray(rejectedFiles.map(function (rej) {
      return rej.file;
    })));
    if (!multiple && droppedFiles.length > 1) {
      dispatchDropError(dropErrors.oneFileAtATime);
      onDropRejected === null || onDropRejected === void 0 ? void 0 : onDropRejected(droppedFiles.map(withOneDropAtATimeError), event);
      return;
    }
    var allFilesWithRejections = [].concat(_toConsumableArray(acceptedFiles.map(function (file) {
      return {
        file: file,
        errors: []
      };
    })), _toConsumableArray(rejectedFiles)).map(function (_ref3, i) {
      var file = _ref3.file,
        errors = _ref3.errors;
      return {
        file: file,
        errors: i < maxFilesRemaining ? errors : [].concat(_toConsumableArray(errors), [{
          message: '',
          code: fileRejectionReason.maxFiles
        }])
      };
    });
    var _partition = partition(function (_ref4) {
        var errors = _ref4.errors;
        return errors.length === 0;
      }, allFilesWithRejections),
      _partition2 = _slicedToArray(_partition, 2),
      actualAcceptedFiles_ = _partition2[0],
      actualRejectedFiles = _partition2[1];
    var actualAcceptedFiles = actualAcceptedFiles_.map(function (_ref5) {
      var file = _ref5.file;
      return file;
    });
    _onDrop === null || _onDrop === void 0 ? void 0 : _onDrop(actualAcceptedFiles, actualRejectedFiles, event);
    setAcceptedFiles(actualAcceptedFiles);
    setFileRejections(actualRejectedFiles);
    if (actualAcceptedFiles.length) {
      onDropAccepted === null || onDropAccepted === void 0 ? void 0 : onDropAccepted(actualAcceptedFiles, event);
    }
    if (actualRejectedFiles.length) {
      onDropRejected === null || onDropRejected === void 0 ? void 0 : onDropRejected(actualRejectedFiles, event);
    }
    var errorCodes = actualRejectedFiles.flatMap(function (rejection) {
      return rejection.errors;
    }).map(function (error) {
      return error.code;
    });
    var hasMultipleErrors = new Set(_toConsumableArray(errorCodes)).size > 1;
    if (hasMultipleErrors) {
      dispatchDropError(dropErrors.multipleErrors);
    } else {
      dispatchDropError(dropErrorsByReason[errorCodes[0]]);
    }
  }, [_onDrop, maxFilesRemaining, onDropRejected, onDropAccepted, multiple]);
  var _useDropzoneBase = useDropzoneBase(_objectSpread(_objectSpread({}, props), {}, {
      disabled: disabled,
      minSize: minSize,
      maxSize: maxSize,
      multiple: multiple,
      onDragEnter: onDragEnter,
      onDragLeave: onDragLeave,
      onDragOver: onDragOver,
      onDrop: onDrop,
      noClick: noClick,
      noKeyboard: noKeyboard
    })),
    getRootProps = _useDropzoneBase.getRootProps,
    getInputProps = _useDropzoneBase.getInputProps,
    isDragActive = _useDropzoneBase.isDragActive,
    isFileDialogActive = _useDropzoneBase.isFileDialogActive,
    isDragReject = _useDropzoneBase.isDragReject,
    isFocused = _useDropzoneBase.isFocused,
    isDragAccept = _useDropzoneBase.isDragAccept,
    draggedFiles = _useDropzoneBase.draggedFiles,
    open = _useDropzoneBase.open,
    rootRef = _useDropzoneBase.rootRef,
    inputRef = _useDropzoneBase.inputRef;
  var dragError = React.useMemo(function () {
    var getFileTypeError = function getFileTypeError() {
      return isDragReject ? I18n.t('core.dropzone.incorrectFileTypeMessage', {
        count: draggedFiles.length
      }) : '';
    };
    var getFileAmountError = function getFileAmountError() {
      var fileLimit = multiple ? maxFilesRemaining : 1;
      var amountError = multiple ? I18n.t('core.dropzone.invalidAmountAttachments', {
        count: maxFileNumber
      }) : I18n.t('core.dropzone.incorrectFileNumber');
      return draggedFiles.length > fileLimit ? amountError : '';
    };
    return getFileAmountError() || getFileTypeError();
  }, [I18n, draggedFiles.length, maxFilesRemaining, isDragReject, maxFileNumber, multiple]);
  return {
    getRootProps: getRootProps,
    getInputProps: getInputProps,
    isDragActive: isDragActive,
    isFileDialogActive: isFileDialogActive,
    isDragReject: isDragReject,
    isFocused: isFocused,
    isDragAccept: isDragAccept,
    draggedFiles: draggedFiles,
    acceptedFiles: acceptedFiles,
    fileRejections: fileRejections,
    open: open,
    rootRef: rootRef,
    inputRef: inputRef,
    multiple: multiple,
    dragError: dragError,
    dropError: dropError,
    dispatchDropError: dispatchDropError,
    disabled: disabled,
    value: value
  };
};
//# sourceMappingURL=Dropzone.hooks.js.map