#' Download data sources from the World Bank API
#'
#' This function returns a tibble of supported data sources for querying the
#' World Bank API. The data sources include various databases and datasets
#' provided by the World Bank.
#'
#' @param language A character string specifying the language code for the API
#'  response (default is "en" for English).
#'
#' @return A tibble with six columns:
#' \describe{
#'   \item{source_id}{An integer identifier for the data source.}
#'   \item{source_code}{A character string for the source code.}
#'   \item{source_name}{The name of the data source (e.g., "World Development
#'                      Indicators").}
#'   \item{update_date}{The last update date of the data source.}
#'   \item{is_data_available}{A boolean indicating whether data is available.}
#'   \item{is_metadata_available}{A boolean indicating whether metadata is
#'                                available.}
#'   \item{concepts}{The number of concepts defined for the data source.}
#' }
#'
#' @details This function provides a reference for the supported data sources
#' and their metadata when querying the World Bank API. The columns
#' `is_data_available` and `is_metadata_available` are logical values
#' derived from the API response, where "Y" indicates availability.
#'
#' @source https://api.worldbank.org/v2/sources
#'
#' @export
#'
#' @examplesIf curl::has_internet()
#' # Download all available data sources in English
#' wdi_get_sources()
#'
wdi_get_sources <- function(language = "en") {
  sources_raw <- perform_request("sources", language)

  # url and description are always empty, hence omitted
  if (!is.null(sources_raw)) {
    sources_processed <- as_tibble(sources_raw) |>
      select(
        source_id = "id",
        source_code = "code",
        source_name = "name",
        update_date = "lastupdated",
        is_data_available = "dataavailability",
        is_metadata_available = "metadataavailability",
        concepts = "concepts"
      ) |>
      mutate(
        across(c("is_data_available", "is_metadata_available"), ~ . == "Y"),
        update_date = as.Date(.data$update_date),
        across(c("source_id", "concepts"), as.integer),
        across(where(is.character), trimws)
      )

    sources_processed
  }
}
