"use strict";
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var common_vendor = require("../../common/vendor.js");
var kongshuju = require("../../kongshuju.js");
var api_index = require("../../api/index.js");
var config_index = require("../../config/index.js");
require("../../utils/request.js");
const _sfc_main = {
  setup(__props) {
    const state = common_vendor.reactive({
      initChatList: [],
      chatList: [],
      finished: false
    });
    const { chatList, finished } = common_vendor.toRefs(state);
    const getMessageRecord = () => {
      api_index.getAllRolesMessageRecord({
        start_time: common_vendor.hooks().subtract(7, "days").startOf().format("YYYY-MM-DD HH:mm:ss"),
        stop_time: common_vendor.hooks().endOf().format("YYYY-MM-DD HH:mm:ss")
      }).then(({ status, data }) => {
        if (Number(status) === 200 && Array.isArray(data.role_list)) {
          const list = data.role_list.map((item) => {
            var _a;
            const [lastMessage] = (_a = item.list) == null ? void 0 : _a.slice(-1);
            return __spreadProps(__spreadValues({}, item), {
              lastMessage
            });
          });
          state.initChatList = list;
          state.chatList = list;
        }
      }).finally(() => {
        state.finished = true;
      });
    };
    common_vendor.onShow(getMessageRecord);
    const onChatOnLine = async ({ role_info }) => {
      try {
        const { user_info } = common_vendor.index.getStorageSync("userData");
        const { user_id } = user_info || {};
        if (user_id) {
          const { status, data } = await api_index.createChat({
            user_id,
            character_id: role_info.role_id
          });
          if (status === "success") {
            common_vendor.index.setStorage({
              key: "chatInfo",
              data: __spreadValues({
                userId: user_id,
                session_id: data.session_id
              }, role_info),
              success: () => {
                common_vendor.index.navigateTo({
                  url: "/pages/chatOnline/index"
                });
              }
            });
          } else {
            common_vendor.index.showToast({
              title: "\u521B\u5EFA\u804A\u5929\u5931\u8D25"
            });
          }
        } else {
          common_vendor.index.showToast({
            title: "\u5F53\u524D\u767B\u5F55\u5DF2\u5931\u6548\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55",
            icon: "none"
          });
          common_vendor.index.clearStorage();
          common_vendor.index.navigateTo({
            url: "/pages/login/index"
          });
        }
      } catch (e) {
        throw new Error(e);
      }
    };
    const timer = common_vendor.ref(null);
    const onInputKeyWords = (e) => {
      const keyWord = e.detail.value;
      if (timer.value) {
        clearTimeout(timer.value);
      }
      timer.value = setTimeout(() => {
        if (keyWord) {
          const clonedChatList = JSON.parse(JSON.stringify(state.initChatList));
          const list = clonedChatList.filter((item) => {
            const { role_info, list: list2 } = item;
            if (role_info.role_name.includes(keyWord)) {
              return true;
            }
            const filterMessageList = list2.filter(({ message }) => message.includes(keyWord));
            if (filterMessageList.length > 0) {
              item.lastMessage = filterMessageList[0];
              return true;
            }
            return false;
          });
          state.chatList = list;
        } else {
          state.chatList = state.initChatList;
        }
      }, 500);
    };
    const sliceMessage = (str) => {
      const message = str.replace(/\r\n|\r|\n/g, " ");
      if (message.length > 20) {
        return `${message.substring(0, 20)}...`;
      }
      return message;
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.o(onInputKeyWords),
        b: common_vendor.unref(finished) && common_vendor.unref(chatList).length === 0
      }, common_vendor.unref(finished) && common_vendor.unref(chatList).length === 0 ? {
        c: kongshuju._imports_1
      } : common_vendor.unref(finished) && common_vendor.unref(chatList).length > 0 ? {
        e: common_vendor.f(common_vendor.unref(chatList), (item, index, i0) => {
          return {
            a: `${common_vendor.unref(config_index.filePrefix)}${item.role_info.avatar_id}`,
            b: common_vendor.t(item.role_info.role_name),
            c: common_vendor.t(sliceMessage(item.lastMessage.message)),
            d: common_vendor.t(item.lastMessage.create_at),
            e: index,
            f: common_vendor.o(($event) => onChatOnLine(item))
          };
        })
      } : {}, {
        d: common_vendor.unref(finished) && common_vendor.unref(chatList).length > 0
      });
    };
  }
};
var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-3fea99d8"], ["__file", "C:/Users/bzm15/Desktop/ix/mini_web-develop/pages/chat/index.vue"]]);
wx.createPage(MiniProgramPage);
