import { ApiClient } from './base'

interface UsernameAvailability {
  available: boolean
}

export class AuthApiClient extends ApiClient {
  async login(username: string, password: string): Promise<string> {
    return await super.postText('/login', { username, password })
  }

  async register(username: string, password: string): Promise<void> {
    await super.postNoResponse('/register', { username, password })
  }

  async getUsernameAvailability(username: string): Promise<boolean> {
    const r = (await super.getJson(
      '/register/username-availability?username=' + username,
    )) as UsernameAvailability
    return r.available
  }

  getMyUser(): Promise<string> {
    return super.getText('/me')
  }

  deleteMyUser(): Promise<void> {
    return super.delete('/me')
  }

  getNewToken(): Promise<string> {
    return super.getText('/me/token')
  }

  changeMyPassword(currentPassword: string, newPassword: string): Promise<void> {
    return super.postNoResponse('/me/password', { currentPassword, newPassword })
  }
}
