import {defineStore} from 'pinia'
import {ref} from "vue";
import authApi from "app/src/api/auth/authApi";

export const useUserInfoStore =
  defineStore('userInfo', () => {
      const loginStat = ref(false)
      const info = ref({})

      async function checkLoginStat() {
        try {
          const stat = await authApi.checkLoginStatus()
          if (!stat.status) {
            clearInfo()
            return false
          }
        } catch (e) {
          console.error(e)
          return false
        }

        return true
      }

      async function logout () {
        await authApi.logout()
        clearInfo()
      }

      function clearInfo() {
        loginStat.value = false
        info.value = {}
      }

      return {loginStat, info, checkLoginStat, logout, clearInfo}
    },
    {persist: true}
  )
