import { $axios } from '@/plugins/axios';
import { AxiosResponse } from 'axios';

const endpoints = {
  login: 'v1/auth/login',
  logout: 'v1/auth/logout',
  me: 'v1/auth/me',
};

export default {
  login: async (
    email: string,
    password: string,
  ): Promise<[AxiosResponse?, string?]> => {
    try {
      const data = await $axios.post(endpoints.login, {
        email,
        password,
      });
      return [data, undefined];
    } catch (e: any) {
      return [undefined, e?.response?.data?.error ?? e.message];
    }
  },
  getMe: async (): Promise<[AxiosResponse?, string?]> => {
    try {
      const data = await $axios.get(endpoints.me);
      return [data, undefined];
    } catch (e: any) {
      return [undefined, e?.response?.data?.error ?? e.message];
    }
  },
  logout: async (): Promise<[AxiosResponse?, string?]> => {
    try {
      const data = await $axios.post(endpoints.logout);
      return [data, undefined];
    } catch (e: any) {
      return [undefined, e?.response?.data?.error ?? e.message];
    }
  },
};
