/* @oracle/oraclejet-preact: undefined */
import { jsx, jsxs, Fragment } from 'preact/jsx-runtime';
import { h as findNearestDataPoint } from './layoutUtils-5d62f2b4.js';
import { A as AreaSeries, g as getLineAreaChartItemPosition } from './chartUtils-088f15d9.js';
import { h as getCumulativeSeries, a as getSeriesData, b as getLineAreaSegments, g as getMarkers } from './useChartDnd-fc7ba21c.js';
import { L as LineSeries, M as MarkersWithLabel } from './LineSeries-fc167ea2.js';
import { f as getLineAreaChartNavUtil, d as getItemColor, h as getMarkerType } from './dataStyleUtils-1e980302.js';
import { u as useUser } from './useUser-1cd223a9.js';
import { g as getColorRamp } from './colorUtils-35e3dd2b.js';
import './accUtils-ce2d6588.js';
import { colorSchemeVars } from './Common/themes/themeContract.css.js';
import { C as Chart } from './Chart-4ec13697.js';

function OverviewLineAreaChart(props) {
    return (jsx(LineAreaChart, { datatip: () => ({ content: '' }), zoomAndScroll: "off", overview: "off", plotArea: {
            yMajorTick: {
                isRendered: false
            }
        }, xAxis: {
            tickLabel: {
                autoRotate: false,
                // @ts-ignore
                isRendered: props.xAxis?.timeAxisType ? true : false
            }
        }, series: props.series, groups: props.groups, getDataItem: props.getDataItem, yAxis: {
            tickLabel: {
                isRendered: false
            }
        }, type: props.type }));
}

const AXIS_OFFSET = 0.2;
const LINE_WITH_AREA_OPACITY = 0.2;
/**
 * The LineAreaChart displays information graphically using lines and filled areas, making relationships among the data easier to understand.
 */
