"use strict";
var __defProp = Object.defineProperty;
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 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 finished = common_vendor.ref(false);
    const list = common_vendor.reactive([]);
    const bannerList = common_vendor.computed$1(() => list.slice(0, 3));
    const getRoleList = () => {
      api_index.getAllRoles().then(({ status, data }) => {
        if (Number(status) === 200 && Array.isArray(data)) {
          list.push(...data);
        }
      }).finally(() => {
        finished.value = true;
      });
    };
    common_vendor.onMounted(getRoleList);
    const showDialog = common_vendor.ref(false);
    const currentItem = common_vendor.ref({});
    const onOpenRoleDialog = (item) => {
      currentItem.value = item;
      showDialog.value = true;
    };
    const isExistChat = (item) => {
      api_index.getChatExist({
        role_id: item.role_id
      }).then(({ status, data }) => {
        if (Number(status) === 200) {
          if (data.exists) {
            toChat(item);
          } else {
            onOpenRoleDialog(item);
          }
        } else {
          onOpenRoleDialog(item);
        }
      }).catch(() => {
        onOpenRoleDialog(item);
      });
    };
    const toCall = async (item) => {
      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: item.role_id
          });
          if (status === "success") {
            common_vendor.index.setStorage({
              key: "chatInfo",
              data: __spreadValues({
                userId: user_id,
                session_id: data.session_id
              }, item),
              success: () => {
                common_vendor.index.navigateTo({
                  url: "/pages/call/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 toChat = async (item) => {
      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: item.role_id
          });
          if (status === "success") {
            common_vendor.index.setStorage({
              key: "chatInfo",
              data: __spreadValues({
                userId: user_id,
                session_id: data.session_id
              }, item),
              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);
      }
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: finished.value && common_vendor.unref(list).length === 0
      }, finished.value && common_vendor.unref(list).length === 0 ? {
        b: kongshuju._imports_1
      } : {}, {
        c: finished.value && common_vendor.unref(bannerList).length > 0
      }, finished.value && common_vendor.unref(bannerList).length > 0 ? {
        d: common_vendor.f(common_vendor.unref(bannerList), (item, index, i0) => {
          return {
            a: common_vendor.o(($event) => onOpenRoleDialog(item)),
            b: `${common_vendor.unref(config_index.filePrefix)}${item.avatar_id}`,
            c: index
          };
        })
      } : {}, {
        e: finished.value && common_vendor.unref(list).length > 0
      }, finished.value && common_vendor.unref(list).length > 0 ? {} : {}, {
        f: common_vendor.f(common_vendor.unref(list), (item, index, i0) => {
          return {
            a: `${common_vendor.unref(config_index.filePrefix)}${item.avatar_id}`,
            b: common_vendor.t(item.role_name),
            c: common_vendor.t(item.world_scenario),
            d: index,
            e: common_vendor.o(($event) => isExistChat(item))
          };
        }),
        g: showDialog.value
      }, showDialog.value ? {
        h: `${common_vendor.unref(config_index.filePrefix)}${currentItem.value.avatar_id}`,
        i: common_vendor.t(currentItem.value.role_name),
        j: common_vendor.t(currentItem.value.description),
        k: common_vendor.o(($event) => toCall(currentItem.value)),
        l: common_vendor.o(($event) => toChat(currentItem.value)),
        m: common_vendor.o(() => {
        }),
        n: common_vendor.o(($event) => showDialog.value = false)
      } : {});
    };
  }
};
var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-57280228"], ["__file", "C:/Users/bzm15/Desktop/ix/mini_web-develop/pages/index/index.vue"]]);
wx.createPage(MiniProgramPage);
