import { defineStore } from 'pinia'

interface UserState {
  username: string
}
export const useUserStore = defineStore('user', {
  state: (): UserState => ({
    username: localStorage.getItem('username') || ''
  }),
  getters: {},
  actions: {
    setUsername(username: any) {
      localStorage.setItem('username', username)
      this.username = username
    },
    async logout(isNeedLogout: boolean = true) {
      if (isNeedLogout) {
        await $api.logout().then(() => {})
      }
      localStorage.removeItem('username')
      this.username = ''
    }
  }
})
