import { defineStore } from "pinia";
import { getConfig } from "@/api/app";

interface AppSate {
  config: Record<string, any>;
}
export const useAppStore = defineStore({
  id: "appStore",
  state: (): AppSate => ({
    config: {},
  }),
  getters: {
    getWebsiteConfig: (state) => state.config.website || {},
    getLoginConfig: (state) => state.config.login || {},
    getBulletinConfig: (state) => state.config.systemAnnouncement || {},
    getTabbarConfig: (state) => state.config.tabbar || [],
    getStyleConfig: (state) => state.config.style || {},
    getH5Config: (state) => state.config.h5 || {},
    getIsShowVip: (state) => state.config?.memberPackageStatus,
    getChatConfig: (state) => state.config.chat || {},
    getShareConfig: (state) => state.config.share || {},
    getIsShowRecharge: (state) => state.config.rechargePackageStatus || false,
    getDrawConfig: (state) => state.config.draw || {},
    getCardCodeConfig: (state) => state.config.cardCode || {},
    getDrawSquareConfig: (state) => state.config.square || {},
    getMindMapConfig: (state) => state.config.mindmap || {},
    getIsVoiceOpen: (state) =>
      !!state.config.voice?.voice_broadcast?.is_open || false,
    getIsVoiceTransfer: (state) =>
      !!state.config.voice?.voice_input?.is_open || false,
    getIsVoiceChat: (state) =>
      !!state.config.voice?.voice_chat?.is_open || false,
  },
  actions: {
    getImageUrl(url: string) {
      const imgUrl = url.indexOf("http") ? `${this.config.domain}${url}` : url;
      return imgUrl;
    },
    async getConfig() {
      const data = await getConfig({ type: 1 });
      this.config = data;
    },
  },
});
