function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _dropErrorsByReason;
var _excluded = ["error", "onDismiss", "qa"];
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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; }
import { Ban, Image } from '@procore/core-icons/dist';
import { equals, omit } from 'ramda';
import React from 'react';
import styled from 'styled-components';
import { Banner } from '../Banner/Banner';
import { Button } from '../Button/Button';
import { Tooltip } from '../Tooltip/Tooltip';
import { Typography } from '../Typography/Typography';
import { useI18nContext } from '../_hooks/I18n';
import { useResizeObserver } from '../_hooks/ResizeObserver';
import { useFocusWithin } from '../_hooks/useFocusWithin';
import { StyledDropzoneCaption, StyledDropzoneContainer, StyledDropzoneFocusDetector, StyledDropzoneIcon, StyledDropzoneMessage, StyledDropzoneWrapper, StyledErrorBanner, StyledErrorBannerDetails, StyledUploadButton } from './Dropzone.styles';
import { dropErrors, fileRejectionReason } from './Dropzone.types';
var extendedViewHeightBreakpoint = 172;
var DropzoneErrorBannerBase = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var error = _ref.error,
    onDismiss = _ref.onDismiss,
    qa = _ref.qa,
    props = _objectWithoutProperties(_ref, _excluded);
  var I18n = useI18nContext();
  var _React$useState = React.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    isExpanded = _React$useState2[0],
    setExpanded = _React$useState2[1];
  React.useEffect(function () {
    setExpanded(false);
  }, [error.title, error.body, error.message]);
  if (error.type === dropErrors.reset) {
    return null;
  }
  return /*#__PURE__*/React.createElement(StyledErrorBanner, _extends({}, props, {
    ref: ref
  }), /*#__PURE__*/React.createElement(Banner.Content, null, /*#__PURE__*/React.createElement(Banner.Title, null, error.title), /*#__PURE__*/React.createElement(Banner.Body, null, error.message)), error.body ? /*#__PURE__*/React.createElement(Banner.Action, null, /*#__PURE__*/React.createElement(Button, {
    "data-qa": qa === null || qa === void 0 ? void 0 : qa.showErrorDetails,
    onClick: function onClick() {
      return setExpanded(function (prev) {
        return !prev;
      });
    }
  }, isExpanded ? I18n.t('core.dropzone.hideDetails') : I18n.t('core.dropzone.showDetails'))) : null, /*#__PURE__*/React.createElement(Banner.Dismiss, {
    onClick: onDismiss,
    "data-qa": qa === null || qa === void 0 ? void 0 : qa.hideError
  }), error.body ? /*#__PURE__*/React.createElement(Banner.ExpandableContent, {
    expanded: isExpanded
  }, error.body) : null);
});
export function MultipleErrors(props) {
  var _props$fileRejections, _messages;
  var I18n = useI18nContext();
  var filesByErrorType = props.fileRejections.reduce(function (acc, rejection) {
    rejection.errors.forEach(function (error) {
      var dropErrorType = dropErrorsByReason[error.code];
      acc[dropErrorType].push(rejection.file);
    });
    return acc;
  }, (_props$fileRejections = {}, _defineProperty(_props$fileRejections, dropErrors.fileAmount, []), _defineProperty(_props$fileRejections, dropErrors.minFileSize, []), _defineProperty(_props$fileRejections, dropErrors.maxFileSize, []), _defineProperty(_props$fileRejections, dropErrors.fileType, []), _defineProperty(_props$fileRejections, dropErrors.oneFileAtATime, []), _defineProperty(_props$fileRejections, dropErrors.reset, []), _defineProperty(_props$fileRejections, dropErrors.multipleErrors, []), _props$fileRejections));
  var messages = (_messages = {}, _defineProperty(_messages, dropErrors.fileAmount, I18n.t('core.dropzone.maxFileNumberErrorGroup', {
    count: props.maxFiles
  })), _defineProperty(_messages, dropErrors.minFileSize, I18n.t('core.dropzone.minFileSizeErrorGroup', {
    sizeInMegabytes: getSizeInMB(props.minSize)
  })), _defineProperty(_messages, dropErrors.maxFileSize, I18n.t('core.dropzone.maxFileSizeErrorGroup', {
    sizeInMegabytes: getSizeInMB(props.maxSize)
  })), _defineProperty(_messages, dropErrors.fileType, I18n.t('core.dropzone.wrongFileTypeErrorGroup')), _defineProperty(_messages, dropErrors.reset, ''), _defineProperty(_messages, dropErrors.multipleErrors, ''), _defineProperty(_messages, dropErrors.oneFileAtATime, ''), _messages);
  return /*#__PURE__*/React.createElement("div", null, [dropErrors.fileAmount, dropErrors.fileType, dropErrors.maxFileSize, dropErrors.minFileSize].map(function (errorType) {
    var files = filesByErrorType[errorType];
    return files.length ? /*#__PURE__*/React.createElement("div", {
      key: errorType
    }, /*#__PURE__*/React.createElement(Typography, {
      weight: "bold"
    }, messages[errorType]), /*#__PURE__*/React.createElement(StyledErrorBannerDetails, null, filesByErrorType[errorType].map(function (_ref2) {
      var name = _ref2.name;
      return /*#__PURE__*/React.createElement("li", {
        key: name
      }, name);
    }))) : null;
  }));
}

