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

#' Remove Columns from a Data Frame
#' 
#' This function does the same thing as kwb.utils::catIf(),
#' but it is bundled with *ambre* to avoid a hard dependency on the 
#' unmaintained package.
#' 
#' @param dframe data frame,
#' @param columns vector of column names giving the columns to remove
#' @param columnsToRemove deprecated. Use argument \code{columns} instead.
#' @param pattern regular expression matching the names of the columns to be
#'   removed. Will only be evaluated if no explicit column names are given in 
#'   \code{columns}.
#' @param drop if FALSE, a data frame is returned in any case, otherwise the
#'   result may be a vector if only one column remains
#' @param dbg if \code{TRUE} (the default is \code{FALSE}), the deletion of
#'   columns is reported on the screen
#' @return \emph{dframe} with columns given in \emph{columns} being removed.
#'   User attributes of \emph{dframe} are restored.
#' @references
#'   Sonnenberg H (2024). kwb.utils: General Utility Functions Developed at KWB. R package version 0.15.0
#'   URL: \url{https://github.com/kwb-r/kwb.utils}
#'   (MIT Licence)
#' @noRd
#' 
#' @examples
#' # Example usage of removeColumns
#' df <- data.frame(
#'   a = 1:3,
#'   b = letters[1:3],
#'   c = c(TRUE, FALSE, TRUE)
#' )
#'
#' # Remove column 'b' using explicit column name
#' df_no_b <- removeColumns(df, columns = "b")
#'
#' # Remove columns matching pattern 'a' or 'c'
#' df_no_a_c <- removeColumns(df, pattern = "^[ac]$")
#'
#' # Print results
#' print("Original data frame:")
#' print(df)
#'
#' print("Data frame with column 'b' removed:")
#' print(df_no_b)
#'
#' print("Data frame with columns 'a' and 'c' removed:")
#' print(df_no_a_c)
removeColumns <- function(
  dframe, columns = NULL, columnsToRemove = NULL, pattern = NULL, drop = FALSE,
  dbg = FALSE
)
{ 
  if (is.null(columns) && ! is.null(columnsToRemove)) {
    
    warning(
      "The argument 'columnsToRemove' is deprecated. Please use the new ", 
      "argument 'columns' instead.", call. = FALSE
    )
    
    columns <- columnsToRemove
  }
  
  all_columns <- names(dframe)
  
  if (is.null(columns)) {
    
    if (is.null(pattern)) {
      
      stop(
        "Either 'columns' or 'pattern' must be given to removeColumns()",
        call. = FALSE
      )
      
    } else {
      
      columns <- grep(pattern, all_columns, value = TRUE)
    }
  }
  
  columns_keep <- setdiff(all_columns, columns)
  
  if (dbg) {
    
    columns_remove <- intersect(all_columns, columns)
    
    cat(sprintf(
      "Removing column%s %s from '%s'.\n", 
      ifelse(length(columns_remove) > 1, "s", ""), stringList(columns_remove), 
      deparse(substitute(dframe))
    ))
  }
  
  hsRestoreAttributes(dframe[, columns_keep, drop = drop], attributes(dframe))
}
