'use strict';

var React = require('react');
var neetoAtoms = require('@bigbinary/neeto-atoms');
var lucideReact = require('lucide-react');
var neetoCist = require('@bigbinary/neeto-cist');
var ramda = require('ramda');
var index = require('./chunk-C-VxABfm.cjs.js');
var reactI18next = require('react-i18next');
var FileIcon = require('./chunk-CDAiAyof.cjs.js');
var v2_Attachments = require('./chunk-Cvxaj__T.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('./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-commons-frontend/v2/initializers');
require('./chunk-2HrSPdAV.cjs.js');
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('yup');
require('classnames');
require('@bigbinary/neeto-commons-frontend/v2/react-utils/useOnClickOutside');
require('@bigbinary/neeto-commons-frontend/v2/utils');

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 v2_Attachments.downloadFile(url, filename);
  };
  var setPreview = function setPreview() {
    var isPreviewAvailable = v2_Attachments.checkPreviewAvailability(contentType);
    if (isPreviewAvailable) {
      switch (contentType.split("/")[0]) {
        case "image":
          return /*#__PURE__*/jsxRuntime.jsx("img", {
            "data-testid": FileIcon.ATTACHMENTS_PREVIEW_DATA_CY,
            src: url
          });
        case "video":
          return /*#__PURE__*/jsxRuntime.jsx("video", {
            controls: true,
            "data-testid": FileIcon.ATTACHMENTS_PREVIEW_DATA_CY,
            src: url
          });
        case "application":
        case "text":
          if (contentType === "application/pdf") {
            return /*#__PURE__*/jsxRuntime.jsx("iframe", {
              "data-testid": FileIcon.ATTACHMENTS_PREVIEW_DATA_CY,
              src: url,
              width: "100%"
            });
          }
          return /*#__PURE__*/jsxRuntime.jsx(index.DocViewer, {
            className: "ne-attachments-preview__body-docviewer",
            "data-testid": FileIcon.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(neetoAtoms.Typography, {
      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(neetoAtoms.Dialog, {
    onClose: onClose,
    className: "ne-attachments-preview",
    isOpen: !ramda.isEmpty(selectedAttachment),
    size: "fullScreen",
    onKeyDown: handleKeyDown,
    children: [/*#__PURE__*/jsxRuntime.jsxs(neetoAtoms.Dialog.Header, {
      className: "ne-attachments-preview__header",
      children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
        className: "ne-attachments-preview__header__fileinfo",
        children: [/*#__PURE__*/jsxRuntime.jsx(neetoAtoms.Dialog.Title, {
          children: /*#__PURE__*/jsxRuntime.jsx(neetoAtoms.Typography, {
            variant: "h4",
            children: neetoCist.truncate(filename, 25)
          })
        }), /*#__PURE__*/jsxRuntime.jsx(neetoAtoms.Typography, {
          variant: "body2",
          children: FileIcon.convertToFileSize(size)
        })]
      }), /*#__PURE__*/jsxRuntime.jsx(neetoAtoms.Button, {
        label: t("neetoEditor.common.download"),
        ref: downloadRef,
        variant: "link",
        onClick: handleDownload
      })]
    }), /*#__PURE__*/jsxRuntime.jsxs(neetoAtoms.Dialog.Body, {
      className: "ne-attachments-preview__body",
      children: [/*#__PURE__*/jsxRuntime.jsx(neetoAtoms.Button, {
        className: "ne-attachments-preview__body-left",
        icon: lucideReact.ChevronLeft,
        size: "lg",
        variant: "ghost",
        onClick: handleLeftArrowClick
      }), /*#__PURE__*/jsxRuntime.jsx(neetoAtoms.Button, {
        className: "ne-attachments-preview__body-right",
        icon: lucideReact.ChevronRight,
        size: "lg",
        variant: "ghost",
        onClick: handleRightArrowClick
      }), setPreview()]
    })]
  });
};

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