"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _popconfirm = require("antd/es/popconfirm");

var _popconfirm2 = _interopRequireDefault(_popconfirm);

var _icon = require("antd/es/icon");

var _icon2 = _interopRequireDefault(_icon);

var _tooltip = require("antd/es/tooltip");

var _tooltip2 = _interopRequireDefault(_tooltip);

var _popover = require("antd/es/popover");

var _popover2 = _interopRequireDefault(_popover);

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

require("antd/es/popconfirm/style/css");

require("antd/es/icon/style/css");

require("antd/es/tooltip/style/css");

require("antd/es/popover/style/css");

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

var _dayjs = require("dayjs");

var _dayjs2 = _interopRequireDefault(_dayjs);

require("dayjs/locale/zh-cn");

var _relativeTime = require("dayjs/plugin/relativeTime");

var _relativeTime2 = _interopRequireDefault(_relativeTime);

var _reactIntlUniversal = require("react-intl-universal");

var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);

var _AvatarHoverCard = require("./AvatarHoverCard");

var _AvatarHoverCard2 = _interopRequireDefault(_AvatarHoverCard);

var _Comment = require("../../Comment");

var _Comment2 = _interopRequireDefault(_Comment);

var _CommentInput = require("../CommentInput");

var _CommentInput2 = _interopRequireDefault(_CommentInput);

var _avatar = require("../../avatar");

var _avatar2 = _interopRequireDefault(_avatar);

var _helper = require("../../helper");

var _constant = require("../../constant");

require("./index.css");

var _ImagePreviewer = require("../ImagePreviewer/ImagePreviewer");

var _ImagePreviewer2 = _interopRequireDefault(_ImagePreviewer);

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// import 'dayjs/locale/es';


// dayjs.locale("zh-cn");
_dayjs2.default.extend(_relativeTime2.default);

var LOCALES = {
  "zh-CN": "zh-cn"
};

