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

#' Barrier Query
#' 
#' retrieve Barrier ID according to its name in the barrier database
#' @param config list configuration file default value is config_ambre dataset
#' @param barrierName character Name of the barrier you want to find the ID
#' @param na.rm boolean FALSE by default
#' @importFrom dplyr filter pull
#' @importFrom purrr map
#' @return barrierID
#' 
#' @export
#' @examples
#' query_barrier(barrierName = "Q.1 - Activated Sludge")
#' query_barrier(barrierName = c("Q.1 - Activated Sludge", "Q.2 - Maturation Pond"))
#' query_barrier(barrierName = "P.9 - Peeling, P.7 - Cooking")
query_barrier <- function(config = config_ambre, barrierName, na.rm = FALSE){
  
  if (length(barrierName) == 1) {
    if (grepl(",", barrierName)) {
      barrierName <- trimws(strsplit(barrierName, ",")[[1]])
    } else {
      barrierName <- trimws(barrierName)
    }
  }
  
  # Gestion des NA
  if (any(is.na(barrierName))) {
    if (na.rm) {
      barrierName <- barrierName[!is.na(barrierName)]
      if (length(barrierName) == 0) {
        return(NA)
      }
    } else {
      return(NA)
    }
  }
  
  allowed_barrier <- c("Q.1 - Activated Sludge", 
                        "Q.2 - Maturation Pond", 
                        "Q.3 - UV Reactor",  
                        "Q.4 - Sand Filter and UV", 
                        "Q.5 - Microfiltration and UV",  
                        "Q.6 - Chlorination",   
                        "Q.7 - Microfiltration and UV and Chlorination", 
                        "P.1 - Non-edilble crop",
                        "P.2 - Distance of 70m", 
                        "P.3 - Night-time irrigation",  
                        "P.4 - Shutdown irrigation in strong winds", 
                        "P.5 - Natural die-off",  
                        "P.6 - Agri-food processing", 
                        "P.7 - Cooking",
                        "P.8 - Drying", 
                       "P.9 - Peeling", 
                        "P.10 - Rinsing with drinking water", 
                        "P.11 - Washing with disinfectant", 
                        "E.1 - Automatic irrigation",  
                        "E.2 - Mechanised crop maintenance", 
                        "E.3 - Mechanised harvesting", 
                        "E.4 - Signage",
                        "E.5 - Fences", 
                        "E.7 - Sheet mulching", 
                        "E.8 - Personnal Protective Equipement", 
                        "E.1.1 - Micro-sprinkler",
                        "E.1.2 - Surface drip irrigation",  
                        "E.1.3 - Subsurface drip irrigation"
                        )
  if (any(!barrierName %in% allowed_barrier)) {
    stop("Barrier name doesn't exist")}
  
  barrierID <- config_ambre$treatment$processes |> 
                filter(TreatmentName %in% barrierName) |> 
                pull(TreatmentID) |> 
                unique()
  
  return(barrierID)
    
}
