'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var React = require('react');
var classnames = require('classnames');
var neetoCist = require('@bigbinary/neeto-cist');
var Button = require('@bigbinary/neetoui/Button');
var Toastr = require('@bigbinary/neetoui/Toastr');
var ramda = require('ramda');
var reactI18next = require('react-i18next');
var reactFileIcon_esm = require('./chunk-D3Unocnm.cjs.js');
var FileIcon = require('./chunk-yX0BlnxV.cjs.js');
var injectCss = require('./chunk-B6qYtOJe.cjs.js');
var useOnClickOutside = require('@bigbinary/neeto-commons-frontend/react-utils/useOnClickOutside');
var utils = require('@bigbinary/neeto-commons-frontend/utils');
var MenuVertical = require('@bigbinary/neeto-icons/MenuVertical');
var Close = require('@bigbinary/neeto-icons/Close');
var Check = require('@bigbinary/neeto-icons/Check');
var Dropdown = require('@bigbinary/neetoui/Dropdown');
var Input = require('@bigbinary/neetoui/Input');
var Spinner = require('@bigbinary/neetoui/Spinner');
var Tooltip = require('@bigbinary/neetoui/Tooltip');
var Typography = require('@bigbinary/neetoui/Typography');
var Alert = require('@bigbinary/neetoui/Alert');
var i18n = require('i18next');
var initializers = require('@bigbinary/neeto-commons-frontend/initializers');
var constants = require('./chunk-2HrSPdAV.cjs.js');
var jsxRuntime = require('react/jsx-runtime');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefault(_toConsumableArray);
var _asyncToGenerator__default = /*#__PURE__*/_interopDefault(_asyncToGenerator);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);
var Button__default = /*#__PURE__*/_interopDefault(Button);
var Toastr__default = /*#__PURE__*/_interopDefault(Toastr);
var useOnClickOutside__default = /*#__PURE__*/_interopDefault(useOnClickOutside);
var MenuVertical__default = /*#__PURE__*/_interopDefault(MenuVertical);
var Close__default = /*#__PURE__*/_interopDefault(Close);
var Check__default = /*#__PURE__*/_interopDefault(Check);
var Dropdown__default = /*#__PURE__*/_interopDefault(Dropdown);
var Input__default = /*#__PURE__*/_interopDefault(Input);
var Spinner__default = /*#__PURE__*/_interopDefault(Spinner);
var Tooltip__default = /*#__PURE__*/_interopDefault(Tooltip);
var Typography__default = /*#__PURE__*/_interopDefault(Typography);
var Alert__default = /*#__PURE__*/_interopDefault(Alert);

