import { defineStore } from 'pinia'
import { getCurrentLang } from '@/locale/index'



export interface ISystemState {
  locale: string;
  localeList: ILocale[];
}


export interface ILocale {
  name: string;
  value: string;
}




const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    locale: getCurrentLang() as string,
    localeList: [
      { name: '简体中文', value: 'zh-CN' },
      { name: 'English', value: 'en' },
    ]
  }),

  getters: {
    localeInfo: function (): ILocale {
      const result = this.localeList.find((item: ILocale) => (item.value == this.locale))
      return result ? result : this.localeList[0]
    }
  },

  actions: {
    toggleLocale(localeInfo: ILocale) {
      if (localeInfo) {
        this.locale = localeInfo.value
        localStorage.setItem('locale', localeInfo.value)
      }
    }
  }
})



export default useSystemStore