import {useUserStore} from "@/stores/user.js";
import {
  DEFAULT_LOCALE, LOCALE_STORAGE_ACCESS_TOKEN_KEY,
  LOCALE_STORAGE_LOCALE_KEY,
  LOCALE_STORAGE_REFRESH_TOKEN_KEY
} from "@/config/index.js";
import {useRouter} from "vue-router";

export function useLogout() {
  const userStore = useUserStore()
  const router = useRouter()

  async function logout() {
    userStore.setUser({
      user: null,
      accessToken: null
    })

    localStorage.removeItem(LOCALE_STORAGE_REFRESH_TOKEN_KEY)
    localStorage.removeItem(LOCALE_STORAGE_ACCESS_TOKEN_KEY)

    await router.push({
      name: 'home',
      params: {
        locale: localStorage.getItem(LOCALE_STORAGE_LOCALE_KEY) || DEFAULT_LOCALE
      }
    })
  }

  return {
    logout
  };
}