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.postJson('/register', { username, password })
  }

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

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

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

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

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