// make error banner accessible as a styled-components selector
export var DropzoneErrorBanner = /*#__PURE__*/styled(DropzoneErrorBannerBase).withConfig({
  displayName: "DropzoneErrorBanner",
  componentId: "core-12_10_0__sc-l4fojb-0"
})([""]);
function DropzoneContainer(_ref3) {
  var active = _ref3.active,
    disabled = _ref3.disabled,
    getRootProps = _ref3.getRootProps,
    rootProps = _ref3.rootProps,
    children = _ref3.children;
  return /*#__PURE__*/React.createElement(StyledDropzoneContainer, _extends({}, getRootProps(rootProps), {
    active: active,
    disabled: disabled
  }), children);
}
function DropzoneContent(_ref4) {
  var disabled = _ref4.disabled,
    isIconVisible = _ref4.isIconVisible,
    contentMessage = _ref4.contentMessage;
  return /*#__PURE__*/React.createElement(StyledDropzoneMessage, null, isIconVisible && /*#__PURE__*/React.createElement(StyledDropzoneIcon, {
    disabled: disabled
  }, disabled ? /*#__PURE__*/React.createElement(Ban, null) : /*#__PURE__*/React.createElement(Image, null)), contentMessage);
}
export function DropzoneDefaultMessage(_ref5) {
  var open = _ref5.open,
    disabled = _ref5.disabled,
    multiple = _ref5.multiple,
    errorMessage = _ref5.errorMessage;
  var I18n = useI18nContext();
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(StyledUploadButton, {
    variant: "secondary",
    onClick: open,
    disabled: disabled
  }, I18n.t('core.dropzone.uploadFiles', {
    count: multiple ? Infinity : 1
  })), /*#__PURE__*/React.createElement(StyledDropzoneCaption, {
    error: !!errorMessage
  }, errorMessage || I18n.t('core.dropzone.dragAndDrop')));
}
var dropErrorsByReason = (_dropErrorsByReason = {}, _defineProperty(_dropErrorsByReason, fileRejectionReason.maxFiles, dropErrors.fileAmount), _defineProperty(_dropErrorsByReason, fileRejectionReason.maxFileSize, dropErrors.maxFileSize), _defineProperty(_dropErrorsByReason, fileRejectionReason.minFileSize, dropErrors.minFileSize), _defineProperty(_dropErrorsByReason, fileRejectionReason.fileType, dropErrors.fileType), _dropErrorsByReason);
var getSizeInMB = function getSizeInMB(maxSize) {
  return maxSize / 10e5;
};
var Dropzone_ = /*#__PURE__*/React.forwardRef(function Dropzone(_ref6, ref) {
  var forceIconVisibility = _ref6.isIconVisible,
    contentRenderer = _ref6.contentRenderer,
    disabled = _ref6.disabled,
    tooltip = _ref6.tooltip,
    rootRef = _ref6.rootRef,
    inputRef = _ref6.inputRef,
    dragError = _ref6.dragError,
    dropError = _ref6.dropError,
    dispatchDropError = _ref6.dispatchDropError,
    getRootProps = _ref6.getRootProps,
    getInputProps = _ref6.getInputProps,
    isDragActive = _ref6.isDragActive,
    multiple = _ref6.multiple,
    fileRejections = _ref6.fileRejections,
    open = _ref6.open,
    _ref6$rootProps = _ref6.rootProps,
    rootProps = _ref6$rootProps === void 0 ? {} : _ref6$rootProps,
    _ref6$inputProps = _ref6.inputProps,
    inputProps = _ref6$inputProps === void 0 ? {} : _ref6$inputProps,
    className = _ref6.className,
    qa = _ref6.qa;
  var _React$useState3 = React.useState(false),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    isIconVisible = _React$useState4[0],
    setIsIconVisible = _React$useState4[1];
  var _useFocusWithin = useFocusWithin(),
    isFocusWithin = _useFocusWithin.isFocusWithin,
    containerProps = _useFocusWithin.containerProps;
  var toggleIconVisibility = React.useCallback(function (entries) {
    var _ref7 = entries[0].target,
      height = _ref7.offsetHeight;
    setIsIconVisible(height >= extendedViewHeightBreakpoint);
  }, [setIsIconVisible]);
  var setResizeObserverTarget = useResizeObserver(toggleIconVisibility);
  React.useEffect(function () {
    setResizeObserverTarget(rootRef.current);
  }, [rootRef, setResizeObserverTarget]);
  React.useImperativeHandle(ref, function () {
    return {
      rootRef: rootRef,
      inputRef: inputRef,
      open: open
    };
  });
  var isDropzoneDisabled = disabled || Boolean(dragError);
  var contentMessage = React.useMemo(function () {
    return contentRenderer ? contentRenderer({
      open: open,
      disabled: isDropzoneDisabled,
      errorMessage: dragError
    }) : /*#__PURE__*/React.createElement(DropzoneDefaultMessage, {
      open: open,
      disabled: isDropzoneDisabled,
      errorMessage: dragError,
      multiple: multiple
    });
  }, [contentRenderer, open, isDropzoneDisabled, dragError, multiple]);
  var validRootProps = React.useMemo(function () {
    return omit(['disabled'], rootProps);
  }, [rootProps]);
  var validInputProps = React.useMemo(function () {
    return omit(['accept', 'multiple', 'type'], inputProps);
  }, [inputProps]);
  return /*#__PURE__*/React.createElement(StyledDropzoneWrapper, {
    direction: "column",
    alignItems: "stretch",
    className: className
  }, /*#__PURE__*/React.createElement(Tooltip, {
    trigger: Boolean(tooltip) ? 'hover' : 'none',
    overlay: tooltip || ''
  }, /*#__PURE__*/React.createElement(StyledDropzoneFocusDetector, containerProps, /*#__PURE__*/React.createElement(DropzoneContainer, {
    rootProps: validRootProps,
    active: isDragActive || isFocusWithin,
    disabled: isDropzoneDisabled,
    getRootProps: getRootProps
  }, /*#__PURE__*/React.createElement("input", _extends({
    type: "file"
  }, getInputProps(validInputProps))), /*#__PURE__*/React.createElement(DropzoneContent, {
    disabled: isDropzoneDisabled,
    isIconVisible: typeof forceIconVisibility === 'undefined' ? isIconVisible : forceIconVisibility,
    contentMessage: contentMessage
  })))), /*#__PURE__*/React.createElement(DropzoneErrorBanner, {
    qa: qa,
    error: dropError,
    fileRejections: fileRejections,
    onDismiss: function onDismiss() {
      return dispatchDropError(dropErrors.reset);
    }
  }));
});
DropzoneContainer.displayName = 'DropzoneContainer';
DropzoneContent.displayName = 'DropzoneContent';
Dropzone_.displayName = 'Dropzone';

/**

 Dropzones allow users to quickly upload files from their computers, as well
 as optionally launch the file select component.

 @since 10.19.0

 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-dropzone--demo)

 @see [Design Guidelines](https://design.procore.com/dropzone)

*/
export var Dropzone = /*#__PURE__*/React.memo(Dropzone_, equals);
//# sourceMappingURL=Dropzone.js.map