var css = ".ne-attachments{display:flex;flex-direction:column;gap:8px}.ne-attachments__items{display:flex;flex-wrap:wrap;gap:8px}.ne-attachments__preview{align-items:center;background-color:rgb(var(--neeto-ui-white));border:1px solid rgb(var(--neeto-ui-gray-200));border-radius:var(--neeto-ui-rounded-sm);display:flex;justify-content:space-between;padding-block:4px;padding-inline:8px 2px;width:256px}.ne-attachments__preview-wrapper{display:flex;gap:8px;width:88%}.ne-attachments__preview-wrapper__icon{height:auto;width:15px}.ne-attachments__preview-wrapper p{display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:92%}.ne-attachments__preview-wrapper:hover{cursor:pointer}.ne-attachments__preview__progress{display:flex;gap:4px;justify-content:space-between;opacity:.5;width:76%}.ne-attachments__preview__progress p{display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:85%}.ne-attachments__preview__progress p:nth-child(2){flex-shrink:0;width:-moz-fit-content;width:fit-content}.ne-attachments__preview__progress-icon{height:auto;width:15px}.ne-attachments__preview:hover{box-shadow:0 0 0 1px rgb(var(--neeto-ui-gray-400))}.ne-attachments input[type=file]{display:none}.ne-attachments-preview{display:flex;flex-direction:column;gap:8px}.ne-attachments-preview .neeto-ui-modal__close{inset-inline-end:16px!important;top:9px!important}.ne-attachments-preview__header{border-bottom:1px solid rgb(var(--neeto-ui-gray-200));display:flex;flex-direction:row;gap:8px;margin-bottom:8px;min-height:58px;padding:16px!important}.ne-attachments-preview__header__fileinfo{align-items:center;border-inline-end:1px solid rgb(var(--neeto-ui-gray-200));display:flex;gap:8px;padding-inline-end:6px}.ne-attachments-preview__body{align-items:center;display:flex;flex-grow:1;justify-content:center;min-height:10px;padding-bottom:8px!important;position:relative;width:100vw}.ne-attachments-preview__body-download{color:rgb(var(--neeto-ui-accent-800));cursor:pointer}.ne-attachments-preview__body-docviewer{height:100%;width:100%}.ne-attachments-preview__body img{max-height:100%;max-width:100%}.ne-attachments-preview__body video{height:100%;width:100%}.ne-attachments-preview__body iframe{height:100%;padding:0 8px;width:100%}.ne-attachments-preview__body-left,.ne-attachments-preview__body-right{position:absolute!important;top:50%;transform:translateY(-50%);z-index:1}.ne-attachments-preview__body-left{inset-inline-start:16px}.ne-attachments-preview__body-right{inset-inline-end:16px}.ne-attachments .attachment-button-loader{scale:.5;width:26px!important}.ne-attachments--integrated{background-color:rgb(var(--neeto-ui-white));border:1px solid rgb(var(--neeto-ui-gray-300));border-radius:0 0 var(--neeto-ui-rounded) var(--neeto-ui-rounded);border-top:none;padding-inline:6px}.ne-attachments__wrapper{position:relative}.ne-attachments__wrapper.is-dragging-over-files:after{align-items:center;background-color:rgba(var(--neeto-ui-gray-300),.5);border:3px dashed rgb(var(--neeto-ui-gray-500));border-radius:5px;color:rgb(var(--neeto-ui-gray-600));content:\"Drop the file here\";display:flex;font-size:1.2rem;font-weight:700;inset:7px;justify-content:center;pointer-events:none;position:absolute}";
injectCss.n(css,{});

var DEFAULT_FILE_UPLOAD_CONFIG = {
  directUploadEndpoint: constants.DIRECT_UPLOAD_ENDPOINT,
  restrictions: {
    maxFileSize: initializers.globalProps.endUserUploadedFileSizeLimitInMb * 1024 * 1024
  }
};
var ATTACHMENT_OPTIONS = {
  OPEN_IN_NEW_TAB: i18n.t("neetoEditor.common.openInNewTab"),
  DOWNLOAD: i18n.t("neetoEditor.common.download"),
  RENAME: i18n.t("neetoEditor.attachments.rename"),
  DELETE: i18n.t("neetoEditor.menu.delete")
};
var ATTACHMENTS_PREVIEW_DATA_CY = "ne-attachments-preview-content";

