import { defineStore } from 'pinia'
import { store } from '@/store'
import { getConfig } from '@/api/system/user'

import { getFilter } from '@/api/home/filter'
import type { AsideConfig } from '/#/api'

export interface ConfigState {
  systemConfig: AsideConfig | null
  customConfig: any
  asideValue: any
}

export const useAsideConfigStore = defineStore({
  id: 'app-config',
  state: (): ConfigState => ({
    systemConfig: null,
    customConfig: null,
    asideValue: null,
  }),
  getters: {
    getConfig(): AsideConfig | null {
      return this.systemConfig
    },
    getCustomConfig(): any {
      return this.customConfig
    },
    getAsideValue(): any {
      return this.asideValue
    },
  },
  actions: {
    setConfig(config: AsideConfig) {
      this.systemConfig = config
    },
    setCustomConfig(config: any) {
      this.customConfig = config
    },
    setAsideValue(value) {
      this.asideValue = value
    },
    // 获取系统配置信息
    async fetchConfig() {
      const response = await getConfig()
      this.setConfig(response.data)
      return response.data
    },
    // 获取个性化配置
    async fetchCustomConfig() {
      const response = await getFilter()
      this.setCustomConfig(response.data)
      return response.data
    },
  },
})

// Need to be used outside the setup
export function useConfig() {
  return useAsideConfigStore(store)
}
