#' Calculate the Multivariate Spectral Principal Components Filters Spatial Functional Data
#'
#' This function calculates the multivariate SPC filters from a given spectral density operator.
#' @param SM_FSD list of univariate spatial functional principal component analysis.
#' @param SM_vec list of variable eigenvectors.
#' @param J number of functional multivariate spatial principal components.
#' @return the Multivariate SPC filters.
#' @details The univariate eigenvectors and filters are used to compute multivariate spatial function filters.
#' @keywords mfsd
#' @export

mfsd_Filter = function (SM_FSD, SM_vec, J)
{
  if (!inherits(SM_FSD, "SM_FSD")) {
    stop("'SM_FSD' is not a SM_FSD type.")
  }

  if (!inherits(SM_vec, "SM_vec")) {
    stop("'SM_vec' is not a SM_vec type.")
  }


  y <- length(SM_FSD)

  SML_Filter = vector("list", y)
  oldClass(SML_Filter) <- ("fsd.filter")

  for(i in 1:y){
    SML_Filter[[i]] <- SM_FSD[[i]][["filters"]]
  }


  for (i in 1:y) {
    for (j in 1:length(SM_FSD[[i]]$filters$operators)) {
      tryCatch({
        ei = as.matrix(SM_FSD[[i]][["filters"]][["operators"]][[j]][,1:J])
        rslt <- ei %*%  SM_vec[[i]]
        SML_Filter[[i]][["operators"]][[j]] <- rslt
      }, error = function(e) {
        message("An error occurred in mfsd_Filter while processing the element ", i, ": ", conditionMessage(e), "\n")
      })
    } }

  return(SML_Filter)
}
