import messages from '../../../locales/messages.esm.js';
import intl from '../../i18n/intl.esm.js';

function getDatumDateRange(datums) {
  let firstDay = 0;
  for (let i = firstDay; i < datums.length; i++) {
    if (datums[i]?.key && datums[i]?.y !== null) {
      firstDay = i;
      break;
    }
  }
  let lastDay = datums.length - 1;
  for (let i = lastDay; i >= 0; i--) {
    if (datums[i]?.key && datums[i].y !== null) {
      lastDay = i;
      break;
    }
  }
  const start = new Date(datums[firstDay].key);
  const end = new Date(datums[lastDay].key);
  return [start, end];
}
function getDateRangeString(datums, key, isSameDate = false, noDataKey = messages.chartNoData) {
  if (!(datums?.length && key)) {
    return intl.formatMessage(noDataKey);
  }
  const [start, end] = getDatumDateRange(datums);
  const dateRange = intl.formatDateTimeRange(isSameDate ? end : start, end, {
    day: "numeric",
    month: "short"
  });
  return intl.formatMessage(key, {
    dateRange
  });
}
function getMinOrMaxY(datum, minOrMaxFn) {
  let value;
  if (datum.y0 !== void 0) {
    value = minOrMaxFn(datum.y, datum.y0);
  } else {
    if (Array.isArray(datum.y)) {
      if (datum.y[0] !== null) {
        value = minOrMaxFn(...datum.y);
      } else if (datum.yVal) {
        value = datum.yVal;
      } else {
        value = null;
      }
    } else {
      value = datum.y;
    }
  }
  return value;
}
function getMaxMinValues(datums) {
  let max = null;
  let min = null;
  if (datums && datums.length) {
    datums.forEach((datum) => {
      const maxY = getMinOrMaxY(datum, Math.max);
      const minY = getMinOrMaxY(datum, Math.min);
      if (maxY !== null && (max === null || maxY > max)) {
        max = maxY;
      }
      if (minY !== null && (min === null || minY < min)) {
        min = minY;
      }
    });
  }
  return { max, min };
}

export { getDateRangeString, getDatumDateRange, getMaxMinValues };
//# sourceMappingURL=chart-datum.esm.js.map