var CommentItem = function (_Component) {
  _inherits(CommentItem, _Component);

  function CommentItem(props) {
    _classCallCheck(this, CommentItem);

    var _this = _possibleConstructorReturn(this, (CommentItem.__proto__ || Object.getPrototypeOf(CommentItem)).call(this, props));

    _this.state = {
      showInput: false,
      showPreviewer: false,
      previewerIndex: 0,
      popoverVisible: false
    };
    _this.handleToggleInput = _this.handleToggleInput.bind(_this);
    _this.renderTextWithReply = _this.renderTextWithReply.bind(_this);
    _this.showPreviewer = _this.showPreviewer.bind(_this);
    _this.hidePreviewer = _this.hidePreviewer.bind(_this);
    _this.handleVisibleChange = _this.handleVisibleChange.bind(_this);
    _this.handleUserAvaClick = _this.handleUserAvaClick.bind(_this);
    return _this;
  }

  _createClass(CommentItem, [{
    key: "showPreviewer",
    value: function showPreviewer(index) {
      this.setState({
        showPreviewer: true,
        previewerIndex: index
      });
    }
  }, {
    key: "hidePreviewer",
    value: function hidePreviewer() {
      this.setState({
        showPreviewer: false
      });
    }
  }, {
    key: "handleToggleInput",
    value: function handleToggleInput() {
      this.setState({ showInput: !this.state.showInput });
    }
  }, {
    key: "handleVisibleChange",
    value: function handleVisibleChange(visible) {
      this.setState({
        popoverVisible: visible
      });
    }
  }, {
    key: "handleUserAvaClick",
    value: function handleUserAvaClick() {
      var user_id = this.props.user_id;
      var userAvaClick = this.props.app.userAvaClick;

      if (userAvaClick) {
        userAvaClick(user_id);
      }
    }
  }, {
    key: "renderTextWithReply",
    value: function renderTextWithReply(text, content) {
      var newText = text;
      var reply = content.reply;

      if (reply) {
        // newText = `${newText} //@<a href="/${reply.user_id}">${
        //   reply.user_name
        // }</a> ${reply.content}`;
        newText = newText + " //@" + reply.user_name + " " + reply.content;
        // newText = (
        //   <span>
        //     {newText}
        //     @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
        //   </span>
        // )
        if (reply.reply) {
          return this.renderTextWithReply(newText, reply);
        }
      }
      return newText;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          commentId = _props.commentId,
          replyId = _props.replyId,
          content = _props.content,
          action = _props.action,
          showReply = _props.showReply,
          onShowReply = _props.onShowReply,
          app = _props.app,
          user_id = _props.user_id,
          page = _props.page;
      var _props$app = this.props.app,
          locale = _props$app.locale,
          showHoverCard = _props$app.showHoverCard,
          showEdit = _props$app.showEdit;
      var showInput = this.state.showInput;

      var newContent = content.content;
      var images = "";
      if (newContent.indexOf(_constant.IMAGE_SPLIT) !== -1) {
        newContent = newContent.split(_constant.IMAGE_SPLIT);
        images = newContent.pop();
        newContent = newContent.join("");
      }

      var imageList = images.split(",");

      // 在3, 7前需要换行
      var needClear = imageList.length === 5 || imageList.length === 6 || imageList.length === 9;
      var imgs = [].concat(_toConsumableArray(imageList));
      if (needClear) {
        if (imgs.length > 6) {
          imgs.splice(3, 0, { type: "divider" });
          imgs.splice(7, 0, { type: "divider" });
        } else {
          imgs.splice(3, 0, { type: "divider" });
        }
      }
      var IconColor = content.favor_count > 0 ? "#71C135" : "#4a90e2";

      return _react2.default.createElement(
        "div",
        { className: "comment-item-box" },
        _react2.default.createElement(
          "div",
          { className: "comment-item-left" },
          showHoverCard ? _react2.default.createElement(
            _popover2.default,
            {
              content: _react2.default.createElement(_AvatarHoverCard2.default, _extends({}, this.props.app, {
                user_id: user_id,
                handleVisibleChange: this.handleVisibleChange,
                image: content.user_avatar || _avatar2.default
              }))
              // placement={this.props.placement}
              // trigger="click"
              , visible: this.state.popoverVisible,
              onVisibleChange: this.handleVisibleChange,
              overlayClassName: "avatar-hover-card-overlay"
            },
            _react2.default.createElement("div", {
              className: "comment-item-avatar",
              style: {
                backgroundImage: "url(" + (content.user_avatar || _avatar2.default) + ")"
              },
              onClick: this.handleUserAvaClick
            })
          ) : _react2.default.createElement("div", {
            className: "comment-item-avatar",
            style: {
              backgroundImage: "url(" + (content.user_avatar || _avatar2.default) + ")"
            },
            onClick: this.handleUserAvaClick
          })
        ),
        _react2.default.createElement(
          "div",
          { className: "comment-item-right" },
          _react2.default.createElement(
            "div",
            null,
            _react2.default.createElement(
              "strong",
              {
                onClick: this.handleUserAvaClick,
                className: "comment-item-name",
                style: { cursor: "pointer" }
              },
              content.user_name || _reactIntlUniversal2.default.get("comment.tourist")
            ),
            _react2.default.createElement(
              "span",
              { className: "comment-item-date", style: { marginLeft: 10 } },
              _react2.default.createElement(
                _tooltip2.default,
                {
                  placement: "top",
                  title: (0, _dayjs2.default)(content.created * 1000).format("YYYY-MM-DD HH:mm:ss")
                },
                LOCALES[locale] ? (0, _dayjs2.default)(content.created * 1000).locale(LOCALES[locale]).fromNow() : (0, _dayjs2.default)(content.created * 1000).fromNow()
              )
            )
          ),
          _react2.default.createElement("div", {
            className: "comment-item-content",
            dangerouslySetInnerHTML: {
              __html: (0, _helper.renderContent)(this.renderTextWithReply(newContent, content))
            }
          }),
          // image为空时不渲染comment-item-image
          imageList.length > 0 && imageList[0] !== "" && _react2.default.createElement(
            "div",
            { className: "comment-item-image" },
            !this.state.showPreviewer && imgs.map(function (item, index) {
              if (item.type === "divider") {
                return _react2.default.createElement(
                  "div",
                  { className: "comment-item-image-wrapper", key: index },
                  _react2.default.createElement("div", { className: "comment-img-divider" })
                );
              }
              return _react2.default.createElement(
                "div",
                {
                  className: "comment-item-image-wrapper",
                  key: index,
                  onClick: function onClick() {
                    var i = index;
                    if (needClear) {
                      if (index > 3) {
                        i -= 1;
                      }
                      if (index > 7) {
                        i -= 1;
                      }
                    }
                    _this2.showPreviewer(i);
                  }
                },
                _react2.default.createElement("div", {
                  style: { backgroundImage: "url(" + item + ")" },
                  className: "comment-img-thumbnail"
                })
              );
            }),
            this.state.showPreviewer && _react2.default.createElement(_ImagePreviewer2.default, {
              list: imageList,
              index: this.state.previewerIndex,
              onFold: this.hidePreviewer
            }),
            _react2.default.createElement("div", { className: "clearfix" })
          ),
          _react2.default.createElement(
            "div",
            { className: "comment-item-bottom" },
            content.reply_count ? _react2.default.createElement(
              "div",
              null,
              _react2.default.createElement(
                "a",
                { className: "comment-item-bottom-left", onClick: onShowReply },
                _reactIntlUniversal2.default.get("reply.totalReply", { total: content.reply_count }),
                showReply ? _react2.default.createElement(_icon2.default, { type: "up" }) : _react2.default.createElement(_icon2.default, { type: "down" })
              )
            ) : null,
            showEdit && app.userId === content.user_id && _react2.default.createElement("i", {
              className: "comment-item-edit",
              onClick: function onClick() {
                return _this2.props.app.handleEdit({
                  action: action,
                  replyId: replyId,
                  commentId: commentId,
                  userId: content.user_id,
                  content: content,
                  replyPage: page
                });
              }
            }),
            app.userId === content.user_id && _react2.default.createElement(
              _popconfirm2.default,
              {
                // title="确定要删除吗?"
                title: _reactIntlUniversal2.default.get("popConfirm.title"),
                onConfirm: function onConfirm() {
                  if (replyId) {
                    app.sDeleteReply(content.id, commentId);
                    return;
                  }
                  app.sDeleteComment(content.id);
                },
                okText: _reactIntlUniversal2.default.get("popConfirm.ok"),
                cancelText: _reactIntlUniversal2.default.get("popConfirm.cancel")
              },
              _react2.default.createElement("i", { className: "comment-item-delete" })
            ),
            _react2.default.createElement("span", { className: "comment-item-divider" }),
            _react2.default.createElement(
              "div",
              {
                className: "comment-item-bottom-right",
                onClick: function onClick() {
                  if (replyId) {
                    // 如果有 replyId,则说明是评论的回复
                    app.sReplyFavor(content.id, commentId, content.favored);
                    return;
                  }
                  app.sCommentFavor(content.id, content.favored);
                },
                style: { color: "" + IconColor }
              },
              _react2.default.createElement("i", {
                className: content.favored ? "comment-item-like" : "comment-item-unlike"
              })
            ),
            _react2.default.createElement(
              "span",
              null,
              "\xA0",
              content.favor_count
            ),
            _react2.default.createElement(
              "div",
              {
                onClick: this.handleToggleInput,
                className: "comment-item-reply"
              },
              "\xA0 ",
              _reactIntlUniversal2.default.get("comment.reply")
            )
          )
        ),
        showInput && _react2.default.createElement(_CommentInput2.default, {
          content: app.children,
          action: action,
          replyId: replyId,
          commentId: commentId,
          userId: content.user_id,
          callback: this.handleToggleInput
        })
      );
    }
  }]);

  return CommentItem;
}(_react.Component);

CommentItem.propTypes = {
  content: _propTypes2.default.object.isRequired,
  // comment 评论
  // reply 评论的回复
  // replyToReply 回复的回复
  action: _propTypes2.default.oneOf(["comment", "reply", "replyToReply"]),
  onShowReply: _propTypes2.default.func,
  showEdit: _propTypes2.default.bool
};

CommentItem.defaultProps = {
  action: "comment",
  onShowReply: function onShowReply() {},
  showEdit: false
};

exports.default = (0, _Comment2.default)(CommentItem);
//# sourceMappingURL=index.js.map