var buildFileUploadConfig = function buildFileUploadConfig(config) {
  return ramda.mergeRight(DEFAULT_FILE_UPLOAD_CONFIG, config);
};
var downloadFile = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee(fileUrl, filename) {
    var response, blob, blobUrl, a;
    return _regeneratorRuntime__default.default.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.prev = 0;
          _context.next = 3;
          return fetch(fileUrl);
        case 3:
          response = _context.sent;
          _context.next = 6;
          return response.blob();
        case 6:
          blob = _context.sent;
          blobUrl = URL.createObjectURL(blob);
          a = document.createElement("a");
          a.href = blobUrl;
          a.setAttribute("download", filename);
          a.style.display = "none";
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
          URL.revokeObjectURL(blobUrl);
          _context.next = 21;
          break;
        case 18:
          _context.prev = 18;
          _context.t0 = _context["catch"](0);
          Toastr__default.default.error(_context.t0);
        case 21:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[0, 18]]);
  }));
  return function downloadFile(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();
var checkPreviewAvailability = function checkPreviewAvailability(contentType) {
  return contentType && (contentType.startsWith("video/") || contentType === "application/pdf" || contentType === "image/jpeg" || contentType === "image/gif" || contentType === "image/png" || contentType === "image/webp" || contentType === "text/plain" || contentType === "image/bmp" || contentType === "application/msword" || contentType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" || contentType === "image/jpg" || contentType === "application/vnd.ms-powerpoint" || contentType === "application/vnd.openxmlformats-officedocument.presentationml.presentation" || contentType === "image/tiff" || contentType === "application/vnd.ms-excel" || contentType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
};

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), true).forEach(function (r) { _defineProperty__default.default(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; }
var Menu = Dropdown__default.default.Menu,
  MenuItem = Dropdown__default.default.MenuItem;
var Attachment = function Attachment(_ref) {
  var attachment = _ref.attachment,
    attachments = _ref.attachments,
    disabled = _ref.disabled,
    onChange = _ref.onChange,
    setSelectedAttachment = _ref.setSelectedAttachment,
    isLoading = _ref.isLoading,
    allowDelete = _ref.allowDelete,
    allowOpenInNewTab = _ref.allowOpenInNewTab;
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var _useState = React.useState(false),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    isRenaming = _useState2[0],
    setIsRenaming = _useState2[1];
  var _useState3 = React.useState(false),
    _useState4 = _slicedToArray__default.default(_useState3, 2),
    isUpdating = _useState4[0],
    setIsUpdating = _useState4[1];
  var _useState5 = React.useState(false),
    _useState6 = _slicedToArray__default.default(_useState5, 2),
    isDeleteAlertOpen = _useState6[0],
    setIsDeleteAlertOpen = _useState6[1];
  var _useState7 = React.useState(false),
    _useState8 = _slicedToArray__default.default(_useState7, 2),
    isDeleting = _useState8[0],
    setIsDeleting = _useState8[1];
  var _useState9 = React.useState(""),
    _useState0 = _slicedToArray__default.default(_useState9, 2),
    newFilename = _useState0[0],
    setNewFilename = _useState0[1];
  var renameRef = React.useRef(null);
  useOnClickOutside__default.default(renameRef, function () {
    if (!isRenaming) return;
    setIsRenaming(false);
    setNewFilename("");
  });
  var handleDownload = function handleDownload() {
    return downloadFile(attachment.url, attachment.filename);
  };
  var handleOpenInNewTab = function handleOpenInNewTab() {
    return window.open(attachment.url, "_blank", "noopener,noreferrer");
  };
  var handleRename = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee() {
      var _response$data, signedId, payload, response, filename;
      return _regeneratorRuntime__default.default.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            setIsUpdating(true);
            signedId = attachment.signedId;
            payload = {
              blob: {
                filename: newFilename
              }
            };
            _context.next = 6;
            return reactFileIcon_esm.directUploadsApi.update({
              signedId: signedId,
              payload: payload
            });
          case 6:
            response = _context.sent;
            filename = ((_response$data = response.data) === null || _response$data === void 0 || (_response$data = _response$data.blob) === null || _response$data === void 0 ? void 0 : _response$data.filename) || response.blob.filename;
            onChange(attachments.map(function (attachment) {
              return attachment.signedId === signedId ? ramda.assoc("filename", filename, attachment) : attachment;
            }));
            _context.next = 14;
            break;
          case 11:
            _context.prev = 11;
            _context.t0 = _context["catch"](0);
            Toastr__default.default.error(_context.t0);
          case 14:
            _context.prev = 14;
            setIsRenaming(false);
            setIsUpdating(false);
            setNewFilename("");
            return _context.finish(14);
          case 19:
          case "end":
            return _context.stop();
        }
      }, _callee, null, [[0, 11, 14, 19]]);
    }));
    return function handleRename() {
      return _ref2.apply(this, arguments);
    };
  }();
  var handleDelete = /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee2() {
      var signedId;
      return _regeneratorRuntime__default.default.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            if (allowDelete) {
              _context2.next = 2;
              break;
            }
            return _context2.abrupt("return");
          case 2:
            setIsDeleting(true);
            _context2.prev = 3;
            signedId = attachment.signedId;
            _context2.next = 7;
            return reactFileIcon_esm.directUploadsApi.destroy(signedId);
          case 7:
            onChange(neetoCist.removeBy({
              signedId: signedId
            }, attachments));
            _context2.next = 13;
            break;
          case 10:
            _context2.prev = 10;
            _context2.t0 = _context2["catch"](3);
            Toastr__default.default.error(_context2.t0);
          case 13:
          case "end":
            return _context2.stop();
        }
      }, _callee2, null, [[3, 10]]);
    }));
    return function handleDelete() {
      return _ref3.apply(this, arguments);
    };
  }();
  var handlers = _objectSpread(_objectSpread({}, allowOpenInNewTab && _defineProperty__default.default({}, ATTACHMENT_OPTIONS.OPEN_IN_NEW_TAB, handleOpenInNewTab)), {}, _defineProperty__default.default(_defineProperty__default.default({}, ATTACHMENT_OPTIONS.DOWNLOAD, handleDownload), ATTACHMENT_OPTIONS.RENAME, handleRename), allowDelete && _defineProperty__default.default({}, ATTACHMENT_OPTIONS.DELETE, handleDelete));
  var onMenuItemClick = function onMenuItemClick(_ref6) {
    var key = _ref6.key,
      handler = _ref6.handler;
    if (key === ATTACHMENT_OPTIONS.RENAME) {
      setIsRenaming(true);
      setNewFilename(attachment.filename);
    } else if (key === ATTACHMENT_OPTIONS.DELETE) {
      setNewFilename(attachment.filename);
      setIsDeleteAlertOpen(true);
    } else {
      handler();
    }
  };
  var handleKeyDown = function handleKeyDown(_ref7) {
    var event = _ref7.event,
      key = _ref7.key;
    var handler = handlers[key];
    if (event.key === "Enter" && handler && !ramda.isEmpty(newFilename)) {
      event.stopPropagation();
      event.preventDefault();
      handler();
    }
    if (event.key === "Escape") {
      setIsRenaming(false);
      setNewFilename("");
    }
  };
  return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
    children: [/*#__PURE__*/jsxRuntime.jsx("div", {
      className: "ne-attachments__preview",
      "data-testid": "ne-attachments-wrapper",
      children: isRenaming ? /*#__PURE__*/jsxRuntime.jsx("div", {
        ref: renameRef,
        children: /*#__PURE__*/jsxRuntime.jsx(Tooltip__default.default, {
          content: newFilename,
          position: "top",
          children: /*#__PURE__*/jsxRuntime.jsx(Input__default.default, {
            autoFocus: true,
            "data-testid": "neeto-editor-preview-rename-input",
            size: "small",
            value: newFilename,
            error: ramda.isEmpty(newFilename) ? t("neetoEditor.attachments.nameEmpty") : "",
            suffix: /*#__PURE__*/jsxRuntime.jsxs("div", {
              className: "flex items-center justify-end",
              children: [/*#__PURE__*/jsxRuntime.jsx(Button__default.default, {
                "data-testid": "neeto-editor-preview-rename-submit-button",
                disabled: ramda.isEmpty(newFilename) || isUpdating,
                icon: Check__default.default,
                loading: isUpdating,
                size: "small",
                style: "text",
                onClick: handleRename
              }), /*#__PURE__*/jsxRuntime.jsx(Button__default.default, {
                "data-testid": "neeto-editor-preview-rename-cancel-button",
                disabled: isUpdating,
                icon: Close__default.default,
                size: "small",
                style: "text",
                onClick: function onClick() {
                  setIsRenaming(false);
                  setNewFilename("");
                }
              })]
            }),
            onChange: utils.withEventTargetValue(setNewFilename),
            onKeyDown: function onKeyDown(event) {
              return handleKeyDown({
                event: event,
                key: ATTACHMENT_OPTIONS.RENAME
              });
            }
          })
        })
      }) : /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
        children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
          className: "ne-attachments__preview-wrapper",
          onClick: function onClick() {
            return setSelectedAttachment(attachment);
          },
          children: [/*#__PURE__*/jsxRuntime.jsx(FileIcon.File, {
            className: "ne-attachments__preview-wrapper__icon",
            fileName: attachment.filename
          }), /*#__PURE__*/jsxRuntime.jsx(Tooltip__default.default, {
            content: attachment.filename,
            position: "top",
            children: /*#__PURE__*/jsxRuntime.jsx(Typography__default.default, {
              style: "body2",
              children: attachment.filename
            })
          }), isLoading && /*#__PURE__*/jsxRuntime.jsx(Spinner__default.default, {
            className: "attachment-button-loader"
          })]
        }), /*#__PURE__*/jsxRuntime.jsx(Tooltip__default.default, {
          content: t("neetoEditor.attachments.actionsBlocked"),
          disabled: !disabled,
          position: "top",
          children: /*#__PURE__*/jsxRuntime.jsx(Dropdown__default.default, {
            disabled: disabled,
            buttonSize: "small",
            buttonStyle: "text",
            icon: MenuVertical__default.default,
            children: /*#__PURE__*/jsxRuntime.jsx(Menu, {
              children: Object.entries(handlers).map(function (_ref8) {
                var _ref9 = _slicedToArray__default.default(_ref8, 2),
                  label = _ref9[0],
                  handler = _ref9[1];
                return /*#__PURE__*/jsxRuntime.jsx(MenuItem.Button, {
                  "data-testid": "neeto-editor-preview-".concat(label.toLowerCase(), "-button"),
                  onClick: function onClick() {
                    return onMenuItemClick({
                      key: label,
                      handler: handler
                    });
                  },
                  children: label
                }, label);
              })
            })
          })
        })]
      })
    }), /*#__PURE__*/jsxRuntime.jsx(Alert__default.default, {
      isOpen: isDeleteAlertOpen,
      isSubmitting: isDeleting,
      submitButtonLabel: t("neetoEditor.menu.delete"),
      title: t("neetoEditor.attachments.deleteTitle"),
      message: /*#__PURE__*/jsxRuntime.jsx(reactI18next.Trans, {
        i18nKey: "neetoEditor.attachments.deleteConfirmation",
        values: {
          entity: newFilename
        }
      }),
      onClose: function onClose() {
        return setIsDeleteAlertOpen(false);
      },
      onSubmit: handleDelete
    })]
  });
};

