#' Weighting function for the phase–function estimator
#'
#' Computes the weighting function \eqn{K(t)} used within the
#' \code{computeD()} objective for the phase–function estimator.
#' The weight controls how much influence each frequency value receives
#' when integrating the squared phase–difference between \eqn{Y}
#' and the linear predictor \eqn{Wb}.
#'
#' The user may choose between several polynomial or absolute–value kernels
#' (via \code{type}), or a Gaussian kernel (via \code{delta}). If
#' \code{delta} is supplied, the Gaussian weight is used regardless of
#' \code{type}.
#'
#' @param t Numeric vector of frequency values, usually normalised to lie
#'   in \eqn{[0, 1]}.
#' @param type Character string specifying the weighting kernel:
#'   \itemize{
#'     \item \code{"quadratic"}: \eqn{(1 - t)^2}
#'     \item \code{"triangular"}: \eqn{1 - |t|}
#'     \item \code{"sqrt"}: \eqn{1 - \sqrt{t}}
#'     \item \code{"parabolic"}: \eqn{1 - t^2}
#'   }
#'   Ignored if \code{delta} is supplied.
#'
#' @param delta Optional numeric value. When supplied, a Gaussian weight
#'   \eqn{\exp(-\delta t^2 / 2)} is used. This may offer improved numerical
#'   stability for noisy empirical characteristic functions.
#'
#' @returns A numeric vector of weights of the same length as \code{t}.
#'
#' @noRd

omega <- function(t, type = c("quadratic", "triangular", "sqrt", "parabolic"),
                  delta = NULL) {

  # Gaussian kernel overrides other choices
  if (!is.null(delta)) {
    return( exp(-0.5 * delta * t^2) )
  }

  # Match type safely
  type <- match.arg(type)

  if (type == "quadratic") {
    return((1 - t)^2)

  } else if (type == "triangular") {
    return(1 - abs(t))

  } else if (type == "sqrt") {
    return(1 - sqrt(t))

  } else if (type == "parabolic") {
    return(1 - t^2)
  }
}
