"use strict";

const chartExportOptions = {
  buttons: {
    contextButton: {
      menuItems: ["downloadPNG", "downloadJPEG", "downloadPDF", "downloadSVG"],
    },
  },
};

/**
 * Convert a hexadecimal color code obtained from the Highcharts object (`Highcharts.getOptions().colors`) to its equivalent RGB color code
 * @param {String} hexCode Input hex color code
 * @returns {String} Output RGB color code
 */
const convertHexColorToRGBColor = function (hexCode) {
  const hexToRGBMapping = {
    "#7cb5ec": "rgb(124, 181, 236)",
    "#434348": "rgb(67, 67, 72)",
    "#90ed7d": "rgb(144, 237, 125)",
    "#f7a35c": "rgb(247, 163, 92)",
    "#8085e9": "rgb(128, 133, 233)",
    "#f15c80": "rgb(241, 92, 128)",
    "#e4d354": "rgb(228, 211, 84)",
    "#2b908f": "rgb(228, 211, 84)",
    "#f45b5b": "rgb(244, 91, 91)",
    "#91e8e1": "rgb(145, 232, 225)",
  };

  if (hexToRGBMapping?.[hexCode] === undefined)
    throw new Error(
      "The provided hex code is not valid or is not supported by this function"
    );

  // Extract the RGB color elements as a single string
  // The individual color elements are separated by commas
  return (hexToRGBMapping?.[hexCode]).slice(4, -1);
};

/**
 * Concatenates metadata properties into a single string with an ampersand as the delimiter
 * @param {Array} metadataPropertiesArr An array of metadata property strings
 * @returns {String} A string made up of combined metadata properties delimited by an ampersand
 */
const createCombinedTextDelimitedByAmpersand = function (
  metadataPropertiesArr
) {
  return metadataPropertiesArr.join(" & ");
};

/**
 * Concatenates metadata properties into a single string with a comma as the delimiter
 * @param {Array} metadataPropertiesArr An array of metadata property strings
 * @returns {String} A string made up of combined metadata properties delimited by a comma
 */
const createCombinedTextDelimitedByComma = function (metadataPropertiesArr) {
  return metadataPropertiesArr.join(", ");
};

/**
 * Extracts the sampling rate substring from a datastream name string
 * @param {Array} datastreamNamesArr An array of datastream name(s)
 * @returns {Array} An array containing the sampling rate substring(s)
 */
const extractSamplingRateFromDatastreamName = function (datastreamNamesArr) {
  // The sampling rate string is the last word in the Datastream name string
  return datastreamNamesArr.map((datastreamName) =>
    datastreamName.split(" ").pop()
  );
};

export {
  chartExportOptions,
  createCombinedTextDelimitedByAmpersand,
  createCombinedTextDelimitedByComma,
  convertHexColorToRGBColor,
  extractSamplingRateFromDatastreamName,
};
