"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 _viewer = require("../viewer");

var _type = require("../upload/type");

var _style = require("./style");

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

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var RVIewerOptions = {
  className: 'preview-image-container',
  toolbar: {
    zoomIn: 4,
    zoomOut: 4,
    oneToOne: 4,
    reset: 4,
    prev: 4,
    play: {
      show: false
    },
    next: 4,
    rotateLeft: 4,
    rotateRight: 4,
    flipHorizontal: false,
    flipVertical: false
  }
};

var CommunicationAttachments = /*#__PURE__*/function (_PureComponent) {
  _inherits(CommunicationAttachments, _PureComponent);

  var _super = _createSuper(CommunicationAttachments);

  function CommunicationAttachments(props) {
    var _this;

    _classCallCheck(this, CommunicationAttachments);

    _this = _super.call(this, props);

    _this.renderAttachmentPreviewThumbnail = function (item, index) {
      var isImage = (0, _type._dealAttachmentType)(item.fileType).IsImage;
      var iconColor = (0, _type._dealAttachmentType)(item.fileType).IconColor;
      var fileType = (0, _type._dealAttachmentType)(item.fileType).FileType;

      if (item.smallImageUrl && item.documentSource && item.documentSource == 1) {
        isImage = true;
      }

      return /*#__PURE__*/_react["default"].createElement(_style.AttachmentItemIcon, {
        className: "file-icon"
      }, isImage ? /*#__PURE__*/_react["default"].createElement("img", {
        src: item.smallImageUrl,
        alt: "",
        style: {
          cursor: 'pointer'
        },
        onMouseDown: function onMouseDown(event) {
          _this.RViewers.show(index);

          event.stopPropagation();
          event.nativeEvent.stopImmediatePropagation();
        }
      }) : /*#__PURE__*/_react["default"].createElement("span", {
        className: "ico_afb tu-icon-".concat(fileType),
        style: {
          color: iconColor
        }
      }));
    };

    _this.renderPreviewTrigger = function (item, index) {
      if (item.previewUrl != null && item.previewUrl != '') {
        // 有预览链接
        if (item.smallImageUrl && item.documentSource == 1) {
          // 图片文件
          return /*#__PURE__*/_react["default"].createElement("span", {
            style: {
              color: '#2879ff',
              cursor: 'pointer'
            },
            onMouseDown: function onMouseDown(event) {
              _this.RViewers.show(index);

              event.stopPropagation();
              event.nativeEvent.stopImmediatePropagation();
            }
          }, _this.props.mode == 'column' ? (0, _getLocale.getLocale)('Mod_Preview') : /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
            overlay: (0, _getLocale.getLocale)('Mod_Preview'),
            getTooltipContainer: function getTooltipContainer() {
              return document.body;
            }
          }, /*#__PURE__*/_react["default"].createElement("span", {
            className: "tu-icon-magnifier-circle"
          })));
        }

        if (item.documentSource == 1) {
          return /*#__PURE__*/_react["default"].createElement("a", {
            href: item.previewUrl,
            target: "_blank",
            "data-attachment": "preview",
            rel: "noreferrer"
          }, _this.props.mode == 'column' ? (0, _getLocale.getLocale)('Mod_Preview') : /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
            overlay: (0, _getLocale.getLocale)('Mod_Preview'),
            getTooltipContainer: function getTooltipContainer() {
              return document.body;
            }
          }, /*#__PURE__*/_react["default"].createElement("span", {
            className: "tu-icon-magnifier-circle"
          })));
        }

        return /*#__PURE__*/_react["default"].createElement("span", null);
      }

      return /*#__PURE__*/_react["default"].createElement("span", null);
    };

    _this.state = {
      attachmentPreviewUrl: []
    };
    return _this;
  }

  _createClass(CommunicationAttachments, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var attachments = this.props.attachments;
      var attachmentPreviewUrl = attachments.length > 0 ? attachments.map(function (item) {
        return item.previewUrl;
      }) : [];
      this.setState({
        attachmentPreviewUrl: attachmentPreviewUrl
      });
    } // 附件缩略图

  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          attachments = _this$props.attachments,
          className = _this$props.className,
          mode = _this$props.mode,
          itemStyle = _this$props.itemStyle,
          wrapperClassName = _this$props.wrapperClassName,
          wrapperStyle = _this$props.wrapperStyle;
      var attachmentPreviewUrl = this.state.attachmentPreviewUrl;
      return /*#__PURE__*/_react["default"].createElement(_style.CommunicationAttachmentContainer, {
        className: wrapperClassName,
        style: wrapperStyle
      }, /*#__PURE__*/_react["default"].createElement(_viewer.RViewer, {
        imageUrls: attachmentPreviewUrl,
        ref: function ref(r) {
          return _this2.RViewers = r;
        },
        options: RVIewerOptions
      }), attachments.length > 0 && attachments.map(function (item, index) {
        return /*#__PURE__*/_react["default"].createElement(_style.AttachmentItemWrapper, {
          style: itemStyle,
          className: "".concat(className, " ").concat(mode == 'column' ? '' : 'row-wrapper'),
          key: item.documentId
        }, _this2.renderAttachmentPreviewThumbnail(item, index), /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
          overlay: item.fileName,
          getTooltipContainer: function getTooltipContainer() {
            return document.body;
          }
        }, /*#__PURE__*/_react["default"].createElement(_style.AttachmentItemName, {
          className: "file-name"
        }, item.fileName)), /*#__PURE__*/_react["default"].createElement(_style.AttachmentAction, {
          className: "item__action"
        }, /*#__PURE__*/_react["default"].createElement("a", {
          href: item.downloadUrl,
          "data-attachment": "dowload"
        }, mode == 'column' ? (0, _getLocale.getLocale)('Mod_Download') : /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
          overlay: (0, _getLocale.getLocale)('Mod_Download'),
          getTooltipContainer: function getTooltipContainer() {
            return document.body;
          }
        }, /*#__PURE__*/_react["default"].createElement("span", {
          className: "tu-icon-download-circle"
        }))), _this2.renderPreviewTrigger(item, index)));
      }));
    }
  }]);

  return CommunicationAttachments;
}(_react.PureComponent);

exports["default"] = CommunicationAttachments;
CommunicationAttachments.defaultProps = {
  mode: 'column'
};