"use strict";

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

import {
  calculateMinimumObservationValuesWithinInterval,
  calculateMaximumObservationValuesWithinInterval,
  calculateSumOfObservationValuesWithinInterval,
  calculateAverageOfObservationValuesWithinInterval,
} from "./aggregate.mjs";

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

/**
 * Calculate the daily sum 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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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 sum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailySumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Calculate SUM / DAILY of values of observations
  const observationsSumDailyNestedArr =
    calculateSumOfObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      "daily"
    );

  // Format the observations
  const formattedObservationsSumDailyNestedArr =
    observationsSumDailyNestedArr.map((obsSumDailyArr, i) =>
      formatAggregationResultForChart(
        uniqueCalendarDatesNestedArr[i],
        obsSumDailyArr
      )
    );

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

  return [
    formattedObservationsSumDailyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the monthly sum 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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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 sum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlySumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

  // Calculate SUM / MONTHLY of values of observations
  const observationsSumMonthlyNestedArr =
    calculateSumOfObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarMonthsNestedArr,
      "monthly"
    );

  // Format the observations
  const formattedObservationsSumMonthlyNestedArr =
    observationsSumMonthlyNestedArr.map((obsSumMonthlyArr, i) =>
      formatAggregationResultForChart(
        uniqueCalendarMonthsNestedArr[i],
        obsSumMonthlyArr
      )
    );

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

  return [
    formattedObservationsSumMonthlyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the daily maximum 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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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 maximum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailyMaximumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Calculate MAXIMUM / DAILY of values of observations
  const observationsMaximumDailyNestedArr =
    calculateMaximumObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      "daily"
    );

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

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

  return [
    formattedObservationsMaximumDailyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the monthly maximum 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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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 maximum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlyMaximumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

  // Calculate MAXIMUM / MONTHLY of values of observations
  const observationsMaximumMonthlyNestedArr =
    calculateMaximumObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarMonthsNestedArr,
      "monthly"
    );

  // Format the observations
  const formattedObservationsMaximumMonthlyNestedArr =
    observationsMaximumMonthlyNestedArr.map((obsMaxMonthlyArr, i) =>
      formatAggregationResultForChart(
        uniqueCalendarMonthsNestedArr[i],
        obsMaxMonthlyArr
      )
    );

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

  return [
    formattedObservationsMaximumMonthlyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * 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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Calculate MINIMUM / DAILY of values of observations
  const observationsMinimumDailyNestedArr =
    calculateMinimumObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

  // Calculate MINIMUM / MONTHLY of values of observations
  const observationsMinimumMonthlyNestedArr =
    calculateMinimumObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      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,
  ];
};

/**
 * 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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Calculate AVERAGE / DAILY of values of observations
  const observationsAverageDailyNestedArr =
    calculateAverageOfObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

  // Calculate AVERAGE / MONTHLY of values of observations
  const observationsAverageMonthlyNestedArr =
    calculateAverageOfObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      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 {
  calculateAndFormatDailySumObservations,
  calculateAndFormatMonthlySumObservations,
  calculateAndFormatDailyMaximumObservations,
  calculateAndFormatMonthlyMaximumObservations,
  calculateAndFormatDailyMinimumObservations,
  calculateAndFormatMonthlyMinimumObservations,
  calculateAndFormatDailyAverageObservations,
  calculateAndFormatMonthlyAverageObservations,
};
