#' Return the longname of the variable.
#' The function attempts to retrieve the longname from the 
#' modules/Visualization/var_long_names.yml configuration file,
#' using the entry specified in 'format'.
#' If the file is not found or does not contain the expected data,
#' the longname from the s2dv_cube is used instead.
#'
#'@param data_cube the s2dv_cube object or its metadata, containing the 
#'  necessary information.
#'@param index The index for the variable
#'@param format Character string indicating the name of the format key.
#'
#'@return Character string of the variable longname
.get_variable_longname <- function(data_cube, index, format = NULL) {
  var_name <- data_cube$attrs$Variable$varName[[index]]
  var_long_name <- tryCatch(
    expr = {
      dictionary <- read_yaml("modules/Visualization/var_long_names.yml")
      dictionary_index <- grep(format, names(dictionary),
                               ignore.case = TRUE,
                               value = TRUE)
      var_long_name <- dictionary[[dictionary_index]][[var_name]]
      return(var_long_name)
    },
    error = function(e) {
      var_long_name <- str_to_title(data_cube$attrs$Variable$metadata[[var_name]]$long)
      warning("No configuration file found for this variable long name. Using s2dv_cube metadata.")
      return(var_long_name)
    }
  )
  return(var_long_name)
}