var AttachmentProgress = function AttachmentProgress(_ref) {
  var _attachment$progress;
  var attachment = _ref.attachment,
    cancelUpload = _ref.cancelUpload;
  var progressPercentage = "".concat((_attachment$progress = attachment.progress) !== null && _attachment$progress !== void 0 ? _attachment$progress : 0, "%");
  var handleCancel = function handleCancel() {
    cancelUpload(attachment.id);
  };
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: "ne-attachments__preview",
    children: [/*#__PURE__*/jsxRuntime.jsx("div", {
      className: "ne-attachments__preview__progress-icon",
      children: /*#__PURE__*/jsxRuntime.jsx(reactFileIcon_esm.FileIcon, {})
    }), /*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "ne-attachments__preview__progress",
      children: [/*#__PURE__*/jsxRuntime.jsx(Typography__default.default, {
        style: "body2",
        children: attachment.filename
      }), /*#__PURE__*/jsxRuntime.jsx(Typography__default.default, {
        style: "body2",
        children: progressPercentage
      })]
    }), /*#__PURE__*/jsxRuntime.jsx(Button__default.default, {
      "data-testid": "neeto-editor-preview-upload-cancel-button",
      icon: Close__default.default,
      size: "small",
      style: "text",
      onClick: handleCancel
    })]
  });
};

