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

#' Apply barriers' specific log-reduction to a config
#'
#' For one or more supplementary barriers, updates the per-config log-reduction
#' according to each barrier's type: pathway-specific barriers use
#' [update_logreduction_path()], natural die-off uses
#' [update_logreduction_decay()], and product/soil-specific barriers use
#' [update_logreduction_specific_barrier()]. Barriers are applied in turn so
#' their reductions accumulate; any other barrier leaves the config unchanged.
#' Extracted from `run_qmra_initial_situation()` /
#' `run_qmra_supplementary_process()` (was an inline anonymous function).
#'
#' @param config A per-row config (list) from the scenario.
#' @param barrierID One or more supplementary barrier ids (a scalar or a vector);
#'   each is applied in turn. `NA` matches no branch and is skipped.
#' @param cropHeight The crop height.
#' @param matrixID The exposure-matrix id.
#' @param popID The exposed-population id.
#' @param decay The number of decay days (for natural die-off barriers).
#'
#' @return The updated config.
#' @export
retrieve_specific_barrier <- function(config, barrierID, cropHeight, matrixID, popID, decay) {
  # Apply each barrier in turn so their log-reductions accumulate (handles a
  # single id, a vector of ids, or NA -- NA matches no branch and is skipped).
  for (bid in barrierID) {
    if (bid %in% c(24, 26, 27, 28) && matrixID %in% c(3, 5)) {
      config <- update_logreduction_specific_barrier(
        config = config,
        cropHeight = cropHeight,
        barrierID = bid
      )
    } else if (bid %in% c(12)) {
      config <- update_logreduction_decay(
        config = config,
        cropHeight = cropHeight,
        barrierID = bid,
        popID = popID,
        nb_day_decay = decay
      )
    } else if (bid %in% c(9, 13, 14, 15, 16, 17, 18, 24, 26, 27, 28)) {
      config <- update_logreduction_path(
        config = config,
        matrixID = matrixID,
        barrierID = bid,
        popID = popID
      )
    }
  }
  config
}
