#' @title PredictionData
#'
#' @description
#' Convert object to a [PredictionData] or a list of [PredictionData].
#'
#' @inheritParams as_task
#' @param task ([Task]).
#' @template param_row_ids
#' @param check (`logical(1)`)\cr
#'   Perform argument checks and type conversions?
#' @param ... (any)\cr
#'   Additional arguments.
#'
#' @return [PredictionData].
#' @export
as_prediction_data = function(x, task, row_ids = task$row_ids, check = TRUE, ...) {
  UseMethod("as_prediction_data")
}

#' @rdname as_prediction_data
#' @export
# nolint next
as_prediction_data.Prediction = function(x, task, row_ids = task$row_ids, check = TRUE, ...) {
  x$data
}

#' @rdname as_prediction_data
#' @export
# nolint next
as_prediction_data.PredictionData = function(x, task, row_ids = task$row_ids, check = TRUE, ...) {
  x
}

#' @rdname as_prediction_data
#' @param train_task ([Task])\cr
#'   Task used for training the learner.
#' @export
# nolint next
as_prediction_data.list = function(x, task, row_ids = task$row_ids, check = TRUE, ..., train_task) {
  assert_list(x, names = "unique")
  predict_types = names(mlr_reflections$learner_predict_types[[task$task_type]])
  if (!is.null(predict_types)) {
    predict_types = c(predict_types, "extra", "raw")
  }
  assert_names(names(x), subset.of = predict_types)

  x$row_ids = row_ids
  if (inherits(task, "TaskSupervised")) {
    x$truth = task$truth(row_ids)
  }

  if ("weights_measure" %chin% task$properties) {
    x$weights = task$weights_measure[list(row_id = row_ids), on = "row_id", "weight"][[1L]]
  }

  task = if (task$task_type == "unsupervised") train_task else task
  pdata = new_prediction_data(x, task$task_type)
  if (check) {
    pdata = check_prediction_data(pdata, train_task = task)
  }

  pdata
}
