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

#' Check for Column Existence
#' 
#' Stops if data frame \emph{frm} does not contain all columns of which the 
#' names are given in \emph{reqCols}.
#' 
#' This function does the same thing as kwb.utils::checkForMissingColumns(),
#' but it is bundled with *ambre* to avoid a hard dependency on the
#' unmaintained package.
#' 
#' @param frm data frame
#' @param reqCols vector of names of which existence in \emph{frm} shall be
#'   checked
#' @param do.stop if TRUE, stop() is called else warning() if a column is
#'   missing
#' @param dataFrameName the name of the data frame to be shown in the error
#'   message if a column was missing
#' @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
#' @return TRUE if all required columns are available, else FALSE
#'   
#' @examples
#'
#' # Example usage of checkForMissingColumns
#' df <- data.frame(a = 1:3, b = letters[1:3], c = c(TRUE, FALSE, TRUE))
#'
#' # Check for existing columns
#' checkForMissingColumns(df, c("a", "b"))  # Should return TRUE
#'
#' # Check for non-existing columns (with warning)
#' checkForMissingColumns(df, c("a", "x"), do.stop = FALSE)
#'
#' # Check for non-existing columns (with error)
#' tryCatch({
#'   checkForMissingColumns(df, c("a", "x"), do.stop = TRUE)
#' }, error = function(e) {
#'   print("Caught expected error:")
#'   print(e$message)
#' })
checkForMissingColumns <- function(
  frm, reqCols, do.stop = TRUE, dataFrameName = deparse(substitute(frm))
)
{
  columnNames <- names(frm)
  
  missing <- reqCols[!(reqCols %in% columnNames)]
  
  if (! isNullOrEmpty(missing)) {
    
    msg <- noSuchElements(
      x = missing, 
      available = columnNames, 
      type = "column",
      sorted = TRUE,
      suffix = sprintf(" in data frame '%s'", dataFrameName)
    )

    do.call(ifelse(do.stop, "stop", "warning"), list(msg, call. = FALSE))
  }
  
  isNullOrEmpty(missing)
}
