import { ApiError, request } from './client';
import type { components } from './generated/v1';

export type LanguageCode = components['schemas']['LanguageCodeResponseModel'];

export function getLanguages(): Promise<LanguageCode[]> {
  return request<LanguageCode[]>('/api/v1/languages');
}

export function messageFromLanguagesError(error: unknown, fallback = 'Unable to load languages'): string {
  if (error instanceof ApiError) {
    return error.detail ?? error.message;
  }

  if (error instanceof Error) {
    return error.message;
  }

  return fallback;
}
