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

#' Replicate the folder structure of a given directory
#'
#' @param dir Character 1L. Path of directory which structure will be replicated
#' @param to Character 1L. Path of an output directory in which replicated structured will be placed
#' @return No return value, called for side effects. The directory
#'   structure of \code{dir} is recreated inside the \code{to} directory.
#' @importFrom magrittr %>%
#' @export
#'
#' @examples
#' library(magrittr)
#'
#' temp_dir_to_replicate <- tempfile()
#' dir.create(temp_dir_to_replicate)
#'
#' dir.create(file.path(temp_dir_to_replicate, "dir1"))
#' dir.create(file.path(temp_dir_to_replicate, "dir2"))
#'
#' temp_dir_out <- tempfile()
#' dir.create(temp_dir_out)
#'
#' folder_structure_replicate(
#'   dir = temp_dir_to_replicate, 
#'   to = temp_dir_out)
#'
#' unlink(temp_dir_to_replicate)
#' unlink(temp_dir_out)
folder_structure_replicate <- function (dir, to) {

  requireNamespace("magrittr")

  if (isTRUE(identical(dir, to))) {
    
    stop(glue::glue("The arguments dir and to must not be identical"))
  }
  
  if (isTRUE(
    length(list.dirs(to, recursive = FALSE)) > 0)) {
    
    stop(glue::glue("For security reasons, {to} must be an empty directory"))
  }
  
  # Warnings are suppressed because of the two security checks above: 
  suppressWarnings({
    
    list.dirs(dir, full.names = FALSE) %>%
      (\(l) for (i in l)
        dir.create(file.path(to, i), recursive = TRUE))
  })
  
}
