import axios from 'axios'
import User, { HasInitials } from '../types/User'
import { Manager } from '@/types/Manager'

const axiosInstance = axios.create({
  withCredentials: true,
})

export class UserService {
  loadUser(baseUrl: string = ''): Promise<User | Manager> {
    return axiosInstance
      .get(`${baseUrl}/api/user`)
      .then(response => response.data.user)
  }
  logout(): Promise<{ url: string }> {
    return axiosInstance
      .get('/logout')
      .then(response => ({ url: response.data.url }))
  }
  getUserInitials(user: HasInitials): string {
    return `${user.lastName ? user.lastName[0] : ''}${
      user.firstName ? user.firstName[0] : ''
    }`.toUpperCase()
  }
  getUserFullName(user: HasInitials): string {
    return `${user.lastName} ${user.firstName}`
  }
}

export default new UserService()
