import { useRef, useEffect } from 'react';
import { findIndexBy, truncate } from '@bigbinary/neeto-cist';
import Left from '@bigbinary/neeto-icons/Left';
import Right from '@bigbinary/neeto-icons/Right';
import Modal from '@bigbinary/neetoui/Modal';
import Typography from '@bigbinary/neetoui/Typography';
import Button from '@bigbinary/neetoui/Button';
import { isEmpty } from 'ramda';
import { D as DocViewer, a as DocViewerRenderers } from './chunk-CeoqieEt.js';
import { useTranslation, Trans } from 'react-i18next';
import { c as convertToFileSize } from './chunk-xCgPoq9w.js';
import { d as downloadFile, c as checkPreviewAvailability, a as ATTACHMENTS_PREVIEW_DATA_CY } from './chunk-BO6bjSuY.js';
import { jsxs, jsx } from 'react/jsx-runtime';
import './chunk-BFTU3MAI.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/regenerator';
import 'zustand/shallow';
import 'axios';
import '@bigbinary/neetoui/Toastr';
import './chunk-BFKmIsWV.js';
import '@babel/runtime/helpers/defineProperty';
import '@bigbinary/neeto-commons-frontend/react-utils';
import 'zustand';
import '@babel/runtime/helpers/createClass';
import '@babel/runtime/helpers/classCallCheck';
import 'i18next';
import '@bigbinary/neeto-icons/LeftAlign';
import '@bigbinary/neeto-icons/CenterAlign';
import '@bigbinary/neeto-icons/RightAlign';
import '@bigbinary/neeto-icons/Delete';
import '@bigbinary/neeto-icons/CustomSize';
import '@bigbinary/neeto-icons/BorderDisable';
import '@bigbinary/neeto-icons/BorderEnable';
import '@bigbinary/neeto-icons/AspectRatio11';
import '@bigbinary/neeto-icons/AspectRatio169';
import '@bigbinary/neeto-icons/AspectRatio916';
import '@bigbinary/neeto-icons/AspectRatio43';
import '@bigbinary/neeto-icons/AspectRatio32';
import '@bigbinary/neeto-commons-frontend/initializers';
import 'yup';
import './chunk-DDwycKmK.js';
import 'classnames';
import './chunk-C2dztUxs.js';
import '@bigbinary/neeto-commons-frontend/react-utils/useOnClickOutside';
import '@bigbinary/neeto-commons-frontend/utils';
import '@bigbinary/neeto-icons/MenuVertical';
import '@bigbinary/neeto-icons/Close';
import '@bigbinary/neeto-icons/Check';
import '@bigbinary/neetoui/Dropdown';
import '@bigbinary/neetoui/Input';
import '@bigbinary/neetoui/Spinner';
import '@bigbinary/neetoui/Tooltip';
import '@bigbinary/neetoui/Alert';

