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

#' Illustrate sources of all duplicated values of a variable or a combination of variables
#' @param data_frame Data.frame. Input data frame. Must be in the Global Environment and has a data.frame class
#' @param vars Character. Vector of variable or combination of variables
#' from which duplicates are checked
#' @param output_as_df Logical 1L. If TRUE, output is rendered as a data.frame. 
#' @return List or data.frame. For each duplicated row regarding to vars, different values of the same variable
#' are shown, separated by AND
#' @importFrom magrittr %>%
#' @export
#'
#' @examples
#' # A fictional data with duplicated values:
#' df <- data.frame("person_id" = c(1, 1, 2, 3,
#'                                  2, 4, 5, 5 ,1),
#'                  "person_age" = c(25, 25, 21, 32,
#'                                   21, 48, 50, 50, 52),
#'                  "survey_month" = c("jan", "feb", "mar", "apr",
#'                                     "apr", "may", "jun", "jul", "jan"),
#'                  "survey_answer" = c("no", "yes", "no", "yes",
#'                                      "yes", "yes", "no", "yes", NA))
#'
#' # Shuffling observations and columns to make duplicates difficult to see:
#' set.seed(1)
#' df <- df[sample(1:nrow(df)),
#'          sample(1:ncol(df))]
#' df
#'
#' dupl_sources(data_frame = df, vars = "person_id")
#' dupl_sources(data_frame = df, vars = "person_id", output_as_df = TRUE)
dupl_sources <- function (data_frame, vars, output_as_df = FALSE) {
  
  requireNamespace("magrittr")
  # Insert element-wise combination of vars input components within the data:
  # idvarsdupl
  data <- dplyr::mutate(data_frame,
                        idvarsdupl =
                          do.call(what = paste,
                                  purrr::map(vars, function (x)
                                    data_frame[[x]])))
  
  # Filter rows with values of idvarsdupl that appears
  # at least for the second time:
  data %>% dplyr::filter(duplicated(idvarsdupl)) %>%
    
    # extract idvarsdupl variable from the filtered data:
    dplyr::pull(idvarsdupl) %>%
    
    # extract all corresponding observations from data input:
    (function (p) dplyr::filter(data, idvarsdupl %in% p)) %>%
    
    # arrange values to visualize the duplicated cases:
    dplyr::arrange(!!dplyr::sym("idvarsdupl")) %>%
    
    # put vars input to first variable(s) location(s):
    dplyr::relocate(dplyr::all_of(vars)) %>%
    
    (\(d) dplyr::group_by(d, !!dplyr::sym("idvarsdupl")) %>%
       dplyr::summarise_all(\(x) stats::na.omit(unique(x)) %>%
                              paste0(collapse = " [AND] ")) %>%
       dplyr::ungroup() %>%
       apply(1, \(x) c(paste0(x[[1]], collapse = " "),
                       x[stringr::str_detect(x, " \\[AND\\] ")]))) %>%
    purrr::map(as.data.frame) -> result_list
  
  # Make result_list visually comfortable: 
  result_list <- purrr::map(result_list, \(x) {
    
    rownames(x)[1] <- paste(vars, collapse = "-")
    colnames(x) <- "values"
    x
  }) %>% (\(r) {
    stats::setNames(
      r, 
      purrr::map(
        r, 
        \(xr) xr[[1]][1] %>% 
          (\(xr2) glue::glue("Duplicate sources where {paste0(vars, collapse = '-')} equals {xr2}"))))
  })
  
  # Data.frame version of the result: 
  result_df <- result_list %>% 
    purrr::map(t) %>% purrr::map(as.data.frame) %>% 
    purrr::list_rbind() %>% magrittr::set_rownames(NULL)
  
  if (isTRUE(output_as_df)) {
    return(result_df)
  } else return(result_list)
}
