#' @title Bootstrap estimates of the diversity indices
#'
#' @description Computes bootstrap standard error and confidence interval of the diversity indices
#'
#' @param x Vector of dimension S (number of species) with the number of individuals observed in each species. NA values are allowed. 0 values are converted to NA.
#' @param ind Index to be computed. The default value is "shannon". Other possible values are: "simpson_E", "simpson_D", "menhinick", "margalef", "evenness", "equitability" and "dominance".
#' @param B Number of bootstrap samples. The default is 1000.
#' @param cl Confidence level. A value between 0 and 1. The default is 0.95.
#'
#' @return
#' \itemize{
#'   \item \code{s}: Boostrap standard error.
#'   \item \code{low}: Lower limit of the confidence interval.
#'   \item \code{up}: Upper limit of the confidence interval.
#' }
#'
#' @references
#' "Quantifying Diversity through Entropy Decomposition: Insights into Hominin Occupation and Carcass Processing at Qesem cave"
#'
#' @examples
#' data(Qesem_s)
#' bs(Qesem_s$HU)
#'
#' @export
bs <- function(x, ind = "shannon", B = 1000, cl = 0.95){
  x <- check_x(x)
  stopifnot(is.numeric(B),
            B==round(B),
            cl>0 & cl<1,
            ind %in% c("shannon", "simpson_E", "simpson_D", "menhinick",
                       "margalef", "evenness", "equitability", "dominance")
            )
  n <- sum(x)
  p <- x/n
  m <- length(x)
  y <- numeric(B)
  for (i in 1:B){
    xx <- sample(1:m, n, replace = T, prob = p)
    xb <- as.vector(table(xx))
    y[i] <- switch(ind,
                   shannon = shannon(xb),
                   simpson_E = simpson_E(xb),
                   simpson_D = simpson_D(xb),
                   menhinick = menhinick(xb),
                   margalef = margalef(xb),
                   evenness = evenness(xb),
                   equitability = equitability(xb),
                   dominance = dominance(xb)
                   )
  }
  q <- stats::quantile(y, probs = sort(c(1-cl, cl)))
  return(list(s = stats::sd(y),
              low = as.numeric(q[1]),
              up = as.numeric(q[2])
              )
         )
}
