"use strict";

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); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _imgViewer = _interopRequireDefault(require("../img-viewer"));

var _dialogUpload = _interopRequireDefault(require("../dialog-upload"));

var _util = require("../dialog-upload/util");

var _uploadedPreview = _interopRequireDefault(require("../dialog-upload/uploaded-preview"));

require("./index.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var prefix = 'attachments-preview';

var AttachmentsPreview = function AttachmentsPreview(props) {
  var attachments = props.attachments,
      _props$enableDelete = props.enableDelete,
      enableDelete = _props$enableDelete === void 0 ? true : _props$enableDelete,
      onChange = props.onChange,
      onUploadFile = props.onUploadFile,
      _props$enableUploadFi = props.enableUploadFile,
      enableUploadFile = _props$enableUploadFi === void 0 ? true : _props$enableUploadFi;
  var previewRef = (0, _react.useRef)();

  var onSubmithandler = function onSubmithandler(index, option) {
    switch (option) {
      case 'cancel':
      case 'delete':
        {
          onChange(attachments.filter(function (f) {
            var _attachments$index;

            return f.Id !== attachments[index].Id || (f === null || f === void 0 ? void 0 : f.DocumentId) !== ((_attachments$index = attachments[index]) === null || _attachments$index === void 0 ? void 0 : _attachments$index.DocumentId);
          }));
          break;
        }

      case 'preview':
        {
          if ((0, _util.isImg)(attachments[index].MediaType || attachments[index].FileType)) {
            var i = attachments.filter(function (f) {
              return (0, _util.isImg)(f.MediaType || attachments[index].FileType);
            }).findIndex(function (f) {
              return f.Id == attachments[index].Id;
            });
            previewRef.current.show(i);
          } else {
            var _attachments$index2, _attachments$index3;

            window.open(((_attachments$index2 = attachments[index]) === null || _attachments$index2 === void 0 ? void 0 : _attachments$index2.ClientUrl) || ((_attachments$index3 = attachments[index]) === null || _attachments$index3 === void 0 ? void 0 : _attachments$index3.PreviewUrl));
          }

          break;
        }

      case 'download':
        {
          window.open(attachments[index].DownloadUrl);
          break;
        }
    }
  };

  if (!attachments.length && !enableUploadFile) return null;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_imgViewer["default"], {
    data: attachments.filter(function (f) {
      return (0, _util.isImg)(f.MediaType || f.FileType);
    }).map(function (f) {
      return _objectSpread(_objectSpread({}, f), {}, {
        previewUrl: f.ClientUrl || f.PreviewUrl
      });
    }),
    ref: previewRef,
    showDownload: true
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: prefix
  }, attachments.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement(_uploadedPreview["default"], {
      className: "".concat(prefix, "__item"),
      key: index,
      index: index,
      uploadResultItem: _objectSpread(_objectSpread({}, item), {}, {
        uploadStatus: 3,
        percentComplete: 100,
        Deleteable: enableDelete
      }),
      onSubmit: onSubmithandler
    });
  }), enableUploadFile && /*#__PURE__*/_react["default"].createElement(_dialogUpload["default"], {
    onSubmit: onUploadFile
  })));
};

var _default = AttachmentsPreview;
exports["default"] = _default;