'use strict';

var React = require('react');
var neetoCist = require('@bigbinary/neeto-cist');
var Left = require('@bigbinary/neeto-icons/Left');
var Right = require('@bigbinary/neeto-icons/Right');
var Modal = require('@bigbinary/neetoui/Modal');
var Typography = require('@bigbinary/neetoui/Typography');
var Button = require('@bigbinary/neetoui/Button');
var ramda = require('ramda');
var index = require('./chunk-C-VxABfm.cjs.js');
var reactI18next = require('react-i18next');
var FileIcon = require('./chunk-yX0BlnxV.cjs.js');
var Attachments = require('./chunk-DRB71ojr.cjs.js');
var jsxRuntime = require('react/jsx-runtime');
require('./chunk-BJu3ubxk.cjs.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/slicedToArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/regenerator');
require('zustand/shallow');
require('axios');
require('@bigbinary/neetoui/Toastr');
require('./chunk-D3Unocnm.cjs.js');
require('@babel/runtime/helpers/defineProperty');
require('@bigbinary/neeto-commons-frontend/react-utils');
require('zustand');
require('@babel/runtime/helpers/createClass');
require('@babel/runtime/helpers/classCallCheck');
require('i18next');
require('@bigbinary/neeto-icons/LeftAlign');
require('@bigbinary/neeto-icons/CenterAlign');
require('@bigbinary/neeto-icons/RightAlign');
require('@bigbinary/neeto-icons/Delete');
require('@bigbinary/neeto-icons/CustomSize');
require('@bigbinary/neeto-icons/BorderDisable');
require('@bigbinary/neeto-icons/BorderEnable');
require('@bigbinary/neeto-icons/AspectRatio11');
require('@bigbinary/neeto-icons/AspectRatio169');
require('@bigbinary/neeto-icons/AspectRatio916');
require('@bigbinary/neeto-icons/AspectRatio43');
require('@bigbinary/neeto-icons/AspectRatio32');
require('@bigbinary/neeto-commons-frontend/initializers');
require('yup');
require('./chunk-2HrSPdAV.cjs.js');
require('classnames');
require('./chunk-B6qYtOJe.cjs.js');
require('@bigbinary/neeto-commons-frontend/react-utils/useOnClickOutside');
require('@bigbinary/neeto-commons-frontend/utils');
require('@bigbinary/neeto-icons/MenuVertical');
require('@bigbinary/neeto-icons/Close');
require('@bigbinary/neeto-icons/Check');
require('@bigbinary/neetoui/Dropdown');
require('@bigbinary/neetoui/Input');
require('@bigbinary/neetoui/Spinner');
require('@bigbinary/neetoui/Tooltip');
require('@bigbinary/neetoui/Alert');

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

var Left__default = /*#__PURE__*/_interopDefault(Left);
var Right__default = /*#__PURE__*/_interopDefault(Right);
var Modal__default = /*#__PURE__*/_interopDefault(Modal);
var Typography__default = /*#__PURE__*/_interopDefault(Typography);
var Button__default = /*#__PURE__*/_interopDefault(Button);

var Preview = function Preview(_ref) {
  var onClose = _ref.onClose,
    attachments = _ref.attachments,
    selectedAttachment = _ref.selectedAttachment,
    setSelectedAttachment = _ref.setSelectedAttachment,
    setDidFetchPreviewBundle = _ref.setDidFetchPreviewBundle;
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var downloadRef = React.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 = neetoCist.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();
    }
  };
  React.useEffect(function () {
    setDidFetchPreviewBundle(true);
  }, []);
  var handleDownload = function handleDownload() {
    return Attachments.downloadFile(url, filename);
  };
  var setPreview = function setPreview() {
    var isPreviewAvailable = Attachments.checkPreviewAvailability(contentType);
    if (isPreviewAvailable) {
      switch (contentType.split("/")[0]) {
        case "image":
          return /*#__PURE__*/jsxRuntime.jsx("img", {
            "data-testid": Attachments.ATTACHMENTS_PREVIEW_DATA_CY,
            src: url
          });
        case "video":
          return /*#__PURE__*/jsxRuntime.jsx("video", {
            controls: true,
            "data-testid": Attachments.ATTACHMENTS_PREVIEW_DATA_CY,
            src: url
          });
        case "application":
        case "text":
          if (contentType === "application/pdf") {
            return /*#__PURE__*/jsxRuntime.jsx("iframe", {
              "data-testid": Attachments.ATTACHMENTS_PREVIEW_DATA_CY,
              src: url,
              width: "100%"
            });
          }
          return /*#__PURE__*/jsxRuntime.jsx(index.DocViewer, {
            className: "ne-attachments-preview__body-docviewer",
            "data-testid": Attachments.ATTACHMENTS_PREVIEW_DATA_CY,
            documents: [{
              uri: url,
              fileType: contentType
            }],
            pluginRenderers: index.DocViewerRenderers,
            config: {
              header: {
                disableHeader: true,
                disableFileName: true
              }
            }
          });
        default:
          return null;
      }
    }
    return /*#__PURE__*/jsxRuntime.jsx(Typography__default.default, {
      children: /*#__PURE__*/jsxRuntime.jsx(reactI18next.Trans, {
        i18nKey: "neetoEditor.attachments.noPreview",
        components: {
          span: /*#__PURE__*/jsxRuntime.jsx("span", {
            className: "ne-attachments-preview__body-download",
            onClick: handleDownload
          })
        }
      })
    });
  };
  return /*#__PURE__*/jsxRuntime.jsxs(Modal__default.default, {
    onClose: onClose,
    className: "ne-attachments-preview",
    isOpen: !ramda.isEmpty(selectedAttachment),
    size: "fullScreen",
    onKeyDown: handleKeyDown,
    children: [/*#__PURE__*/jsxRuntime.jsxs(Modal__default.default.Header, {
      className: "ne-attachments-preview__header",
      children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
        className: "ne-attachments-preview__header__fileinfo",
        children: [/*#__PURE__*/jsxRuntime.jsx(Typography__default.default, {
          style: "h4",
          children: neetoCist.truncate(filename, 25)
        }), /*#__PURE__*/jsxRuntime.jsx(Typography__default.default, {
          style: "body2",
          children: FileIcon.convertToFileSize(size)
        })]
      }), /*#__PURE__*/jsxRuntime.jsx(Button__default.default, {
        label: t("neetoEditor.common.download"),
        ref: downloadRef,
        style: "link",
        onClick: handleDownload
      })]
    }), /*#__PURE__*/jsxRuntime.jsxs(Modal__default.default.Body, {
      className: "ne-attachments-preview__body",
      children: [/*#__PURE__*/jsxRuntime.jsx(Button__default.default, {
        className: "ne-attachments-preview__body-left",
        icon: Left__default.default,
        size: "large",
        style: "tertiary",
        onClick: handleLeftArrowClick
      }), /*#__PURE__*/jsxRuntime.jsx(Button__default.default, {
        className: "ne-attachments-preview__body-right",
        icon: Right__default.default,
        size: "large",
        style: "tertiary",
        onClick: handleRightArrowClick
      }), setPreview()]
    })]
  });
};

exports.default = Preview;
//# sourceMappingURL=chunk-BYyLrDTW.cjs.js.map
