#' Get or manage the worldbank API cache
#'
#' `wb_cache_dir()` returns the path where cached API responses are stored.
#' `wb_cache_clear()` clears all cached responses.
#'
#' @details
#' The cache is only used when enabled with `options(worldbank.cache = TRUE)`.
#' Cached responses are stored for 1 day by default, but this can be customized with
#' `options(worldbank.cache_max_age = seconds)`.
#'
#' @name cache
#' @returns
#' `wb_cache_dir()` returns a string with the path to the cache directory.
#'
#' `wb_cache_clear()` is called for its side effect of clearing the cached
#' responses and returns `NULL` invisibly.
#' @examples
#' \dontrun{
#' # enable caching
#' options(worldbank.cache = TRUE)
#'
#' # view cache location
#' wb_cache_dir()
#'
#' # clear the cache
#' wb_cache_clear()
#' }
NULL

#' @rdname cache
#' @export
wb_cache_dir <- function() {
  file.path(tools::R_user_dir("worldbank", "cache"), "httr2")
}

#' @rdname cache
#' @export
wb_cache_clear <- function() {
  cache_dir <- wb_cache_dir()
  if (dir.exists(cache_dir)) {
    unlink(dir(cache_dir, full.names = TRUE))
  }
  invisible()
}

#' @rdname cache
#' @export
wb_cache_delete <- function() {
  .Deprecated("wb_cache_clear")
  wb_cache_clear()
}

req_wb_cache <- function(req) {
  if (isTRUE(getOption("worldbank.cache", FALSE))) {
    req <- req_cache(
      req,
      path = wb_cache_dir(),
      max_age = getOption("worldbank.cache_max_age", 86400L) # 1 day
    )
  }
  req
}