var Preview = /*#__PURE__*/React.lazy(function () {
  return Promise.resolve().then(function () { return require('./chunk-BYyLrDTW.cjs.js'); });
});
var Attachments$1 = function Attachments(_ref, ref) {
  var _fileUploadConfig$all;
  var _ref$attachments = _ref.attachments,
    attachments = _ref$attachments === void 0 ? [] : _ref$attachments,
    _ref$className = _ref.className,
    className = _ref$className === void 0 ? "" : _ref$className,
    _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? neetoCist.noop : _ref$onChange,
    _ref$isIndependent = _ref.isIndependent,
    isIndependent = _ref$isIndependent === void 0 ? true : _ref$isIndependent,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    _ref$dragDropRef = _ref.dragDropRef,
    dragDropRef = _ref$dragDropRef === void 0 ? null : _ref$dragDropRef,
    _ref$config = _ref.config,
    config = _ref$config === void 0 ? {} : _ref$config,
    _ref$setIsUploading = _ref.setIsUploading,
    setIsUploading = _ref$setIsUploading === void 0 ? neetoCist.noop : _ref$setIsUploading,
    _ref$allowDelete = _ref.allowDelete,
    allowDelete = _ref$allowDelete === void 0 ? true : _ref$allowDelete,
    _ref$allowOpenInNewTa = _ref.allowOpenInNewTab,
    allowOpenInNewTab = _ref$allowOpenInNewTa === void 0 ? true : _ref$allowOpenInNewTa;
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var _useState = React.useState({}),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    selectedAttachment = _useState2[0],
    setSelectedAttachment = _useState2[1];
  var _useState3 = React.useState(false),
    _useState4 = _slicedToArray__default.default(_useState3, 2),
    didFetchPreviewBundle = _useState4[0],
    setDidFetchPreviewBundle = _useState4[1];
  var attachmentInputRef = React.useRef(null);
  var fileUploadConfig = React.useMemo(function () {
    return buildFileUploadConfig(config);
  }, [config]);
  var _useFileUploader = FileIcon.useFileUploader({
      config: fileUploadConfig,
      setIsUploadingOnHost: setIsUploading,
      attachments: attachments
    }),
    addFiles = _useFileUploader.addFiles,
    uploadFiles = _useFileUploader.uploadFiles,
    queuedFiles = _useFileUploader.queuedFiles,
    cancelUpload = _useFileUploader.cancelUpload,
    isUploading = _useFileUploader.isUploading;
  var handleAddFile = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee(event) {
      var uploadedFiles;
      return _regeneratorRuntime__default.default.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            addFiles(event.target.files);
            _context.next = 3;
            return uploadFiles();
          case 3:
            uploadedFiles = _context.sent;
            neetoCist.isNotEmpty(uploadedFiles) && onChange([].concat(_toConsumableArray__default.default(attachments), _toConsumableArray__default.default(uploadedFiles)));
            attachmentInputRef.current.value = null;
          case 6:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function handleAddFile(_x) {
      return _ref2.apply(this, arguments);
    };
  }();
  var handleUploadAttachments = function handleUploadAttachments() {
    return attachmentInputRef.current.click();
  };
  var handleFileInputClick = function handleFileInputClick(event) {
    if (!(!ramda.isEmpty(attachments) && fileUploadConfig.maxNumberOfFiles === 1)) {
      return;
    }
    event.preventDefault();
    Toastr__default.default.warning(t("neetoEditor.attachments.oneAttachmentAllowed"));
  };
  React.useImperativeHandle(ref, function () {
    return {
      handleUploadAttachments: handleUploadAttachments
    };
  }, []);
  var handleFilesDrop = /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee2(files) {
      var uploadedFiles;
      return _regeneratorRuntime__default.default.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            addFiles(files);
            _context2.next = 3;
            return uploadFiles();
          case 3:
            uploadedFiles = _context2.sent;
            neetoCist.isNotEmpty(uploadedFiles) && onChange([].concat(_toConsumableArray__default.default(attachments), _toConsumableArray__default.default(uploadedFiles)));
          case 5:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    return function handleFilesDrop(_x2) {
      return _ref3.apply(this, arguments);
    };
  }();
  reactFileIcon_esm.useDropFiles({
    dropTargetRef: dragDropRef,
    attachments: attachments,
    onDrop: handleFilesDrop
  });
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: classnames__default.default("ne-attachments", _defineProperty__default.default({}, className, className)),
    children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "ne-attachments__items",
      children: [attachments.map(function (attachment) {
        return /*#__PURE__*/React.createElement(Attachment, {
          allowDelete: allowDelete,
          allowOpenInNewTab: allowOpenInNewTab,
          attachment: attachment,
          attachments: attachments,
          disabled: disabled,
          onChange: onChange,
          setSelectedAttachment: setSelectedAttachment,
          key: attachment.signedId,
          isLoading: !didFetchPreviewBundle && selectedAttachment.url === attachment.url
        });
      }), queuedFiles.map(function (attachment) {
        return /*#__PURE__*/React.createElement(AttachmentProgress, {
          attachment: attachment,
          cancelUpload: cancelUpload,
          key: attachment.id
        });
      })]
    }), /*#__PURE__*/jsxRuntime.jsxs("div", {
      children: [isIndependent && /*#__PURE__*/jsxRuntime.jsx(Button__default.default, {
        "data-testid": "neeto-editor-attachments-upload-button",
        disabled: isUploading,
        label: t("neetoEditor.attachments.add"),
        loading: isUploading,
        size: "medium",
        style: "link",
        onClick: handleUploadAttachments
      }), /*#__PURE__*/jsxRuntime.jsx("input", {
        accept: (_fileUploadConfig$all = fileUploadConfig.allowedFileTypes) === null || _fileUploadConfig$all === void 0 ? void 0 : _fileUploadConfig$all.join(","),
        multiple: fileUploadConfig.maxNumberOfFiles !== 1,
        ref: attachmentInputRef,
        type: "file",
        onChange: handleAddFile,
        onClick: handleFileInputClick
      }), /*#__PURE__*/jsxRuntime.jsx(React.Suspense, {
        fallback: /*#__PURE__*/jsxRuntime.jsx("span", {}),
        children: neetoCist.isPresent(selectedAttachment) && /*#__PURE__*/jsxRuntime.jsx(Preview, {
          attachments: attachments,
          selectedAttachment: selectedAttachment,
          setDidFetchPreviewBundle: setDidFetchPreviewBundle,
          setSelectedAttachment: setSelectedAttachment,
          onClose: function onClose() {
            return setSelectedAttachment({});
          }
        })
      })]
    })]
  });
};
Attachments$1.displayName = "NeetoEditorAttachments";
var Attachments = /*#__PURE__*/React.memo(/*#__PURE__*/React.forwardRef(Attachments$1));

exports.ATTACHMENTS_PREVIEW_DATA_CY = ATTACHMENTS_PREVIEW_DATA_CY;
exports.Attachments = Attachments;
exports.checkPreviewAvailability = checkPreviewAvailability;
exports.downloadFile = downloadFile;
//# sourceMappingURL=chunk-DRB71ojr.cjs.js.map
