/** @format */

import { defineStore } from 'pinia'
import { useUserStore } from './user'

export const useCommonStore = defineStore(
  'commonStore',
  () => {
    const navigateTextColor = ref('#ffffff')
    const navigateBgColor = ref('#0F0820')
    const loginType = ref<string>('choice')
    const downloadCatalog = ref<any>(null)
    const accountList = ref<any>([])
    // const userStore = useUserStore()
    const loginedAccountList = computed(() => {
      // 获取已经登陆的账号列表的前两位
      return accountList.value.filter((item: any) => item.email).slice(0, 2)
    })

    /**
     * 设置头部导航背景色
     */
    const setNavigateBgColor = (color: string) => {
      if (!color)
        return
      color = color.toUpperCase()
      if (color === '#0F0820') {
        navigateTextColor.value = '#ffffff'
        navigateBgColor.value = 'rgba(15,8,32, 0.7)'
      }
      else if (color === '#F3F4FB') {
        navigateTextColor.value = '#0F0820'
        navigateBgColor.value = 'rgba(243,244,251, 0.7)'
      }
      else if (color === '#FFFFFF') {
        navigateTextColor.value = '#0F0820'
        navigateBgColor.value = 'rgba(255, 255, 255, 0.7)'
      }
    }
    /**
     * 设置已经登陆的账号列表 (在登陆成功后调用)
     */
    const pushAccount = (user: any) => {
      if (!user || !user.email)
        return
      // 检查是否已经存在相同的账号
      const existingAccount = accountList.value.find((item: any) => item.email === user.email && item.type === user.type)
      if (existingAccount)
        return
      accountList.value.push(user)
    }
    /**
     *  type 登录方式
     *
     */
    const setLoginType = (type: string) => {
      if (!type)
        return
      loginType.value = type
    }

    /**
     * 设置当前的下载的目录数据
     */
    const setDownloadCatalog = (catalog: any) => {
      if (!catalog)
        return
      downloadCatalog.value = catalog
    }

    return {
      navigateTextColor,
      navigateBgColor,
      downloadCatalog,
      setNavigateBgColor,
      setLoginType,
      accountList,
      loginType,
      pushAccount,
      loginedAccountList,
      setDownloadCatalog,
    }
  },
  {
    persist: true,
  },
)