"use strict";

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

import { calculateSumOfObservationValuesWithinInterval } 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} 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 sum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailySumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Calculate SUM / DAILY of values of observations
  const observationsSumDailyNestedArr =
    calculateSumOfObservationValuesWithinInterval(
      observationsNestedArr,
      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} 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 sum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlySumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

  // Calculate SUM / MONTHLY of values of observations
  const observationsSumMonthlyNestedArr =
    calculateSumOfObservationValuesWithinInterval(
      observationsNestedArr,
      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,
  ];
};

export {
  calculateAndFormatDailySumObservations,
  calculateAndFormatMonthlySumObservations,
};
