var _excluded = ["commentId", "total", "items", "onLoadMoreReply", "getToken"],
  _excluded2 = ["record", "renderAvatar"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
// 主评论、子评论
import React, { useState, useRef, useEffect, useContext } from 'react';
import { Avatar, Button } from 'antd';
import { UserOutlined } from '@ant-design/icons';
import { useRequest } from 'ahooks';
import moment from 'moment';
import { ConfigContext } from "../../../config-provider/context";
import { replaceMessage } from "../../../locale/utils";
import CommentDetail from "../comment-detail";
import { getCommentReplyList } from "../../service";
import "./index.less";

// 主评论的回复列表
var maxCount = 3; // 默认展示子评论条数
var CommentListItemMinor = function CommentListItemMinor(props) {
  var _useContext = useContext(ConfigContext),
    uiLocale = _useContext.uiLocale;
  var commentId = props.commentId,
    total = props.total,
    items = props.items,
    onLoadMoreReply = props.onLoadMoreReply,
    getToken = props.getToken,
    rest = _objectWithoutProperties(props, _excluded);
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    list = _useState2[0],
    setList = _useState2[1]; // 当前展示的回复评论列表
  var _useRequest = useRequest(getCommentReplyList, {
      manual: true
    }),
    loading = _useRequest.loading,
    runAsync = _useRequest.runAsync; // 回复评论列表分页查询

  /**
   * 查看更多
   * 1、第一次点击“查看更多”时，展示第一页评论数据（第一页信息在初始化查询评论列表时已经给了，无需再查询）
   * 2、往后每次点击“查看更多”按钮，分页查询评论数据，手动组装评论列表(数据源在最上层父组件统一管理，故手动组装数据的操作在父组件完成)
   */
  var isFirst = useRef(true);
  var onLoadMore = function onLoadMore() {
    if (isFirst.current) {
      setList(items);
      isFirst.current = false;
      return;
    }
    getToken().then(function (data) {
      runAsync({
        orderId: moment(list[list.length - 1].id).valueOf(),
        page: 1,
        pageSize: 10,
        id: commentId,
        accessToken: data === null || data === void 0 ? void 0 : data.accessToken
      }).then(function (res) {
        res.items && onLoadMoreReply(commentId, res.items); // 向上抛出分页查询的数据，在上层完成数据组装工作
      });
    });
  };

  // 未“查看更多”则只展示默认三条数据
  useEffect(function () {
    if (isFirst.current) {
      setList(_toConsumableArray(items).splice(0, maxCount));
      return;
    }
    setList(items);
  }, [items]);
  return /*#__PURE__*/React.createElement("div", {
    className: "u-comment-item-minor"
  }, list.map(function (item) {
    return /*#__PURE__*/React.createElement("div", {
      className: "minor-item",
      key: item.id
    }, /*#__PURE__*/React.createElement(CommentDetail, _extends({}, rest, {
      className: "minor-comment",
      type: "minor",
      record: item,
      commentId: commentId,
      parentReplyId: item.id,
      getToken: getToken
    })));
  }), total > maxCount && list.length < total && /*#__PURE__*/React.createElement("div", {
    className: "comment-item-load-more"
  }, /*#__PURE__*/React.createElement("span", null, replaceMessage(uiLocale.Comment.totalComment, {
    count: total
  })), /*#__PURE__*/React.createElement(Button, {
    type: "link",
    onClick: onLoadMore,
    loading: loading
  }, uiLocale.Comment.loadMore)));
};

// 主评论
var CommentListItem = function CommentListItem(props) {
  var _record$replyDTOPageI;
  var record = props.record,
    renderAvatar = props.renderAvatar,
    rest = _objectWithoutProperties(props, _excluded2);
  var _renderAvatar = function _renderAvatar() {
    var commentor = record.commentor;
    // 由于other信息由业务层决定，包含哪些信息未知，故后端数据库字段设置时只能使用一个JSON格式的字符串代替，在抛出给业务层使用时这里需要转成对象
    return renderAvatar ? renderAvatar(commentor ? {
      id: commentor.id,
      name: commentor.name,
      other: JSON.parse(commentor.other || '{}')
    } : null) : /*#__PURE__*/React.createElement(Avatar, {
      icon: /*#__PURE__*/React.createElement(UserOutlined, null)
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "u-comment-item"
  }, /*#__PURE__*/React.createElement("div", {
    className: "comment-item-avatar"
  }, _renderAvatar()), /*#__PURE__*/React.createElement("div", {
    className: "comment-item-wrap"
  }, /*#__PURE__*/React.createElement(CommentDetail, _extends({}, rest, {
    record: record,
    type: "main",
    commentId: record.id
  })), !!((_record$replyDTOPageI = record.replyDTOPageInfo) !== null && _record$replyDTOPageI !== void 0 && _record$replyDTOPageI.total) && /*#__PURE__*/React.createElement("div", {
    className: "comment-item-expend"
  }, /*#__PURE__*/React.createElement(CommentListItemMinor, _extends({}, rest, {
    commentId: record.id,
    total: record.replyDTOPageInfo.total,
    items: record.replyDTOPageInfo.items
  })))));
};
export default CommentListItem;