import { getInteractiveLegendItemStyles } from '@patternfly/react-charts';
import '../../i18n/intl.esm.js';
import '../../../locales/messages.esm.js';
import { getMaxMinValues } from './chart-datum.esm.js';

const isSeriesHidden = (hiddenSeries, index) => {
  return hiddenSeries.has(index);
};
const getDomain = (series, hiddenSeries, groupedSeriesCount = 0) => {
  const domain = { y: [0, 1] };
  let maxValue = null;
  let minValue = null;
  if (series) {
    let maybeEmptyHiddenSeries = hiddenSeries;
    if (series.length - groupedSeriesCount === maybeEmptyHiddenSeries.size) {
      domain.x = [0, 1];
      maybeEmptyHiddenSeries = /* @__PURE__ */ new Set();
    }
    series.forEach((s, index) => {
      if (!isSeriesHidden(maybeEmptyHiddenSeries, index) && s.data && s.data.length !== 0) {
        const { max, min } = getMaxMinValues(s.data);
        if (max !== null && (maxValue === null || max > maxValue)) {
          maxValue = max;
        }
        if (min !== null && (minValue === null || min < minValue)) {
          minValue = min;
        }
      }
    });
  }
  if (maxValue && minValue) {
    const threshold = maxValue * 0.05;
    const max = maxValue > 0 ? Math.ceil(maxValue + threshold) : 0;
    const _min = minValue > 0 ? Math.max(0, Math.floor(minValue - threshold)) : 0;
    const min = _min > 0 ? _min : 0;
    if (max > 0) {
      domain.y = [min, max];
    }
  }
  return domain;
};
const getLegendData = (series, hiddenSeries, tooltip = false) => {
  if (!series) {
    return void 0;
  }
  const result = [];
  series.map((s, index) => {
    if (s.legendItem) {
      const data = {
        childName: s.childName,
        ...s.legendItem,
        // name property
        ...tooltip && { name: s.legendItem.tooltip },
        // Override name property for tooltip
        ...getInteractiveLegendItemStyles(hiddenSeries?.has(index))
        // hidden styles
      };
      result.push(data);
    }
  });
  return result;
};
const getResizeObserver = (containerRef, handleResize) => {
  const containerElement = containerRef;
  const { ResizeObserver } = window;
  let _resizeObserver;
  if (containerElement && ResizeObserver) {
    const resizeObserver = new ResizeObserver((entries) => {
      window.requestAnimationFrame(() => {
        if (!Array.isArray(entries) || !entries.length) {
          return;
        }
        handleResize();
      });
    });
    resizeObserver.observe(containerElement);
    _resizeObserver = () => resizeObserver.unobserve(containerElement);
  } else {
    handleResize();
    window.addEventListener("resize", handleResize);
    _resizeObserver = () => window.removeEventListener("resize", handleResize);
  }
  return () => {
    if (_resizeObserver) {
      _resizeObserver();
    }
  };
};
const initHiddenSeries = (hiddenSeries, index) => {
  const result = new Set(hiddenSeries);
  if (!result.delete(index)) {
    result.add(index);
  }
  return result;
};
const isDataAvailable = (series, hiddenSeries) => {
  const unavailable = [];
  if (series) {
    series.forEach((s, index) => {
      if (hiddenSeries && isSeriesHidden(hiddenSeries, index) || s.data && s.data.length === 0) {
        unavailable.push(index);
      }
    });
  }
  return unavailable.length !== (series ? series.length : 0);
};

export { getDomain, getLegendData, getResizeObserver, initHiddenSeries, isDataAvailable, isSeriesHidden };
//# sourceMappingURL=chart-utils.esm.js.map
