import { format } from 'date-fns';

const getRecommendationTerm = (interval, recommendations) => {
  let result;
  switch (interval) {
    case "shortTerm":
      result = recommendations?.recommendationTerms?.shortTerm;
      break;
    case "mediumTerm":
      result = recommendations?.recommendationTerms?.mediumTerm;
      break;
    case "longTerm":
      result = recommendations?.recommendationTerms?.longTerm;
      break;
  }
  return result || {};
};
const createUsageDatum = (usageType, currentInterval, recommendations) => {
  const datum = [];
  if (recommendations) {
    const term = getRecommendationTerm(currentInterval, recommendations);
    const plotsData = term?.plots?.plotsData || {};
    for (const key of Object.keys(plotsData)) {
      const data = plotsData?.[key]?.[usageType];
      const date = new Date(key);
      const xVal = currentInterval === "shortTerm" ? format(date, "kk:mm") : format(date, "MMM d");
      datum.push({
        key,
        name: usageType,
        units: data?.format,
        x: xVal,
        y: data ? [data.min, data.median, data.max, data.q1, data.q3] : [null]
      });
    }
  }
  if (datum.length === 0 && recommendations?.monitoringEndTime) {
    if (currentInterval === "shortTerm") {
      const today = new Date(recommendations?.monitoringEndTime);
      for (let hour = 24; hour > 0; hour -= 6) {
        today.setHours(today.getHours() - hour);
        datum.push({
          key: today.toDateString(),
          name: usageType,
          x: format(today, "kk:mm"),
          y: [null]
        });
      }
    } else {
      for (let day = currentInterval === "longTerm" ? 15 : 7; day > 0; day--) {
        const today = new Date(recommendations?.monitoringEndTime);
        today.setDate(today.getDate() - day);
        datum.push({
          key: today.toDateString(),
          name: usageType,
          x: format(today, "MMM d"),
          y: [null]
        });
      }
    }
  }
  return datum;
};
const createRecommendationDatum = (currentInterval, usageDatum, recommendationType, resourceType, optimizationType, recommendations) => {
  const term = getRecommendationTerm(currentInterval, recommendations);
  const values = term?.recommendationEngines?.[optimizationType]?.config?.[resourceType]?.[recommendationType];
  const datum = [];
  if (values) {
    usageDatum.forEach((data) => {
      datum.push({
        ...data,
        name: resourceType,
        y: values.amount,
        units: values.format
      });
    });
  }
  return datum.length ? [
    {
      ...datum[0],
      key: void 0,
      // Don't use date here
      x: 0
      // Extends threshold lines to chart edge
    },
    ...datum,
    {
      ...datum[0],
      key: void 0,
      // Don't use date here
      x: 100
    }
  ] : [];
};

export { createRecommendationDatum, createUsageDatum, getRecommendationTerm };
//# sourceMappingURL=chart-data-format.esm.js.map
