pkg.env <- new.env()

.onLoad <- function(libname, pkgname) {
  suppressMessages(setAccess("public"))
  pkg.env$local_sf <- requireNamespace("sf", quietly = TRUE)
  options("dataRetrieval.nldi_base" = "https://api.water.usgs.gov/nldi/linked-data/")
  options("dataRetrieval.api_version" = "v0")
  options("dataRetrieval.api_version_stat" = "v0")
  options("dataRetrieval.attach_request" = TRUE)
  options("dataRetrieval.convertType" = TRUE)
  options("dataRetrieval.no_paging" = FALSE)
  options("dataRetrieval.site_chunk_size_meta" = 250)
  options("dataRetrieval.site_chunk_size_data" = 10)
  options("dataRetrieval.limit" = 50000)

  services <- c(
    "server",
    "daily",
    "time-series-metadata",
    "monitoring-locations",
    "latest-continuous",
    "field-measurements",
    "latest-daily",
    "continuous",
    "field-measurements-metadata",
    "combined-metadata",
    "channel-measurements",
    "peaks"
  )
  collections <- c(
    "parameter-codes",
    "agency-codes",
    "altitude-datums",
    "aquifer-codes",
    "aquifer-types",
    "coordinate-accuracy-codes",
    "coordinate-datum-codes",
    "coordinate-method-codes",
    "medium-codes",
    "counties",
    "countries",
    "hydrologic-unit-codes",
    "methods",
    "method-categories",
    "method-citations",
    "citations",
    "states",
    "national-aquifer-codes",
    "reliability-codes",
    "site-types",
    "statistic-codes",
    "topographic-codes",
    "time-zone-codes"
  )
  names(collections) <- c(
    "parameter_code",
    "agency_code",
    "altitude_datum",
    "aquifer_code",
    "aquifer_type",
    "coordinate_accuracy_code",
    "coordinate_datum_code",
    "coordinate_method_code",
    "medium_code",
    "county",
    "country",
    "hydrologic_unit_code",
    "methods",
    "method_categories",
    "method_citations",
    "citations",
    "state",
    "national_aquifer_code",
    "reliability_code",
    "site_types",
    "statistic_code",
    "topographic_code",
    "time_zone_code"
  )

  pkg.env$api_endpoints <- services
  pkg.env$metadata <- collections
}


#' Is this a dataRetrieval user
#'
#' Reveals if this is a user or not
#' @export
#' @examples
#' is_dataRetrieval_user()
is_dataRetrieval_user <- function() {
  interactive() ||
    !identical(Sys.getenv("CI"), "") ||
    identical(Sys.getenv("NOT_CRAN"), "true")
}

wqp_message <- function() {
  message(
    "NEWS: Data does not include USGS data newer than March 11, 2024. More details:
https://doi-usgs.github.io/dataRetrieval/articles/Status.html"
  )
}

wqp_message_beta <- function() {
  message("WQX3 services are in-development, use with caution.")
}

is_legacy <- function(service) {
  legacy <- service %in%
    c(
      "Result",
      "Station",
      "Activity",
      "Organization",
      "ActivityMetric",
      "SiteSummary",
      "Project",
      "ProjectMonitoringLocationWeighting",
      "ResultDetectionQuantitationLimit",
      "BiologicalMetric"
    )
  return(legacy)
}

nwis_message <- function() {
  return(
    "WARNING: whatNWISdata does not include
discrete water quality data newer than March 11, 2024.
For additional details, see:
https://doi-usgs.github.io/dataRetrieval/articles/Status.html"
  )
}

new_nwis_message <- function() {
  return(
    "ALERT: All NWIS services are slated for decommission
and new dataRetrieval functions will be added.
For up-to-date information, see: 
https://doi-usgs.github.io/dataRetrieval/articles/Status.html"
  )
}
