var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/comment/index.tsx
var comment_exports = {};
__export(comment_exports, {
  default: () => comment_default
});
module.exports = __toCommonJS(comment_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_moment = __toESM(require("moment"));
var import_context = require("../config-provider/context");
var import_utils = require("../locale/utils");
var import_hooks = require("../_util/hooks");
var import_service = require("./service");
var import_comment_editor = __toESM(require("../comment-editor"));
var import_tabs = __toESM(require("./tabs"));
var import_list = __toESM(require("./list"));
var import_index = require("./index.less");
var DEFAULT_PAGINATION = {
  page: 1,
  pageSize: 10
};
var Comment = (props) => {
  const { uiLocale } = (0, import_react.useContext)(import_context.ConfigContext);
  const { placeholder = uiLocale.Comment.placeholderComment, empty, issueId, getToken, getCommentCount, onSubmit, ...rest } = props;
  const { data: commentList, loading, runAsync } = (0, import_ahooks.useRequest)(import_service.getCommentList, { manual: true });
  const { totalPages } = commentList || {};
  const [list, setList] = (0, import_react.useState)([]);
  const [count, setCount] = (0, import_react.useState)(0);
  const [tab, setTab] = (0, import_react.useState)("newest");
  const getList = (0, import_react.useCallback)(() => {
    getToken().then((data) => {
      runAsync({ ...DEFAULT_PAGINATION, id: issueId, accessToken: data == null ? void 0 : data.accessToken, status: tab }).then((data2) => {
        const { total = 0 } = data2;
        setCount(total);
        setList(data2.items || []);
      });
    });
  }, [issueId, tab]);
  (0, import_react.useEffect)(() => {
    getList();
  }, [tab]);
  (0, import_react.useEffect)(() => {
    getCommentCount == null ? void 0 : getCommentCount(count);
  }, [count]);
  const onPublish = async (params) => {
    let token = await getToken();
    let data = await (0, import_service.postComment)({ issueId, accessToken: token.accessToken, ...params });
    setList([data, ...list]);
    setCount((prev) => prev + 1);
    onSubmit == null ? void 0 : onSubmit(data);
  };
  const onReply = (id, value) => {
    let newList = list.map((item) => {
      var _a, _b;
      if (item.id === id || ((_b = (_a = item.replyDTOPageInfo) == null ? void 0 : _a.items) == null ? void 0 : _b.some((_item) => _item.id === id))) {
        return {
          ...item,
          replyDTOPageInfo: item.replyDTOPageInfo ? {
            ...item.replyDTOPageInfo,
            total: item.replyDTOPageInfo.total + 1,
            items: [value, ...item.replyDTOPageInfo.items]
            // 顶部插入评论内容（时间倒序）
          } : {
            // 主评论下没有回复则手动生成一个评论回复列表
            total: 1,
            page: 1,
            totalPages: 1,
            items: [value]
          }
        };
      }
      return item;
    });
    setList(newList);
  };
  const onDelete = (id, type) => {
    if (type === "main") {
      setList(list.filter((item) => item.id !== id));
      setCount((prev) => prev - 1);
    }
    if (type === "minor") {
      let newList = list.map((item) => {
        var _a;
        if ((_a = item.replyDTOPageInfo) == null ? void 0 : _a.items.some((_item) => _item.id === id)) {
          item.replyDTOPageInfo = {
            ...item.replyDTOPageInfo,
            total: item.replyDTOPageInfo.total - 1,
            // 总回复数减一
            items: item.replyDTOPageInfo.items.filter((_item) => _item.id !== id)
            // 过滤被删除回复
          };
        }
        return item;
      });
      setList(newList);
    }
  };
  const onChange = (id, load, type) => {
    if (type === "main") {
      setList(list.map((item) => item.id === id ? { ...item, ...load } : item));
    }
    if (type === "minor") {
      let newList = list.map((item) => {
        var _a;
        if ((_a = item.replyDTOPageInfo) == null ? void 0 : _a.items.some((_item) => _item.id === id)) {
          item.replyDTOPageInfo = {
            ...item.replyDTOPageInfo,
            items: item.replyDTOPageInfo.items.map((__item) => __item.id === id ? { ...__item, ...load } : __item)
          };
          console.log(item);
        }
        return item;
      });
      setList(newList);
    }
  };
  const onLoadMore = () => {
    getToken().then((data) => {
      runAsync({
        orderId: (0, import_moment.default)(list[list.length - 1].id).valueOf(),
        ...DEFAULT_PAGINATION,
        id: issueId,
        accessToken: data == null ? void 0 : data.accessToken,
        status: tab
      }).then((data2) => {
        let newList = list.concat(data2.items || []), temp = [], ids = [];
        newList.forEach((item) => !ids.includes(item.id) && temp.push(item));
        setList(newList);
      });
    });
  };
  const onLoadMoreReply = (id, commentList2) => {
    let newList = list.map((item) => {
      if (item.id === id && item.replyDTOPageInfo) {
        item.replyDTOPageInfo = {
          ...item.replyDTOPageInfo,
          items: item.replyDTOPageInfo.items.concat(commentList2)
        };
      }
      return item;
    });
    setList(newList);
  };
  return (0, import_hooks.useConfig)(
    /* @__PURE__ */ import_react.default.createElement("div", { className: "u-comment" }, /* @__PURE__ */ import_react.default.createElement(import_comment_editor.default, { ...rest, placeholder, onSubmit: onPublish }), /* @__PURE__ */ import_react.default.createElement("div", { className: "comment-wrap" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { spinning: loading }, count ? /* @__PURE__ */ import_react.default.createElement("div", { className: "comment-content-all" }, /* @__PURE__ */ import_react.default.createElement(import_tabs.default, { onChange: setTab }), /* @__PURE__ */ import_react.default.createElement(
      import_list.default,
      {
        ...rest,
        issueId,
        list,
        getToken,
        onReply,
        onDelete,
        onChangeComment: onChange,
        onLoadMoreReply
      }
    ), /* @__PURE__ */ import_react.default.createElement("div", { className: "load-more" }, totalPages === 1 && /* @__PURE__ */ import_react.default.createElement("div", null, uiLocale.Comment.noMore), list.length < count && /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("span", null, (0, import_utils.replaceMessage)(uiLocale.Comment.totalComment, { count })), /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { onClick: onLoadMore, type: "link", loading }, uiLocale.Comment.loadMore)))) : empty)))
  );
};
var comment_default = Comment;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
