"use strict";

import {
  extractUniqueCalendarMonthsFromCalendarDates,
  formatAggregationResultForChart,
} from "./aggregateHelpers.mjs";

import { calculateMinimumObservationValuesWithinInterval } from "./aggregate.mjs";

import { extractPropertiesFromFormattedDatastreamMetadata } from "./fetchedDataProcessing.mjs";

/**
 * Calculate the daily minimum of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsNestedArr An array made up of sub-array(s) of observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (daily minimum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailyMinimumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Calculate MINIMUM / DAILY of values of observations
  const observationsMinimumDailyNestedArr =
    calculateMinimumObservationValuesWithinInterval(
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      "daily"
    );

  // Format the observations
  const formattedObservationsMinimumDailyNestedArr =
    observationsMinimumDailyNestedArr.map((obsMinDailyArr, i) =>
      formatAggregationResultForChart(
        uniqueCalendarDatesNestedArr[i],
        obsMinDailyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "daily",
      "minimum"
    );

  return [
    formattedObservationsMinimumDailyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the monthly minimum of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsNestedArr An array made up of sub-array(s) of observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (monthly minimum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlyMinimumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

  // Calculate MINIMUM / MONTHLY of values of observations
  const observationsMinimumMonthlyNestedArr =
    calculateMinimumObservationValuesWithinInterval(
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarMonthsNestedArr,
      "monthly"
    );

  // Format the observations
  const formattedObservationsMinimumMonthlyNestedArr =
    observationsMinimumMonthlyNestedArr.map((obsMinMonthlyArr, i) =>
      formatAggregationResultForChart(
        uniqueCalendarMonthsNestedArr[i],
        obsMinMonthlyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "monthly",
      "minimum"
    );

  return [
    formattedObservationsMinimumMonthlyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

export {
  calculateAndFormatDailyMinimumObservations,
  calculateAndFormatMonthlyMinimumObservations,
};
