"use strict";

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

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

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

/**
 * 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} 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 maximum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailyMaximumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Calculate MAXIMUM / DAILY of values of observations
  const observationsMaximumDailyNestedArr =
    calculateMaximumObservationValuesWithinInterval(
      observationsNestedArr,
      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} 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 maximum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlyMaximumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

  // Calculate MAXIMUM / MONTHLY of values of observations
  const observationsMaximumMonthlyNestedArr =
    calculateMaximumObservationValuesWithinInterval(
      observationsNestedArr,
      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,
  ];
};

export {
  calculateAndFormatDailyMaximumObservations,
  calculateAndFormatMonthlyMaximumObservations,
};
