import { defineStore } from 'pinia'
import { store } from '@/store'
import { getFilter } from '@/api/home/filter'

export interface ConfigState {
  customConfig: string[] | null
  asideValue: any
}

export const useFinalStore = defineStore({
  id: 'app-final',
  state: (): ConfigState => ({
    customConfig: null,
    asideValue: null,
  }),
  getters: {
    getCustomConfig(): string[] | null {
      return this.customConfig
    },
    getAsideValue(): any {
      return this.asideValue
    },
  },
  actions: {
    setAsideValue(value) {
      this.asideValue = value
    },
    // 获取终审个性化配置
    async fetchCustomConfig() {
      const res = await getFilter(1)
      const { data } = res
      const list = data && data.searchcount ? data.searchcount.split(',') : []
      this.customConfig = list
      return list
    },
  },
})

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