#' Minkowski-based cluster-wise regression, p = 1 (internal)
#'
#' L1 case via iteratively re-weighted least squares (IRLS) with weights
#' `w_i = p_i^a * d_i^(b-1) / |residual|`.
#'
#' @noRd
PDVRegressionWithMinkowskiEqualToOne <- function(X = NULL, Y, K, a, b,
                                                 threshold, max.iter) {

  if (is.null(X)) {
    X <- matrix(1, nrow(Y), 1)
  } else {
    X <- as.matrix(X)
    X <- cbind(rep(1, nrow(X)), X)
  }
  Y <- as.matrix(Y)

  const <- b / (a - 1)

  dx <- ncol(X)
  dy <- ncol(Y)
  n  <- nrow(X)

  B    <- array(stats::runif(dy * dx * K), dim = c(dy, dx, K))
  newB <- B
  if (dx == 1) {
    B    <- array(t(stats::kmeans(Y, K)$centers), dim = c(dx, dy, K))
    newB <- B
  }

  d <- data.frame(matrix(ncol = K, nrow = n))
  p <- data.frame(matrix(0, ncol = K, nrow = n))

  for (x in 1:K) {
    names(d)[x] <- paste("Cluster", x)
    names(p)[x] <- paste("Cluster", x)
  }

  JDF     <- c()
  newNorm <- threshold + 1
  iter    <- 0

  while (newNorm >= threshold & iter < max.iter) {
    iter <- iter + 1

    for (j in 1:K) {
      for (i in 1:n) {
        resids  <- cbind(matrix(unlist(Y[i, ])),
                         B[, , j] %*% matrix(unlist(X[i, ])))
        d[i, j] <- sum(abs(resids[, 1] - resids[, 2])^1)^(1 / 1)
      }
    }

    JDF_value <- 0
    for (x in 1:n) {
      JDF_numerator   <- prod(d[x, ])^const
      JDF_denominator <- 0
      for (y in 1:K) {
        JDF_denominator <- JDF_denominator + prod(d[x, -y])^const
      }
      JDF_value <- JDF_value + (JDF_numerator / JDF_denominator)^(a - 1)
    }
    JDF <- c(JDF, JDF_value)

    for (x in 1:n) {
      sum1 <- 0
      for (y in 1:K) {
        sum1 <- sum1 + prod(d[x, -y])^const
      }
      for (z in 1:K) {
        product1 <- prod(d[x, -z])^const
        p[x, z]  <- product1 / sum1
      }
    }
    p[sapply(p, is.nan)] <- 1

    for (j in 1:K) {
      for (s in 1:dy) {
        numerator   <- 0
        denominator <- 0
        for (i in 1:n) {
          c <- 0
          for (t in 2:dx) {
            c <- c + (X[i, t] * newB[s, t, j])
          }
          abs_resid <- abs(Y[i, s] - newB[s, 1, j] - c)
          abs_resid <- max(abs_resid, 1e-18)
          w           <- (p[i, j]^a * d[i, j]^(b - 1)) / abs_resid
          numerator   <- numerator + w * (Y[i, s] - c)
          denominator <- denominator + w
        }
        newB[s, 1, j] <- numerator / denominator
      }
    }

    for (j in 1:K) {
      for (s in 1:dy) {
        for (t in 2:dx) {
          numerator   <- 0
          denominator <- 0
          for (i in 1:n) {
            c <- 0
            for (l in 2:dx) {
              c <- c + (X[i, l] * newB[s, l, j])
            }
            abs_resid <- abs(Y[i, s] - newB[s, 1, j] - c)
            abs_resid <- max(abs_resid, 1e-18)
            w  <- p[i, j]^a * d[i, j]^(b - 1) / abs_resid
            c1 <- 0
            for (l in 2:dx) {
              if (l != t) {
                c1 <- c1 + (X[i, l] * newB[s, l, j])
              }
            }
            numerator   <- numerator + w * X[i, t] * (Y[i, s] - newB[s, 1, j] - c1)
            denominator <- denominator + w * X[i, t]^2
          }
          newB[s, t, j] <- numerator / denominator
        }
      }
    }

    newNorm <- sqrt(sum(B - newB)^2)
    B <- newB
  }

  l <- apply(p, 1, which.max)
  return(list(B = B, d = d, p = p, JDF = JDF, l = l))
}
