"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../_vendor/@legendapp/state/index.cjs");
const index_browser = require("../_vendor/nanoid/index.browser.cjs");
const coreStore = require("./core-store.cjs");
const messageStore = require("./message-store.cjs");
const ratingStore$ = index.observable({
  // 负面评价映射表（以评价ID为键）
  negative: {},
  // 正面评价映射表（以评价ID为键）
  positive: {},
  // 反馈对话框状态
  feedbackDialog: {
    isOpen: false,
    rating: null,
    targetMessageId: null
  }
});
const ratingComputed$ = index.observable({
  // 评价统计信息
  ratingsStats: () => {
    return {
      positiveCount: Object.keys(ratingStore$.positive.get()).length,
      negativeCount: Object.keys(ratingStore$.negative.get()).length
    };
  },
  // 总评价数
  totalRatings: () => {
    const positive = Object.keys(ratingStore$.positive.get()).length;
    const negative = Object.keys(ratingStore$.negative.get()).length;
    return positive + negative;
  },
  // 所有评价列表
  allRatings: () => {
    const positive = Object.values(ratingStore$.positive.get());
    const negative = Object.values(ratingStore$.negative.get());
    return [...positive, ...negative];
  }
});
const ratingActions = {
  // 添加评价
  addRating: (rating) => {
    const isPositive = rating.rating === "positive";
    const ratingsMap = isPositive ? ratingStore$.positive.peek() : ratingStore$.negative.peek();
    const newRatingsMap = { ...ratingsMap, [rating.id]: rating };
    if (isPositive) {
      ratingStore$.positive.set(newRatingsMap);
    } else {
      ratingStore$.negative.set(newRatingsMap);
    }
  },
  // 删除评价
  // ❌ 未使用
  removeRating: (ratingId) => {
    const positive = { ...ratingStore$.positive.peek() };
    const negative = { ...ratingStore$.negative.peek() };
    delete positive[ratingId];
    delete negative[ratingId];
    ratingStore$.positive.set(positive);
    ratingStore$.negative.set(negative);
  },
  // 重置所有评价
  // ❌ 未使用
  resetRatings: () => {
    ratingStore$.positive.set({});
    ratingStore$.negative.set({});
  },
  // 根据消息ID获取评价
  // ❌ 未使用
  getRatingByMessageId: (messageId) => {
    const positive = ratingStore$.positive.peek();
    const negative = ratingStore$.negative.peek();
    const positiveRating = Object.values(positive).find((rating) => rating.messageId === messageId);
    const negativeRating = Object.values(negative).find((rating) => rating.messageId === messageId);
    return positiveRating || negativeRating || null;
  },
  // 获取所有评价
  // ❌ 未使用
  getAllRatings: () => {
    const positive = Object.values(ratingStore$.positive.peek());
    const negative = Object.values(ratingStore$.negative.peek());
    return [...positive, ...negative];
  },
  // 获取正面评价
  // ❌ 未使用
  getPositiveRatings: () => {
    return Object.values(ratingStore$.positive.peek());
  },
  // 获取负面评价
  // ❌ 未使用
  getNegativeRatings: () => {
    return Object.values(ratingStore$.negative.peek());
  },
  // 清空所有评价（resetRatings 的别名）
  // ❌ 未使用
  clearAllRatings: () => {
    ratingStore$.positive.set({});
    ratingStore$.negative.set({});
  },
  // 根据评价ID获取评价
  // ❌ 未使用
  getRating: (ratingId) => {
    const positive = ratingStore$.positive.peek();
    const negative = ratingStore$.negative.peek();
    return positive[ratingId] || negative[ratingId] || null;
  },
  // 按类型获取评价
  // ❌ 未使用
  getRatingsByType: (type) => {
    return Object.values(ratingStore$[type].peek());
  },
  // 获取评价统计
  // ❌ 未使用
  getTotalRatings: () => {
    const positiveCount = Object.keys(ratingStore$.positive.peek()).length;
    const negativeCount = Object.keys(ratingStore$.negative.peek()).length;
    return {
      positiveCount,
      negativeCount,
      totalCount: positiveCount + negativeCount
    };
  },
  // 打开反馈对话框
  openFeedbackDialog: (messageId, rating) => {
    ratingStore$.feedbackDialog.set({ isOpen: true, rating, targetMessageId: messageId });
  },
  // 关闭反馈对话框
  closeFeedbackDialog: () => {
    ratingStore$.feedbackDialog.set({ isOpen: false, rating: null, targetMessageId: null });
  },
  // 提交反馈
  submitFeedback: (data) => {
    const { messageId, rating, reason, additionalFeedback } = data;
    const loaded = messageStore.messageStore$.loadedMessages.peek();
    const aiMessage = loaded[messageId];
    if (!aiMessage) {
      console.warn(`AI message not found for feedback: ${messageId}`);
      return;
    }
    const chat = coreStore.coreStore$.currentChat.peek();
    const ratedAt = (/* @__PURE__ */ new Date()).toISOString();
    const userMessageId = aiMessage.parentMessageId || "";
    const userMessage = userMessageId ? loaded[userMessageId] : void 0;
    const ratingItem = {
      id: index_browser.nanoid(),
      messageId: aiMessage.id,
      chatId: chat?.id || "",
      userId: chat?.userId || "",
      rating,
      feedback: reason || additionalFeedback ? {
        reason,
        comment: additionalFeedback
      } : void 0,
      question: userMessage?.content || "",
      // 用户问题
      response: aiMessage.content,
      // AI 回复
      createdAt: ratedAt
    };
    ratingActions.addRating(ratingItem);
    messageStore.messageActions.updateMessage(aiMessage.id, {
      rating,
      ratedAt,
      feedback: {
        rating,
        reason,
        additionalComments: additionalFeedback,
        ratedAt
      },
      lastUpdatedBy: "submitFeedback"
    });
    ratingActions.closeFeedbackDialog();
  }
};
exports.ratingActions = ratingActions;
exports.ratingComputed$ = ratingComputed$;
exports.ratingStore$ = ratingStore$;
//# sourceMappingURL=rating-store.cjs.map
