import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', () => {
  const token = ref(localStorage.getItem('token') || '')
  const userInfo = ref(
    localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {},
  )
  const setToken = (t) => {
    token.value = t
    localStorage.setItem('token', t)
  }
  const setUserInfo = (u) => {
    userInfo.value = u
    localStorage.setItem('userInfo', JSON.stringify(u))
  }
  const logout = () => {
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
    token.value = ''
    userInfo.value = {}
  }
  return { token, setToken, setUserInfo, userInfo, logout }
})
