import axios from 'axios';
import { ref } from 'vue';
import { showError } from '@/lib/shared/message/functions';
import { useAppRoute } from './useAppRoute';

const isLoadingLogout = ref(false);

export const authService = {
  
  logout: async (errorMsg?: string) => {
    const route = useAppRoute();
    if (isLoadingLogout.value) return;
    isLoadingLogout.value = true;

    try {
      await axios.post(route('logout'));
      window.location.href = '/';
    } catch (error) {
      if (errorMsg) showError(errorMsg);
      else showError('Une erreur est survenue lors de la déconnexion.');
    } finally {
      // isLoadingLogout.value = false;
    }
  },

  isLoading: () => isLoadingLogout.value,
};