# WARNING - Generated by {fusen} from dev/flat_rename_r.Rmd: do not edit by hand

#' Create an excel mask compatible with the rename_r() function. 
#' 
#' Create an excel mask compatible with the rename_r() function. This must be used on a collection of files, i.e stored within the same folder. 
#' 
#' @param input_path Character 1L. Folder containing the set of files to rename
#' @param output_filename Character 1L. File name of the excel mask.
#'
#' @return No return value, called for side effects. An Excel mask file
#'   is written to \code{input_path}.
#' 
#' @importFrom magrittr %>% 
#' @export
#' @examples
#' library(magrittr)
#' data(cars)
#' data(mtcars)
#'
#' mydir <- tempfile()
#' dir.create(mydir)
#'
#' saveRDS(cars, file.path(mydir, "cars.rds"))
#' saveRDS(mtcars, file.path(mydir, "mtcars.rds"))
#'
#' list.files(mydir)
#'
#' mask_rename_r(input_path = mydir)
#'
#' list.files(mydir)
#'
#' readxl::read_xlsx(file.path(mydir, "mask_rename_r.xlsx"))
#'
#' unlink(mydir, recursive = TRUE)
mask_rename_r <- function(input_path, 
                          output_filename = "mask_rename_r.xlsx"){
    
  if (! dir.exists(input_path)) {
    stop("input_path doesn't exist")
  }
  
  if (length(list.files(input_path)) < 2) {
    stop("input_path must contains at least two files. Renaming a single file with this function is not worth it.")
  }
  
  if (output_filename %in% list.files(input_path)) {
    
    stop(glue::glue("There is apparently already a mask in your folder: {output_filename}. Maybe you should complete it and run the rename_r() function."))
  }
  
  mask <- data.frame(file = list.files(input_path), 
                     renamed_file = NA, 
                     to_rename = NA)
  
  writexl::write_xlsx(
    mask, 
    path = file.path(input_path, output_filename))
}
