"use strict";

/**
 * Create a temporal filter string for the fetched Observations
 * @param {String} dateStart Start date in YYYY-MM-DD format
 * @param {String} dateStop Stop date in YYYY-MM-DD format
 * @returns {String} Temporal filter string
 */
const createTemporalFilterString = function (dateStart, dateStop) {
  if (!dateStart || !dateStop) return;
  return `resultTime ge ${dateStart}T00:00:00.000Z and resultTime le ${dateStop}T00:00:00.000Z`;
};

/**
 * Create a query parameter object that should be sent together with a HTTP GET request using the Axios library
 * @param {String} dateStart Start date (for temporal filter) in YYYY-MM-DD format
 * @param {String} dateStop Stop date (for temporal filter) in YYYY-MM-DD format
 * @returns {Object} A query parameter object
 */
const createUrlParametersForGetRequest = function (dateStart, dateStop) {
  const QUERY_PARAM_RESULT_FORMAT = "dataArray";
  const QUERY_PARAM_ORDER_BY = "phenomenonTime asc";
  const QUERY_PARAM_FILTER = createTemporalFilterString(dateStart, dateStop);
  const QUERY_PARAM_SELECT = "result,phenomenonTime";

  return {
    "$resultFormat": QUERY_PARAM_RESULT_FORMAT,
    "$orderBy": QUERY_PARAM_ORDER_BY,
    "$filter": QUERY_PARAM_FILTER,
    "$select": QUERY_PARAM_SELECT,
  };
};

export const QUERY_PARAMS_COMBINED = createUrlParametersForGetRequest(
  "2020-01-01",
  "2021-01-01"
);
