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



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


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' },
    ],
    screenWidth: 1920
  }),

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

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

    updateScreenWidth(width: number) {
      if (width < 325) {
        this.screenWidth = 325
      }
      else {
        this.screenWidth = width
      }
    }
  }
})



export default useSystemStore