import { RouteLocationNormalized } from 'vue-router'
import { router } from '@/router'
import { useUserStore } from '@/store'

export default function useUser(currentRuter: RouteLocationNormalized) {
  const userStore = useUserStore()
  const logout = async (logoutTo?: string) => {
    await userStore.logout()
    router.push({
      name: logoutTo && typeof logoutTo === 'string' ? logoutTo : 'login',
      query: {
        redirect:
          currentRuter?.name !== 'login'
            ? (currentRuter?.fullPath as string)
            : undefined
      }
    })
  }
  return {
    logout
  }
}
