import { defineStore } from "pinia";
import { getUserInfoApi } from "@/api/user.js";
import Cache from "@/utils/cache.js";
import { USER_INFO,CITYINFO,TOKEN} from "@/config/cache.js";
import { uniLogin } from "@/utils/util.js";
export const useAppStore = defineStore("app", {
  state: () => {
    return {
      userInfo: Cache.get(USER_INFO) ? JSON.parse(Cache.get(USER_INFO)) : null,
      token: Cache.get(TOKEN) || "",
      agentId: '',//当前选择的智能体ID
      useBalance:false,//是否使用晓豆
      msgContent:'',//当前跳转过来带的问题
      moneyUnit:'AI豆',//默认晓豆
      title: "瑞鲸速达",//小程序名字
      statusBarHeight: 0,//状态栏高度
      navbarHeight: 0,//导航栏总高度
      bgDefaultImage:"https://fs.bjwdys.com/miniapp/bg-default.png?time="+Date.now(),//纯色背景图，带时间戳防止缓存
    };
  },
  getters: {
    $userInfo: (state) => state.userInfo,
    onShareAppMessageObj:(state) =>({
      title: "瑞鲸速达",
      path: `/pages/index/index?scene=inviteCode=${state.userInfo?.inviteCode || ''}`, // 分享路径
    })
  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    UPDATE_agentId(agentId) {
      this.agentId = agentId
    },
    UPDATE_msgContent(msgContent) {
      this.msgContent = msgContent
    },
    UPDATE_TOKEN(token) {
      this.token = token
      Cache.set(TOKEN, token);
    },
   
    LOGOUT() {
      this.token = undefined;
      this.userInfo = undefined;
      Cache.clear(USER_INFO);
      Cache.clear(TOKEN);

    },
    UPDATE_USERINFO(userInfo) {
      this.userInfo = userInfo;
      Cache.set(USER_INFO, userInfo);
      // uniLogin();
    },
    async USERINFO(force) {
      try {
        const res = await getUserInfoApi();
        this.UPDATE_USERINFO(res.user);
        return res.user;
      } catch (e) {
        // 可以处理异常
      }
    },
    UPDATE_statusBarHeight(statusBarHeight) {
      this.statusBarHeight = statusBarHeight;
      // 导航栏总高度 = 状态栏高度 + 自定义导航内容高度（通常 44px）
      this.navbarHeight = statusBarHeight + 44;
    },
  }
});