function LineAreaChart({ width, height, selectedIds = [], hiddenIds = [], highlightedIds, hideAndShowBehavior = 'none', orientation = 'vertical', yAxis, series, groups, isStacked = false, getDataItem, selectionMode = 'none', drilling = 'off', type = 'line', overview = 'off', dataLabelEffect = 'outline', ...props }) {
    const { direction, forcedColors } = useUser();
    const isHighContrast = forcedColors === 'active';
    const isTestEnv = props.testId !== undefined;
    const isRtl = direction === 'rtl';
    const isLog = yAxis?.scale === 'log';
    const timeAxisType = props.xAxis?.timeAxisType;
    const hiddenSet = new Set(hiddenIds);
    const selectedSet = new Set(selectedIds);
    const highlightedSet = new Set(highlightedIds);
    const isHoriz = orientation === 'horizontal';
    const colors = getColorRamp();
    const renderedSeries = isStacked
        ? getCumulativeSeries(series.length, groups.length, getDataItem, hiddenSet, isLog)
        : getSeriesData(series?.length, groups?.length, getDataItem, hiddenSet, isLog);
    const renderedGetDataItem = (seriesIndex, groupIndex) => {
        if (!isStacked)
            return getDataItem(seriesIndex, groupIndex);
        return {
            ...getDataItem(seriesIndex, groupIndex),
            value: renderedSeries[seriesIndex][groupIndex]
        };
    };
    const getGapRatio = () => {
        return 1;
    };
    const getNextChartItemFunc = (startIndex, endIndex) => {
        // TODO: JET-71332
        //@ts-ignore
        return getLineAreaChartNavUtil(getDataItem, series.length, startIndex, endIndex, isStacked, isRtl, isHoriz);
    };
    const getDataItemPos = (xScale, yScale) => {
        return (seriesIndex, groupIndex) => {
            return getLineAreaChartItemPosition(seriesIndex, groupIndex, renderedSeries, getDataItem, groups, isHoriz, xScale, yScale, timeAxisType);
        };
    };
    const getMarkerColorAndType = (seriesIndex, groupIndex) => {
        return {
            color: getItemColor(seriesIndex, groupIndex, series, getDataItem),
            type: getMarkerType(seriesIndex, groupIndex, getDataItem)
        };
    };
    const findNearest = (xScale, yScale) => {
        const getSeriesDataInfo = () => {
            return {
                offset: 0,
                dataWidth: 0
            };
        };
        const getYCoord = (value, yScale) => {
            return yScale.transform(value);
        };
        return findNearestDataPoint(series, groups, xScale, yScale, renderedGetDataItem, isStacked, getSeriesDataInfo, getYCoord, isLog, 0, hiddenSet, timeAxisType);
    };
    const markers = getMarkers();
    const areaOpacity = type === 'lineWithArea' ? LINE_WITH_AREA_OPACITY : 1;
    const isAreaRendered = type === 'area' || type === 'lineWithArea';
    const isContrastLineNeeded = type === 'area';
    const allLineSegmentCoords = [];
    return (jsx(Chart, { width: width, height: height, selectedIds: selectedSet, hiddenIds: hiddenIds, hideAndShowBehavior: hideAndShowBehavior, orientation: orientation, yAxis: yAxis, series: series, groups: groups, selectionMode: selectionMode, drilling: drilling, isStacked: isStacked, getDataItem: getDataItem, findNearest: findNearest, getDataItemPos: getDataItemPos, getMarkerInfo: getMarkerColorAndType, renderGridLinesInFront: isAreaRendered, getNavUtil: getNextChartItemFunc, getGapRatio: getGapRatio, offset: AXIS_OFFSET, isRtl: isRtl, overview: overview, defaultOverviewContentRenderer: () => {
            return (jsx(OverviewLineAreaChart, { series: series, groups: groups, getDataItem: getDataItem, type: type }));
        }, ...props, children: ({ xStartIndex, xEndIndex, xScale, yScale, activeId, focusedItemInfo, hoveredItemInfo, isPointInsideMarquee, defaultFontSize, getTextDimensions }) => {
            return (jsxs(Fragment, { children: [renderedSeries.map((lineAreaSeries, index) => {
                        if (lineAreaSeries.length === 0) {
                            /* TODO: need a better way to handle when a series is hidden. */
                            return;
                        }
                        const seriesItem = series[index];
                        const lineSegmentCoords = getLineAreaSegments(yScale, xScale, lineAreaSeries, xStartIndex, xEndIndex, isHoriz, isLog, groups, index, renderedGetDataItem, timeAxisType);
                        allLineSegmentCoords[index] = lineSegmentCoords[0];
                        const isBottomSegmentCurved = seriesItem.lineType === 'curved' &&
                            isStacked &&
                            renderedSeries.length > 1 &&
                            index > 0;
                        const isHighlighted = highlightedIds === undefined ||
                            highlightedIds.length === 0 ||
                            highlightedSet.has(seriesItem.id);
                        const prevCoords = isStacked && index != 0
                            ? allLineSegmentCoords.reduce((prevValue, newValue, j) => {
                                // find the index i such that i < index, and series i  has at least
                                // one or more data item unhidden. Only such series will can be
                                // used as bottomCoord for current series. Otherwise we have to use
                                // baseline coord
                                if (j >= index) {
                                    return prevValue;
                                }
                                if (newValue.length > 0) {
                                    return j;
                                }
                                return prevValue;
                            }, 0)
                            : undefined;
                        return (jsxs(Fragment, { children: [isAreaRendered && (jsx(AreaSeries, { yScale: yScale, lineType: seriesItem.lineType, color: seriesItem.areaColor || colors[index % colors.length], isHorizontal: isHoriz, areaColorOpacity: areaOpacity, isBottomSegmentCurved: isBottomSegmentCurved, bottomCoords: prevCoords !== undefined &&
                                        allLineSegmentCoords[prevCoords].some((value) => value != null)
                                        ? allLineSegmentCoords[prevCoords]
                                        : undefined, lineSegments: lineSegmentCoords, isLog: isLog, isHighlighted: isHighlighted, seriesIndex: index })), jsx(LineSeries, { lineType: seriesItem.lineType, 
                                    /* TODO: confirm with UX if this is contrastLine (changes depending on dark/light mode) or always white.
                                        It's always white in legacy.*/
                                    lineColor: isContrastLineNeeded ? colorSchemeVars.dvt.contrastLine : seriesItem.lineColor, 
                                    /* The area generated in legacy are bit apart and not touching when stacked. Preact areas are a bit closer than legacy when curved.
                                      Increased the contrast linewidth (to 2.5 from legacy 1.25) to give legacy visual treatment.
                                      TODO: investivage why legacy areas are not touching. maybe we want to do the same in preact svg utils that
                                      generates this curve.
                                     */
                                    lineWidth: isContrastLineNeeded ? (isStacked ? 2.5 : 1.25) : seriesItem.lineWidth, lineStyle: seriesItem.lineStyle, color: seriesItem.lineColor || colors[index % colors.length], lineSegmentCoords: lineSegmentCoords, isHorizontal: isHoriz, isHighlighted: isHighlighted, seriesIndex: index }), 
                                /*for non stacked charts, markers should render in the order of series.
                                 some markers might get hidden behind area but this preserves the legacy behavior. */
                                !isStacked && (jsx(MarkersWithLabel, { yScale: yScale, xScale: xScale, groups: groups, series: seriesItem, timeAxisType: timeAxisType, color: seriesItem.lineColor ||
                                        seriesItem.areaColor ||
                                        colors[index % colors.length] /* TODO : make this use getItemColor function from dataStyleUtils. */, seriesIndex: index, startIndex: xStartIndex, endIndex: xEndIndex, markerType: markers[index % markers.length], orientation: orientation, isLog: isLog, activeId: activeId, getDataItem: getDataItem, isPointInsideMarquee: isPointInsideMarquee, selectedIds: selectedSet, highlightedIds: highlightedIds !== undefined ? highlightedSet : undefined, focusedItemIndex: focusedItemInfo.seriesIndex === index &&
                                        focusedItemInfo.isFocusVisible &&
                                        !(props.isDataCursorEnabled && props.dataCursorStyle?.isMarkerDisplayed)
                                        ? focusedItemInfo.groupIndex
                                        : undefined, hoveredItemIndex: hoveredItemInfo?.isCurrent && hoveredItemInfo.seriesIndex === index
                                        ? hoveredItemInfo.groupIndex
                                        : undefined, isSelectionEnabled: selectionMode === 'single' || selectionMode === 'multiple', isDrillEnabled: drilling === 'on', defaultFontSize: defaultFontSize, dataLabelEffect: dataLabelEffect, isHighContrast: isHighContrast, isTestEnv: isTestEnv, getTextDimensions: getTextDimensions }))] }));
                    }), isStacked &&
                        renderedSeries.map((lineAreaSeries, index) => {
                            if (lineAreaSeries.length === 0) {
                                /* TODO: need a better way to handle when a series is hidden.*/
                                return;
                            }
                            const seriesItem = series[index];
                            return (jsx(MarkersWithLabel, { yScale: yScale, series: series[index], xScale: xScale, groups: groups, timeAxisType: timeAxisType, color: seriesItem.lineColor || seriesItem.areaColor || colors[index % colors.length], seriesIndex: index, isPointInsideMarquee: isPointInsideMarquee, startIndex: xStartIndex, endIndex: xEndIndex, markerType: markers[index % markers.length], orientation: orientation, isLog: isLog, activeId: activeId, getDataItem: renderedGetDataItem, selectedIds: selectedSet, focusedItemIndex: focusedItemInfo.seriesIndex === index && focusedItemInfo.isFocusVisible
                                    ? focusedItemInfo.groupIndex
                                    : undefined, hoveredItemIndex: hoveredItemInfo?.isCurrent && hoveredItemInfo.seriesIndex === index
                                    ? hoveredItemInfo.groupIndex
                                    : undefined, isSelectionEnabled: selectionMode === 'single' || selectionMode === 'multiple', isDrillEnabled: drilling === 'on', defaultFontSize: defaultFontSize, dataLabelEffect: dataLabelEffect, isHighContrast: isHighContrast, isTestEnv: isTestEnv, getTextDimensions: getTextDimensions }));
                        })] }));
        } }));
}

export { LineAreaChart as L };
//# sourceMappingURL=LineAreaChart-32cf5c62.js.map
