#' Michael & Schucany (1979) Transformation
#'
#' Transforms an r-out-of-n Type-II right-censored uniform sample into a complete sample of size r.
#'
#' @param u_cens Numeric vector of order statistics u_1:n < ... < u_r:n in (0, 1).
#' @param n Total sample size (n >= length(u_cens)).
#'
#' @return Numeric vector of length r representing the transformed complete sample Z_1:r < ... < Z_r:r.
#' @export
#'
#' @examples
#' u <- c(0.03, 0.06, 0.10, 0.11, 0.13)
#' transform_ms(u, n = 10)
transform_ms <- function(u_cens, n) {
  if (!is.numeric(u_cens) || length(u_cens) == 0) {
    stop("`u_cens` must be a non-empty numeric vector.")
  }
  r <- length(u_cens)
  if (!is.numeric(n) || length(n) != 1 || n < r) {
    stop("`n` must be an integer >= length(u_cens).")
  }

  eps <- .Machine$double.eps^0.5
  u_cens <- pmin(pmax(u_cens, eps), 1 - eps)
  u_r <- u_cens[r]

  pbeta_val <- stats::pbeta(u_r, shape1 = r, shape2 = n - r + 1)
  pbeta_val <- pmin(pmax(pbeta_val, eps), 1 - eps)

  h_rn <- (pbeta_val^(1 / r)) / u_r
  z <- u_cens * h_rn
  z_sorted <- sort(pmin(pmax(z, eps), 1 - eps))
  return(z_sorted)
}
