"use strict";

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

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

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

/**
 * Calculate the daily average 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 average) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailyAverageObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Calculate AVERAGE / DAILY of values of observations
  const observationsAverageDailyNestedArr =
    calculateAverageOfObservationValuesWithinInterval(
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      "daily"
    );

  // Format the observations
  const formattedObservationsAverageDailyNestedArr =
    observationsAverageDailyNestedArr.map((obsAverageDailyArr, i) =>
      formatAggregationResultForChart(
        uniqueCalendarDatesNestedArr[i],
        obsAverageDailyArr
      )
    );

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

  return [
    formattedObservationsAverageDailyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the monthly average 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 average) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlyAverageObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

  // Calculate AVERAGE / MONTHLY of values of observations
  const observationsAverageMonthlyNestedArr =
    calculateAverageOfObservationValuesWithinInterval(
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarMonthsNestedArr,
      "monthly"
    );

  // Format the observations
  const formattedObservationsAverageMonthlyNestedArr =
    observationsAverageMonthlyNestedArr.map((obsAverageMonthlyArr, i) =>
      formatAggregationResultForChart(
        uniqueCalendarMonthsNestedArr[i],
        obsAverageMonthlyArr
      )
    );

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

  return [
    formattedObservationsAverageMonthlyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

export {
  calculateAndFormatDailyAverageObservations,
  calculateAndFormatMonthlyAverageObservations,
};
