## The version is read from the installed DESCRIPTION, so it cannot go stale.
## The year is looked up the way utils::citation() does it: the CRAN
## publication date, then a Date field (DESCRIPTION has none), then the date
## R CMD build packaged the source, which a GitHub install normally has since
## remotes and pak build the package before installing it. Only an install
## straight from a source directory records none of them and falls back to
## the current year. [[ ]] matches exactly: meta$Date would silently pick up
## Date/Publication by partial matching.
year <- NA_character_
for (field in c("Date/Publication", "Date", "Packaged")) {
  value <- trimws(as.character(meta[[field]]))[1L]
  if (!is.na(value) && grepl("^[0-9]{4}-", value)) {
    year <- substr(value, 1L, 4L)
    break
  }
}
if (is.na(year)) year <- format(Sys.Date(), "%Y")

mheader <- paste(
  "To cite spatialkit in publications, use the first entry. spatialkit",
  "implements published methods; if you rely on one of them, cite its paper",
  "as well. Each entry after the first names the function it goes with."
)

c(
  bibentry(
    bibtype  = "Manual",
    title    = paste0("spatialkit: ", meta$Title),
    author   = person("Justin", "Chase", email = "jchase.msu@gmail.com"),
    year     = year,
    note     = paste("R package version", meta$Version),
    url      = "https://github.com/elkronos/gis_modeling_toolkit",
    mheader  = mheader
  ),
  bibentry(
    bibtype  = "Article",
    header   = "area_of_applicability():",
    author   = c(person("H.", "Meyer"), person("E.", "Pebesma")),
    title    = paste("Predicting into unknown space? Estimating the area of",
                     "applicability of spatial prediction models"),
    journal  = "Methods in Ecology and Evolution",
    year     = "2021",
    volume   = "12",
    number   = "9",
    pages    = "1620--1633",
    doi      = "10.1111/2041-210X.13650"
  ),
  bibentry(
    bibtype  = "Article",
    header   = "make_folds(method = \"nndm\"):",
    author   = c(person("C.", "Mil\u00e0"), person("J.", "Mateu"),
                 person("E.", "Pebesma"), person("H.", "Meyer")),
    title    = paste("Nearest neighbour distance matching leave-one-out",
                     "cross-validation for map validation"),
    journal  = "Methods in Ecology and Evolution",
    year     = "2022",
    volume   = "13",
    number   = "6",
    pages    = "1304--1316",
    doi      = "10.1111/2041-210X.13851"
  ),
  bibentry(
    bibtype  = "Article",
    header   = "make_folds(method = \"knndm\"):",
    author   = c(person("J.", "Linnenbrink"), person("C.", "Mil\u00e0"),
                 person("M.", "Ludwig"), person("H.", "Meyer")),
    title    = paste("kNNDM CV: k-fold nearest-neighbour distance matching",
                     "cross-validation for map accuracy estimation"),
    journal  = "Geoscientific Model Development",
    year     = "2024",
    volume   = "17",
    pages    = "5897--5912",
    doi      = "10.5194/gmd-17-5897-2024"
  ),
  bibentry(
    bibtype  = "Article",
    header   = "fit_bayesian_spatial_model(), its Hilbert space approximation:",
    author   = c(person("G.", "Riutort-Mayol"),
                 person("P.-C.", "B\u00fcrkner"),
                 person(c("M.", "R."), "Andersen"), person("A.", "Solin"),
                 person("A.", "Vehtari")),
    title    = paste("Practical Hilbert space approximate Bayesian Gaussian",
                     "processes for probabilistic programming"),
    journal  = "Statistics and Computing",
    year     = "2023",
    volume   = "33",
    pages    = "17",
    doi      = "10.1007/s11222-022-10167-2"
  ),
  bibentry(
    bibtype  = "Article",
    header   = "fit_rf_model(), its permutation importance:",
    author   = c(person("C.", "Strobl"), person("A.-L.", "Boulesteix"),
                 person("A.", "Zeileis"), person("T.", "Hothorn")),
    title    = paste("Bias in random forest variable importance measures:",
                     "illustrations, sources and a solution"),
    journal  = "BMC Bioinformatics",
    year     = "2007",
    volume   = "8",
    pages    = "25",
    doi      = "10.1186/1471-2105-8-25"
  ),
  bibentry(
    bibtype  = "Article",
    header   = "fit_rf_model(include_coords = ) and cv_rf():",
    author   = c(person("H.", "Meyer"), person("C.", "Reudenbach"),
                 person("S.", "W\u00f6llauer"), person("T.", "Nauss")),
    title    = paste("Importance of spatial predictor variable selection in",
                     "machine learning applications - moving from data",
                     "reproduction to spatial prediction"),
    journal  = "Ecological Modelling",
    year     = "2019",
    volume   = "411",
    pages    = "108815",
    doi      = "10.1016/j.ecolmodel.2019.108815"
  ),
  bibentry(
    bibtype  = "Article",
    header   = "fit_gwr_model() and gwr_model_selection():",
    author   = c(person("B.", "Lu"), person("P.", "Harris"),
                 person("M.", "Charlton"), person("C.", "Brunsdon")),
    title    = paste("The GWmodel R package: further topics for exploring",
                     "spatial heterogeneity using geographically weighted",
                     "models"),
    journal  = "Geo-spatial Information Science",
    year     = "2014",
    volume   = "17",
    number   = "2",
    pages    = "85--101",
    doi      = "10.1080/10095020.2014.917453"
  )
)
