import { defineStore } from 'pinia'
import { useLocalStorageState } from 'vue-hooks-plus'
import { ConstantLocalStorage, ConstantStore } from '@/utils/constant'
import { reactive, watch, toRefs } from 'vue'

const initState:API.User = {
  token: '',
  wisdomCoin: 0
}


export const useUserStore = defineStore(ConstantStore.USER, () => {

  let appUser = reactive<API.User>({...initState})

  const [ user, setUser ] = useLocalStorageState<API.User>(ConstantLocalStorage.USER, {
    defaultValue: initState!
  })

  appUser = user.value!

  watch(
    () => appUser.wisdomCoin,
    () =>  setUser(appUser)
  )


  return toRefs(appUser)

})