import { storeToRefs } from 'pinia'

import { useAuthStore } from '@/stores/auth'

export function useAuth() {
  const router = useRouter()

  const authStore = useAuthStore()
  const { isLogin, user } = storeToRefs(authStore)
  const loading = ref(false)

  function logout() {
    authStore.logout()
    router.push({ path: '/auth/sign-in' })
  }

  function toHome() {
    router.push({ path: '/dashboard' })
  }

  async function login() {
    loading.value = true
    await new Promise(resolve => setTimeout(resolve, 10))
    isLogin.value = true
    loading.value = false

    const redirect = router.currentRoute.value.query.redirect as string
    if (!redirect || redirect.startsWith('//')) {
      toHome()
    }
    else {
      router.push(redirect)
    }
  }

  return {
    loading,
    logout,
    login,
    isLogin,
    user,
  }
}
