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); }
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) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
// 评论详情
import React, { useState, useContext } from 'react';
import { FireFilled } from '@ant-design/icons';
import { Space } from 'antd';
import { useRequest } from 'ahooks';
import moment from 'moment';
import { ConfigContext } from "../../../config-provider/context";
import Iconfont from "../../../iconfont";
import CommentEditor from "../../../comment-editor";
import SetInnerHtml from "../set-inner-html";
import Delete from "../delete"; // 删除
import ThumbsUp from "../thumbs-up"; // 点赞
import Selection from "../selection"; // 精选
import { postCommentReply } from "../../service";
import "./index.less";
var CommentItemDetail = function CommentItemDetail(_ref) {
  var issueId = _ref.issueId,
    commentId = _ref.commentId,
    parentReplyId = _ref.parentReplyId,
    type = _ref.type,
    maxLength = _ref.maxLength,
    _ref$className = _ref.className,
    className = _ref$className === void 0 ? '' : _ref$className,
    record = _ref.record,
    hideSelection = _ref.hideSelection,
    hideDelete = _ref.hideDelete,
    hideLike = _ref.hideLike,
    getToken = _ref.getToken,
    onReply = _ref.onReply,
    onDelete = _ref.onDelete,
    onChangeComment = _ref.onChangeComment,
    renderUser = _ref.renderUser,
    renderAt = _ref.renderAt;
  var _useContext = useContext(ConfigContext),
    uiLocale = _useContext.uiLocale;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1]; // 是否展示回复评论框
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    show = _useState4[0],
    setShow = _useState4[1]; // 是否展示操作按钮

  var _useRequest = useRequest(postCommentReply, {
      manual: true
    }),
    postReply = _useRequest.runAsync;

  // 回复评论
  var evReply = function evReply(value) {
    return getToken().then(function (data) {
      postReply(_objectSpread({
        id: issueId,
        commentId: commentId,
        parentReplyId: parentReplyId,
        accessToken: data === null || data === void 0 ? void 0 : data.accessToken
      }, value)).then(function (res) {
        onReply(record.id, res);
        setVisible(false);
        setShow(false);
      });
    });
  };

  // 自定义渲染评论信息，由于other信息由业务层决定，包含哪些信息未知，故后端数据库字段设置时只能使用一个JSON格式的字符串代替，在抛出给业务层使用时这里需要转成对象
  var renderUserInfo = function renderUserInfo(info) {
    var name = (info === null || info === void 0 ? void 0 : info.name) || uiLocale.Comment.anonymity;
    return renderUser ? renderUser(info ? {
      id: info.id,
      name: info.name,
      other: JSON.parse(info.other || '{}')
    } : null) : [name, name];
  };
  var renderOperate = function renderOperate() {
    return /*#__PURE__*/React.createElement(Space, {
      size: [16, 0]
    }, record.allowedChosen && type === 'main' && !hideSelection && /*#__PURE__*/React.createElement(Selection, {
      id: record.id,
      type: type,
      selected: record.chosen,
      onChange: onChangeComment,
      getToken: getToken
    }), record.allowedDelete && !hideDelete && /*#__PURE__*/React.createElement(Delete, {
      id: record.id,
      type: type,
      onChange: onDelete,
      getToken: getToken
    }), /*#__PURE__*/React.createElement("span", {
      onClick: function onClick() {
        return setVisible(!visible);
      },
      className: "comment-operate-item"
    }, visible ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Iconfont, {
      type: "icon-ehr-ui-shouqi"
    }), uiLocale.Comment.collapse) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Iconfont, {
      type: "icon-ehr-ui-huifu"
    }), uiLocale.Comment.reply)), !hideLike && /*#__PURE__*/React.createElement(ThumbsUp, {
      id: record.id,
      type: type,
      count: record.likeNumber || 0,
      selected: record.allowedCancelLike,
      onChange: onChangeComment,
      getToken: getToken
    }));
  };

  // 渲染评论人信息
  var renderName = function renderName() {
    return record.parentReplyId ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("a", null, renderUserInfo(record.commentor)[1]), " ", uiLocale.Comment.reply, " ", /*#__PURE__*/React.createElement("a", null, renderUserInfo(record.toReplier)[1])) : /*#__PURE__*/React.createElement("a", null, renderUserInfo(record.commentor)[1]);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "comment-item-detail ".concat(className, " "),
    onMouseEnter: function onMouseEnter() {
      return setShow(true);
    },
    onMouseLeave: function onMouseLeave() {
      return setShow(false);
    }
  }, type === 'main' && /*#__PURE__*/React.createElement("div", {
    className: "comment-item-detail-main"
  }, /*#__PURE__*/React.createElement("div", {
    className: "comment-item-detail-head comment-item-detail-head-main"
  }, /*#__PURE__*/React.createElement("span", {
    className: "name"
  }, renderName()), record.chosen && /*#__PURE__*/React.createElement("span", {
    className: "selection"
  }, /*#__PURE__*/React.createElement(FireFilled, null), " ", uiLocale.Comment.selected)), /*#__PURE__*/React.createElement("div", {
    className: "comment-item-detail-content"
  }, /*#__PURE__*/React.createElement(SetInnerHtml, {
    html: record.content,
    renderAt: renderAt
  })), /*#__PURE__*/React.createElement("div", {
    className: "comment-operate"
  }, /*#__PURE__*/React.createElement("div", {
    className: "time"
  }, moment(record.createTime).format('YYYY-MM-DD HH:mm')), /*#__PURE__*/React.createElement("div", null, renderOperate())), visible && /*#__PURE__*/React.createElement(CommentEditor, {
    maxLength: maxLength,
    placeholder: "".concat(uiLocale.Comment.replyTo, " ").concat(renderUserInfo(record.commentor)[0], "\uFF1A"),
    onSubmit: evReply
  })), type === 'minor' && /*#__PURE__*/React.createElement("div", {
    className: "comment-item-detail-minor"
  }, /*#__PURE__*/React.createElement("div", {
    className: "comment-item-detail-head"
  }, /*#__PURE__*/React.createElement("span", {
    className: "name"
  }, renderName(), "\uFF1A"), /*#__PURE__*/React.createElement(SetInnerHtml, {
    html: record.content,
    renderAt: renderAt
  })), /*#__PURE__*/React.createElement("div", {
    className: "comment-item-detail-footer"
  }, /*#__PURE__*/React.createElement("span", {
    className: "time"
  }, moment(record.createTime).format('YYYY-MM-DD HH:mm')), show && /*#__PURE__*/React.createElement("div", {
    className: "comment-operate"
  }, renderOperate())), visible && /*#__PURE__*/React.createElement(CommentEditor, {
    maxLength: maxLength,
    placeholder: "".concat(uiLocale.Comment.replyTo, " ").concat(renderUserInfo(record.commentor)[0], "\uFF1A"),
    onSubmit: evReply
  })));
};
export default CommentItemDetail;