var Preview = function Preview(_ref) {
  var onClose = _ref.onClose,
    attachments = _ref.attachments,
    selectedAttachment = _ref.selectedAttachment,
    setSelectedAttachment = _ref.setSelectedAttachment,
    setDidFetchPreviewBundle = _ref.setDidFetchPreviewBundle;
  var _useTranslation = useTranslation(),
    t = _useTranslation.t;
  var downloadRef = useRef(null);
  var _selectedAttachment$f = selectedAttachment.filename,
    filename = _selectedAttachment$f === void 0 ? "" : _selectedAttachment$f,
    _selectedAttachment$c = selectedAttachment.contentType,
    contentType = _selectedAttachment$c === void 0 ? null : _selectedAttachment$c,
    _selectedAttachment$u = selectedAttachment.url,
    url = _selectedAttachment$u === void 0 ? "" : _selectedAttachment$u,
    _selectedAttachment$s = selectedAttachment.signedId,
    signedId = _selectedAttachment$s === void 0 ? "" : _selectedAttachment$s,
    _selectedAttachment$s2 = selectedAttachment.size,
    size = _selectedAttachment$s2 === void 0 ? undefined : _selectedAttachment$s2;
  var attachmentIndex = findIndexBy({
    signedId: signedId
  }, attachments);
  var handleRightArrowClick = function handleRightArrowClick() {
    var _downloadRef$current;
    var newIndex = (attachmentIndex + 1) % attachments.length;
    setSelectedAttachment(attachments[newIndex]);
    (_downloadRef$current = downloadRef.current) === null || _downloadRef$current === void 0 || _downloadRef$current.focus();
  };
  var handleLeftArrowClick = function handleLeftArrowClick() {
    var _downloadRef$current2;
    var newIndex = (attachmentIndex - 1 + attachments.length) % attachments.length;
    setSelectedAttachment(attachments[newIndex]);
    (_downloadRef$current2 = downloadRef.current) === null || _downloadRef$current2 === void 0 || _downloadRef$current2.focus();
  };
  var handleKeyDown = function handleKeyDown(event) {
    if (event.key === "ArrowRight") {
      handleRightArrowClick();
    } else if (event.key === "ArrowLeft") {
      handleLeftArrowClick();
    }
  };
  useEffect(function () {
    setDidFetchPreviewBundle(true);
  }, []);
  var handleDownload = function handleDownload() {
    return downloadFile(url, filename);
  };
  var setPreview = function setPreview() {
    var isPreviewAvailable = checkPreviewAvailability(contentType);
    if (isPreviewAvailable) {
      switch (contentType.split("/")[0]) {
        case "image":
          return /*#__PURE__*/jsx("img", {
            "data-testid": ATTACHMENTS_PREVIEW_DATA_CY,
            src: url
          });
        case "video":
          return /*#__PURE__*/jsx("video", {
            controls: true,
            "data-testid": ATTACHMENTS_PREVIEW_DATA_CY,
            src: url
          });
        case "application":
        case "text":
          if (contentType === "application/pdf") {
            return /*#__PURE__*/jsx("iframe", {
              "data-testid": ATTACHMENTS_PREVIEW_DATA_CY,
              src: url,
              width: "100%"
            });
          }
          return /*#__PURE__*/jsx(DocViewer, {
            className: "ne-attachments-preview__body-docviewer",
            "data-testid": ATTACHMENTS_PREVIEW_DATA_CY,
            documents: [{
              uri: url,
              fileType: contentType
            }],
            pluginRenderers: DocViewerRenderers,
            config: {
              header: {
                disableHeader: true,
                disableFileName: true
              }
            }
          });
        default:
          return null;
      }
    }
    return /*#__PURE__*/jsx(Typography, {
      children: /*#__PURE__*/jsx(Trans, {
        i18nKey: "neetoEditor.attachments.noPreview",
        components: {
          span: /*#__PURE__*/jsx("span", {
            className: "ne-attachments-preview__body-download",
            onClick: handleDownload
          })
        }
      })
    });
  };
  return /*#__PURE__*/jsxs(Modal, {
    onClose: onClose,
    className: "ne-attachments-preview",
    isOpen: !isEmpty(selectedAttachment),
    size: "fullScreen",
    onKeyDown: handleKeyDown,
    children: [/*#__PURE__*/jsxs(Modal.Header, {
      className: "ne-attachments-preview__header",
      children: [/*#__PURE__*/jsxs("div", {
        className: "ne-attachments-preview__header__fileinfo",
        children: [/*#__PURE__*/jsx(Typography, {
          style: "h4",
          children: truncate(filename, 25)
        }), /*#__PURE__*/jsx(Typography, {
          style: "body2",
          children: convertToFileSize(size)
        })]
      }), /*#__PURE__*/jsx(Button, {
        label: t("neetoEditor.common.download"),
        ref: downloadRef,
        style: "link",
        onClick: handleDownload
      })]
    }), /*#__PURE__*/jsxs(Modal.Body, {
      className: "ne-attachments-preview__body",
      children: [/*#__PURE__*/jsx(Button, {
        className: "ne-attachments-preview__body-left",
        icon: Left,
        size: "large",
        style: "tertiary",
        onClick: handleLeftArrowClick
      }), /*#__PURE__*/jsx(Button, {
        className: "ne-attachments-preview__body-right",
        icon: Right,
        size: "large",
        style: "tertiary",
        onClick: handleRightArrowClick
      }), setPreview()]
    })]
  });
};

export { Preview as default };
//# sourceMappingURL=chunk-3JO2B_HR.js.map
