"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 _tooltip = _interopRequireDefault(require("../tooltip"));

var _util = require("./util");

var _getLocale = require("../../utils/getLocale");

require("./uploaded-preview.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; }

var prefix = 'titaui-dialog-upload-preview';

var UploadedPreview = function UploadedPreview(_ref) {
  var uploadResultItem = _ref.uploadResultItem,
      onSubmit = _ref.onSubmit,
      className = _ref.className,
      index = _ref.index;
  var updateStatusTimer = null; // 附件缩略图

  var renderPreviewThumbnail = function renderPreviewThumbnail() {
    // 上传中或不是图片文件时用默认icon
    if (uploadResultItem.uploadStatus === 2 || !(0, _util.isImg)(uploadResultItem.MediaType || uploadResultItem.FileType)) {
      if (uploadResultItem.MediaType || uploadResultItem.FileType) {
        return /*#__PURE__*/_react["default"].createElement("img", {
          src: (0, _util.getFileTypeMapIcon)(uploadResultItem.MediaType || uploadResultItem.FileType),
          className: "upload-preview-img"
        });
      }
    }

    return /*#__PURE__*/_react["default"].createElement("img", {
      src: uploadResultItem.ClientUrl || uploadResultItem.PreviewUrl,
      className: "upload-preview-img"
    }); // 上传成功且为图片时
  };

  var handleClickTitleToPreView = function handleClickTitleToPreView() {
    onSubmit(index, 'preview');
  }; // 删除、预览icon


  var dealPreviewOptions = function dealPreviewOptions(uploadStatus, index, deleteable, smallImageUrl, downloadUrl) {
    var canCancel = uploadStatus === 2 || uploadStatus === 1;
    var canDelete = deleteable && !canCancel;
    var canPreview = !!smallImageUrl;
    var canDowload = !canCancel && !!downloadUrl;
    return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, canDowload && /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
      overlay: (0, _getLocale.getLocale)('Mod_Download'),
      placement: "top"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "dialog-upload-file-action-wrapper"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-APP-xiazai action-icon",
      onMouseDown: function onMouseDown() {
        return onSubmit(index, 'download');
      }
    }))), canPreview && /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
      overlay: (0, _getLocale.getLocale)('Mod_Preview'),
      placement: "top"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "dialog-upload-file-action-wrapper"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-yulan action-icon",
      onMouseDown: function onMouseDown() {
        return onSubmit(index, 'preview');
      }
    }))), canDelete && /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
      overlay: (0, _getLocale.getLocale)('Mod_Delete'),
      placement: "top"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "dialog-upload-file-action-wrapper"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-canceled action-icon delete",
      onMouseDown: function onMouseDown() {
        return onSubmit(index, 'delete');
      }
    }))), canCancel && /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
      overlay: (0, _getLocale.getLocale)('Rep_NewS_CanUpload'),
      placement: "top"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "dialog-upload-file-action-wrapper"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-canceled action-icon action-icon-delete",
      onMouseDown: function onMouseDown() {
        return onSubmit(index, 'cancel');
      }
    }))));
  };

  (0, _react.useEffect)(function () {
    return function () {
      clearTimeout(updateStatusTimer);
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "preview__content document-mouse-event-ignore ".concat(className, " ").concat(prefix)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__img-wrapper"),
    onClick: handleClickTitleToPreView
  }, renderPreviewThumbnail()), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__message-wrapper")
  }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
    overlay: uploadResultItem.FileName,
    placement: "top"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__file-title"),
    "data-tip": uploadResultItem.FileName,
    key: uploadResultItem.Id,
    onClick: handleClickTitleToPreView
  }, uploadResultItem.FileName, (uploadResultItem.uploadStatus === 2 || uploadResultItem.uploadStatus === 3) && uploadResultItem.percentComplete !== 100 ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__upload-progress")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      width: "".concat(uploadResultItem.percentComplete, "%")
    },
    className: "upload-file--progress"
  })) : /*#__PURE__*/_react["default"].createElement("div", {
    className: uploadResultItem.uploadStatus === 3 || uploadResultItem.uploadStatus === 2 ? 'upload-file-size' : 'upload-file-size upload-file-size--error'
  }, uploadResultItem.uploadStatus === 3 || uploadResultItem.uploadStatus === 2 ? (0, _util.getFileSize)(uploadResultItem.Size) : uploadResultItem.Msg)))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__options-wrapper")
  }, dealPreviewOptions(uploadResultItem.uploadStatus, index, uploadResultItem.Deleteable, uploadResultItem.ClientUrl || uploadResultItem.PreviewUrl, uploadResultItem.DownloadUrl)));
};

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