'use client';
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "GroupedVerticalBarChart", {
    enumerable: true,
    get: function() {
        return GroupedVerticalBarChart;
    }
});
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
const _useGroupedVerticalBarChartStylesstyles = require("./useGroupedVerticalBarChartStyles.styles");
const _d3selection = require("d3-selection");
const _d3array = require("d3-array");
const _d3scale = require("d3-scale");
const _reactutilities = require("@fluentui/react-utilities");
const _index = require("../../utilities/index");
const _index1 = require("../../index");
const _reacttheme = require("@fluentui/react-theme");
const _hooks = require("../../utilities/hooks");
const _chartutilities = require("@fluentui/chart-utilities");
const MIN_DOMAIN_MARGIN = 8;
const X1_INNER_PADDING = 0.1;
const VERTICAL_BAR_GAP = 1;
const MIN_BAR_HEIGHT = 1;
const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((_props, forwardedRef)=>{
    var _props_legendProps, _props_legendProps1, _barData_, _lineData__data_, _lineData_;
    const props = {
        xAxisCategoryOrder: 'default',
        maxBarWidth: 24,
        ..._props
    };
    const _emptyChartId = (0, _reactutilities.useId)('_GVBC_empty');
    const _useRtl = (0, _index.useRtl)();
    let _domainMargin = MIN_DOMAIN_MARGIN;
    let _xAxisLabels = [];
    let _datasetForBars = [];
    let _margins = {
        top: 0,
        right: 0,
        bottom: 0,
        left: 0
    };
    let _groupedVerticalBarGraph = [];
    let _yMax = 0;
    let _calloutAnchorPoint = null;
    let _barWidth = 0;
    let _groupWidth = 0;
    let _xAxisInnerPadding = 0;
    let _xAxisOuterPadding = 0;
    let _barLegends = [];
    let _lineLegends = [];
    let _legendColorMap = {};
    const { cartesianChartRef, legendsRef: _legendsRef } = (0, _hooks.useImageExport)(props.componentRef, props.hideLegend);
    const Y_ORIGIN = 0;
    const _rectRef = _react.useRef(null);
    const _uniqDotId = (0, _reactutilities.useId)('gvbc_dot_');
    const [color, setColor] = _react.useState('');
    const [dataForHoverCard, setDataForHoverCard] = _react.useState(0);
    const [selectedLegends, setSelectedLegends] = _react.useState(((_props_legendProps = props.legendProps) === null || _props_legendProps === void 0 ? void 0 : _props_legendProps.selectedLegends) || []);
    const [xCalloutValue, setXCalloutValue] = _react.useState('');
    const [yCalloutValue, setYCalloutValue] = _react.useState('');
    const [yValueHover, setYValueHover] = _react.useState([]);
    const [hoverXValue, setHoverXValue] = _react.useState('');
    const [calloutLegend, setCalloutLegend] = _react.useState('');
    const [activeLegend, setActiveLegend] = _react.useState('');
    const [callOutAccessibilityData, setCallOutAccessibilityData] = _react.useState(undefined);
    const [popoverTarget, setPopoverTarget] = _react.useState(null);
    const [isPopoverOpen, setPopoverOpen] = _react.useState(false);
    const [activeLinePoint, setActiveLinePoint] = _react.useState('');
    const classes = (0, _useGroupedVerticalBarChartStylesstyles.useGroupedVerticalBarChartStyles_unstable)(props);
    _react.useEffect(()=>{
        var _props_legendProps;
        if (!(0, _index.areArraysEqual)((_props_legendProps = props.legendProps) === null || _props_legendProps === void 0 ? void 0 : _props_legendProps.selectedLegends, selectedLegends)) {
            var _props_legendProps1;
            setSelectedLegends(((_props_legendProps1 = props.legendProps) === null || _props_legendProps1 === void 0 ? void 0 : _props_legendProps1.selectedLegends) || []);
        }
    }, [
        (_props_legendProps1 = props.legendProps) === null || _props_legendProps1 === void 0 ? void 0 : _props_legendProps1.selectedLegends
    ]);
    const _adjustProps = ()=>{
        _barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth);
        // x0_inner_padding = space_between_groups / (space_between_groups + group_width)
        // space_between_groups = 2 * bar_width
        // group_width = _barLegends.length * bar_width + (_barLegends.length - 1) * space_between_bars
        _xAxisInnerPadding = (0, _index.getScalePadding)(props.xAxisInnerPadding, undefined, 2 / (2 + (0, _index.calcTotalBandUnits)(_barLegends.length, X1_INNER_PADDING)));
        _xAxisOuterPadding = (0, _index.getScalePadding)(props.xAxisOuterPadding);
    };
    const _createDataset = (barData, lineData)=>{
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const datasetForBars = [];
        const linePointsByX = {};
        const visitedX = new Set();
        lineData.forEach((series)=>{
            series.data.forEach((point)=>{
                if (!linePointsByX[point.x]) {
                    linePointsByX[point.x] = [];
                }
                linePointsByX[point.x].push({
                    ...point,
                    legend: series.legend,
                    color: series.color,
                    data: point.y,
                    useSecondaryYScale: series.useSecondaryYScale
                });
            });
        });
        barData.forEach((point, index)=>{
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const singleDatasetPointForBars = {};
            const legendToBarPoint = {};
            point.series.forEach((seriesPoint)=>{
                if (!singleDatasetPointForBars[seriesPoint.legend]) {
                    singleDatasetPointForBars[seriesPoint.legend] = [
                        {
                            ...seriesPoint
                        }
                    ];
                    legendToBarPoint[seriesPoint.legend] = {
                        ...seriesPoint
                    };
                } else {
                    singleDatasetPointForBars[seriesPoint.legend].push({
                        ...seriesPoint
                    });
                    legendToBarPoint[seriesPoint.legend].data += seriesPoint.data;
                }
            });
            singleDatasetPointForBars.xAxisPoint = point.name;
            singleDatasetPointForBars.indexNum = index;
            var _linePointsByX_point_name;
            singleDatasetPointForBars.groupSeries = [
                ...Object.values(legendToBarPoint),
                ...(_linePointsByX_point_name = linePointsByX[point.name]) !== null && _linePointsByX_point_name !== void 0 ? _linePointsByX_point_name : []
            ];
            singleDatasetPointForBars.stackCallOutAccessibilityData = point.stackCallOutAccessibilityData;
            datasetForBars.push(singleDatasetPointForBars);
            visitedX.add(point.name);
        });
        Object.keys(linePointsByX).forEach((xPoint)=>{
            if (!visitedX.has(xPoint)) {
                datasetForBars.push({
                    xAxisPoint: xPoint,
                    groupSeries: linePointsByX[xPoint]
                });
            }
        });
        return datasetForBars;
    };
    const _createDataSetOfGVBC = (barData, lineData)=>{
        const barLegends = new Set();
        barData.forEach((point)=>{
            point.series.forEach((seriesPoint)=>{
                barLegends.add(seriesPoint.legend);
            });
        });
        const lineLegends = new Set(lineData.map((series)=>series.legend));
        const xAxisLabels = _getOrderedXAxisLabels(barData, lineData);
        const datasetForBars = _createDataset(barData, lineData);
        return {
            barLegends: Array.from(barLegends),
            lineLegends: Array.from(lineLegends),
            xAxisLabels,
            datasetForBars
        };
    };
    const onLegendSelectionChange = (selectedLegends, event, currentLegend)=>{
        var _props_legendProps, _props_legendProps1;
        if ((_props_legendProps = props.legendProps) === null || _props_legendProps === void 0 ? void 0 : _props_legendProps.canSelectMultipleLegends) {
            setSelectedLegends(selectedLegends);
        } else {
            setSelectedLegends(selectedLegends.slice(-1));
        }
        if ((_props_legendProps1 = props.legendProps) === null || _props_legendProps1 === void 0 ? void 0 : _props_legendProps1.onChange) {
            props.legendProps.onChange(selectedLegends, event, currentLegend);
        }
    };
    const _getLegendData = ()=>{
        const actions = [];
        const addLegendButton = (legendTitle, isLineLegendInBarChart)=>{
            const legend = {
                title: legendTitle,
                color: _legendColorMap[legendTitle][0],
                isLineLegendInBarChart,
                hoverAction: ()=>{
                    _handleChartMouseLeave();
                    _onLegendHover(legendTitle);
                },
                onMouseOutAction: ()=>{
                    _onLegendLeave();
                }
            };
            actions.push(legend);
        };
        _lineLegends.forEach((legendTitle)=>addLegendButton(legendTitle, true));
        _barLegends.forEach((legendTitle)=>addLegendButton(legendTitle));
        return /*#__PURE__*/ _react.createElement(_index1.Legends, {
            legends: actions,
            enabledWrapLines: props.enabledLegendsWrapLines,
            overflowText: props.legendsOverflowText,
            ...props.legendProps,
            onChange: onLegendSelectionChange,
            legendRef: _legendsRef
        });
    };
    const _processDataV2 = (dataV2)=>{
        const barPointsByX = {};
        const lineData = [];
        dataV2.forEach((series)=>{
            if (series.type === 'bar') {
                series.data.forEach((point)=>{
                    if (!barPointsByX[point.x]) {
                        barPointsByX[point.x] = {
                            name: point.x,
                            series: []
                        };
                    }
                    var _series_key, _point_color;
                    barPointsByX[point.x].series.push({
                        key: (_series_key = series.key) !== null && _series_key !== void 0 ? _series_key : series.legend,
                        data: point.y,
                        color: (_point_color = point.color) !== null && _point_color !== void 0 ? _point_color : series.color,
                        // gradient: series.gradient,
                        legend: series.legend,
                        xAxisCalloutData: point.xAxisCalloutData,
                        yAxisCalloutData: point.yAxisCalloutData,
                        onClick: point.onClick,
                        useSecondaryYScale: series.useSecondaryYScale
                    });
                });
            } else if (series.type === 'line') {
                lineData.push(series);
            }
        });
        return {
            barData: Object.values(barPointsByX),
            lineData
        };
    };
    const _prepareChartData = ()=>{
        let barData = props.data;
        let lineData;
        if (Array.isArray(props.dataV2) && props.dataV2.length > 0) {
            ({ barData, lineData } = _processDataV2(props.dataV2));
        }
        _legendColorMap = {};
        let colorIndex = 0;
        var _barData_map, _lineData_map;
        return {
            barData: (_barData_map = barData === null || barData === void 0 ? void 0 : barData.map((point)=>{
                var _point_series;
                var _point_series_map;
                return {
                    ...point,
                    series: (_point_series_map = (_point_series = point.series) === null || _point_series === void 0 ? void 0 : _point_series.map((seriesPoint)=>{
                        // TODO: Add support for gradient colors
                        let startColor = seriesPoint.color ? seriesPoint.color : (0, _index.getNextColor)(colorIndex, 0);
                        let endColor = startColor;
                        if (!_legendColorMap[seriesPoint.legend]) {
                            _legendColorMap[seriesPoint.legend] = [
                                startColor,
                                endColor
                            ];
                        }
                        colorIndex += 1;
                        var _seriesPoint_color;
                        return {
                            ...seriesPoint,
                            color: (_seriesPoint_color = seriesPoint.color) !== null && _seriesPoint_color !== void 0 ? _seriesPoint_color : _legendColorMap[seriesPoint.legend][0]
                        };
                    })) !== null && _point_series_map !== void 0 ? _point_series_map : []
                };
            })) !== null && _barData_map !== void 0 ? _barData_map : [],
            lineData: (_lineData_map = lineData === null || lineData === void 0 ? void 0 : lineData.map((series)=>{
                let lineColor;
                if (typeof series.color === 'undefined') {
                    lineColor = (0, _index.getNextColor)(colorIndex, 0);
                } else {
                    lineColor = (0, _index1.getColorFromToken)(series.color);
                }
                if (!_legendColorMap[series.legend]) {
                    _legendColorMap[series.legend] = [
                        lineColor,
                        lineColor
                    ];
                }
                colorIndex += 1;
                return {
                    ...series,
                    color: _legendColorMap[series.legend][0]
                };
            })) !== null && _lineData_map !== void 0 ? _lineData_map : []
        };
    };
    const _getOrderedXAxisLabels = (barData, lineData)=>{
        if (_xAxisType !== _index.XAxisTypes.StringAxis) {
            return [];
        }
        return (0, _index.sortAxisCategories)(_mapCategoryToValues(barData, lineData), props.xAxisCategoryOrder);
    };
    const _mapCategoryToValues = (barData, lineData)=>{
        const categoryToValues = {};
        barData.forEach((point)=>{
            if (!categoryToValues[point.name]) {
                categoryToValues[point.name] = [];
            }
            point.series.forEach((seriesPoint)=>{
                categoryToValues[point.name].push(seriesPoint.data);
            });
        });
        lineData.forEach((series)=>{
            series.data.forEach((point)=>{
                if (!categoryToValues[point.x]) {
                    categoryToValues[point.x] = [];
                }
                categoryToValues[point.x].push(point.y);
            });
        });
        return categoryToValues;
    };
    const { barData, lineData } = _prepareChartData();
    var _barData__name;
    const firstXValue = (_barData__name = (_barData_ = barData[0]) === null || _barData_ === void 0 ? void 0 : _barData_.name) !== null && _barData__name !== void 0 ? _barData__name : (_lineData_ = lineData[0]) === null || _lineData_ === void 0 ? void 0 : (_lineData__data_ = _lineData_.data[0]) === null || _lineData__data_ === void 0 ? void 0 : _lineData__data_.x;
    const _xAxisType = (0, _chartutilities.isInvalidValue)(firstXValue) ? _index.XAxisTypes.StringAxis : (0, _index.getTypeOfAxis)(firstXValue, true);
    const { barLegends, lineLegends, xAxisLabels, datasetForBars } = _createDataSetOfGVBC(barData, lineData);
    _barLegends = barLegends;
    _lineLegends = lineLegends;
    _xAxisLabels = xAxisLabels;
    _datasetForBars = datasetForBars;
    const legendBars = _getLegendData();
    _adjustProps();
    // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Add commentMore actions
    function _getMinMaxOfYAxis(datasetForBars, yAxisType, useSecondaryYScale) {
        const values = [];
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        datasetForBars.forEach((data)=>{
            data.groupSeries.forEach((point)=>{
                if (!useSecondaryYScale === !point.useSecondaryYScale) {
                    values.push(point.data);
                }
            });
        });
        return {
            startValue: (0, _d3array.min)(values),
            endValue: (0, _d3array.max)(values)
        };
    }
    function _getDomainNRangeValues(points, margins, width, chartType, isRTL, xAxisType, barWidth, tickValues) {
        let domainNRangeValue;
        if (xAxisType === _index.XAxisTypes.NumericAxis || xAxisType === _index.XAxisTypes.DateAxis) {
            domainNRangeValue = {
                dStartValue: 0,
                dEndValue: 0,
                rStartValue: 0,
                rEndValue: 0
            };
        } else {
            domainNRangeValue = (0, _index.domainRangeOfXStringAxis)(margins, width, isRTL);
        }
        return domainNRangeValue;
    }
    // The maxOfYVal prop is only required for the primary y-axis, so yMax should be calculated
    // using only the data points associated with the primary y-axis.
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const yMax = _getMinMaxOfYAxis(_datasetForBars).endValue;
    _yMax = Math.max(yMax, props.yMaxValue || 0);
    const calloutProps = {
        positioning: {
            target: popoverTarget
        },
        isPopoverOpen,
        color,
        legend: calloutLegend,
        XValue: xCalloutValue,
        YValue: yCalloutValue ? yCalloutValue : dataForHoverCard,
        YValueHover: yValueHover,
        hoverXValue,
        culture: props.culture,
        isCartesian: true,
        isCalloutForStack: props.isCalloutForStack,
        ...props.calloutProps,
        ...(0, _index.getAccessibleDataObject)(callOutAccessibilityData, 'text', false)
    };
    const tickParams = {
        tickValues: props.tickValues,
        tickFormat: props.tickFormat
    };
    const _getGraphData = (xScale, yScalePrimary, containerHeight, containerWidth, xElement, yAxisElement, yScaleSecondary)=>{
        const xScale0 = _createX0Scale(containerWidth);
        // Setting the bar width here is safe because there are no dependencies earlier in the code
        // that rely on the width of bars in vertical bar charts with string x-axis.
        _barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth, (0, _index.calcBandwidth)(xScale0.bandwidth(), _barLegends.length, X1_INNER_PADDING));
        _groupWidth = (0, _index.calcRequiredWidth)(_barWidth, _barLegends.length, X1_INNER_PADDING);
        const xScale1 = _createX1Scale();
        const allGroupsBars = [];
        _datasetForBars.forEach((singleSet)=>{
            allGroupsBars.push(_buildGraph(singleSet, xScale0, xScale1, yScalePrimary, yScaleSecondary, containerHeight, xElement));
        });
        _groupedVerticalBarGraph = allGroupsBars;
    };
    const _getMargins = (margins)=>{
        _margins = margins;
    };
    const onBarHover = (pointData, groupData, mouseEvent)=>{
        mouseEvent.persist();
        if (_calloutAnchorPoint !== pointData) {
            _calloutAnchorPoint = pointData;
            _showCallout(mouseEvent.currentTarget, pointData, groupData);
        }
    };
    const _onBarLeave = ()=>{
    /**/ };
    const _handleChartMouseLeave = ()=>{
        _calloutAnchorPoint = null;
        setPopoverOpen(false);
    };
    const onBarFocus = (focusEvent, pointData, groupData)=>{
        _showCallout(focusEvent.currentTarget, pointData, groupData);
    };
    const _buildGraph = (singleSet, xScale0, xScale1, yScalePrimary, yScaleSecondary, containerHeight, xElement)=>{
        const singleGroup = [];
        const barLabelsForGroup = [];
        // Get the actual legends present at this x-axis point
        const presentLegends = _barLegends.filter((key)=>key in singleSet);
        const effectiveGroupWidth = (0, _index.calcRequiredWidth)(_barWidth, presentLegends.length, X1_INNER_PADDING);
        // For stacked bars, center the single bar group in the available space
        // Instead of using the global legend position, use the local position within present legends
        const localScale = (0, _d3scale.scaleBand)().domain(presentLegends).range(_useRtl ? [
            effectiveGroupWidth,
            0
        ] : [
            0,
            effectiveGroupWidth
        ]).paddingInner(X1_INNER_PADDING);
        _barLegends.forEach((legendTitle, legendIndex)=>{
            const barPoints = singleSet[legendTitle];
            if (barPoints) {
                const yBarScale = barPoints[0].useSecondaryYScale && yScaleSecondary ? yScaleSecondary : yScalePrimary;
                var _localScale;
                const xPoint = ((_localScale = localScale(legendTitle)) !== null && _localScale !== void 0 ? _localScale : 0) + (localScale.bandwidth() - _barWidth) / 2;
                const isLegendActive = _legendHighlighted(legendTitle) || _noLegendHighlighted();
                const barOpacity = isLegendActive ? '' : '0.1';
                let barTotalValue = 0;
                const yBaseline = yBarScale(Y_ORIGIN);
                let yPositiveStart = yBaseline;
                let yNegativeStart = yBaseline;
                let yPoint = Y_ORIGIN;
                barPoints.forEach((pointData, pointIndex)=>{
                    if (!pointData.data) {
                        // Not rendering data with 0.
                        return;
                    }
                    const barGap = VERTICAL_BAR_GAP / 2 * (pointIndex > 0 ? 2 : 0);
                    const height = Math.max(yBarScale(Y_ORIGIN) - yBarScale(Math.abs(pointData.data)), MIN_BAR_HEIGHT);
                    const pointColor = pointData.color; // Use the color of the current point
                    if (pointData.data >= Y_ORIGIN) {
                        yPositiveStart -= height + barGap;
                        yPoint = yPositiveStart;
                    } else {
                        yPoint = yNegativeStart + barGap;
                        yNegativeStart = yPoint + height;
                    }
                    singleGroup.push(/*#__PURE__*/ _react.createElement("rect", {
                        key: `${singleSet.indexNum}-${legendIndex}-${pointIndex}`,
                        className: classes.opacityChangeOnHover,
                        height: height,
                        width: _barWidth,
                        x: xPoint,
                        y: yPoint,
                        opacity: barOpacity,
                        fill: pointColor,
                        rx: props.roundCorners ? 3 : 0,
                        onMouseOver: (event)=>onBarHover(pointData, singleSet, event),
                        onMouseMove: (event)=>onBarHover(pointData, singleSet, event),
                        onMouseOut: _onBarLeave,
                        onFocus: (event)=>onBarFocus(event, pointData, singleSet),
                        onBlur: _onBarLeave,
                        onClick: pointData.onClick,
                        "aria-label": getAriaLabel(pointData, singleSet.xAxisPoint),
                        tabIndex: _legendHighlighted(pointData.legend) || _noLegendHighlighted() ? 0 : undefined,
                        role: "img"
                    }));
                    // Render individual bar label if provided
                    if (pointData.barLabel && isLegendActive) {
                        barLabelsForGroup.push(/*#__PURE__*/ _react.createElement("text", {
                            key: `${singleSet.indexNum}-${legendIndex}-${pointIndex}-label`,
                            x: xPoint + _barWidth / 2,
                            y: pointData.data >= Y_ORIGIN ? yPoint - 6 : yPoint + height + 12,
                            textAnchor: "middle",
                            className: classes.barLabel,
                            "aria-hidden": true,
                            style: {
                                direction: 'ltr',
                                unicodeBidi: 'isolate'
                            }
                        }, pointData.barLabel));
                    }
                    barTotalValue += pointData.data;
                });
                if (barTotalValue !== null && !props.hideLabels && Math.ceil(_barWidth) >= 16 && isLegendActive) {
                    barLabelsForGroup.push(/*#__PURE__*/ _react.createElement("text", {
                        key: `${singleSet.indexNum}-${legendIndex}`,
                        x: xPoint + _barWidth / 2,
                        y: barTotalValue >= Y_ORIGIN ? yPositiveStart - 6 : yNegativeStart + 12,
                        textAnchor: "middle",
                        className: classes.barLabel,
                        "aria-hidden": true,
                        style: {
                            direction: 'ltr',
                            unicodeBidi: 'isolate'
                        }
                    }, typeof props.yAxisTickFormat === 'function' ? props.yAxisTickFormat(barTotalValue) : (0, _index.formatScientificLimitWidth)(barTotalValue)));
                }
            }
        });
        return /*#__PURE__*/ _react.createElement("g", {
            key: singleSet.indexNum,
            transform: `translate(${xScale0(singleSet.xAxisPoint) + (xScale0.bandwidth() - effectiveGroupWidth) / 2}, 0)`
        }, singleGroup, barLabelsForGroup);
    };
    // For grouped vertical bar chart, First need to define total scale (from start to end)
    // From that need to define scale for single group of bars - done by createX1Scale
    const _createX0Scale = (containerWidth)=>{
        const x0Axis = (0, _d3scale.scaleBand)().domain(xAxisLabels).range(_useRtl ? [
            containerWidth - _margins.right - _domainMargin,
            _margins.left + _domainMargin
        ] : [
            _margins.left + _domainMargin,
            containerWidth - _margins.right - _domainMargin
        ]).paddingInner(_xAxisInnerPadding).paddingOuter(_xAxisOuterPadding);
        return x0Axis;
    };
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const _createX1Scale = ()=>{
        return (0, _d3scale.scaleBand)().domain(_barLegends) // When there is only one group, xScale0 adds padding around it,
        // causing the bandwidth to become smaller than the actual group width.
        // So to render bars in the group correctly, use groupWidth instead of the generated scale bandwidth.
        .range(_useRtl ? [
            _groupWidth,
            0
        ] : [
            0,
            _groupWidth
        ]).paddingInner(X1_INNER_PADDING);
    };
    const _onLegendHover = (legendTitle)=>{
        setActiveLegend(legendTitle);
    };
    const _onLegendLeave = ()=>{
        setActiveLegend('');
    };
    const _getAxisData = _react.useCallback((yAxisData)=>{
        if (yAxisData && yAxisData.yAxisDomainValues.length) {
            const { yAxisDomainValues: domainValue } = yAxisData;
            _yMax = Math.max(domainValue[domainValue.length - 1], props.yMaxValue || 0);
        }
    }, [
        props.yMaxValue
    ]);
    /**
   * This function checks if the given legend is highlighted or not.
   * A legend can be highlighted in 2 ways:
   * 1. selection: if the user clicks on it
   * 2. hovering: if there is no selected legend and the user hovers over it
   */ const _legendHighlighted = (legendTitle)=>{
        return _getHighlightedLegend().includes(legendTitle);
    };
    /**
   * This function checks if none of the legends is selected or hovered.
   */ const _noLegendHighlighted = ()=>{
        return _getHighlightedLegend().length === 0;
    };
    const _getHighlightedLegend = ()=>{
        return selectedLegends.length > 0 ? selectedLegends : activeLegend ? [
            activeLegend
        ] : [];
    };
    const getAriaLabel = (point, xAxisPoint)=>{
        var _point_callOutAccessibilityData;
        const xValue = point.xAxisCalloutData || xAxisPoint;
        const legend = point.legend;
        const yValue = point.yAxisCalloutData || point.data;
        return ((_point_callOutAccessibilityData = point.callOutAccessibilityData) === null || _point_callOutAccessibilityData === void 0 ? void 0 : _point_callOutAccessibilityData.ariaLabel) || `${xValue}. ${legend}, ${yValue}.`;
    };
    const _getDomainMargins = (containerWidth)=>{
        /** Total width available to render the bars */ const totalWidth = (0, _index.calcTotalWidth)(containerWidth, _margins, MIN_DOMAIN_MARGIN);
        if (_xAxisType === _index.XAxisTypes.StringAxis) {
            if ((0, _index.isScalePaddingDefined)(props.xAxisOuterPadding)) {
                // Setting the domain margin for string x-axis to 0 because the xAxisOuterPadding prop is now available
                // to adjust the space before the first group and after the last group.
                _domainMargin = 0;
            } else if (props.barWidth !== 'auto') {
                // Update the bar width so that when CartesianChart rerenders,
                // the following calculations don't use the previous bar width.
                _barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth);
                const groupWidth = (0, _index.calcRequiredWidth)(_barWidth, _barLegends.length, X1_INNER_PADDING);
                /** Total width required to render the groups. Directly proportional to group width */ const reqWidth = (0, _index.calcRequiredWidth)(groupWidth, _xAxisLabels.length, _xAxisInnerPadding);
                if (totalWidth >= reqWidth) {
                    // Center align the chart by setting equal left and right margins for domain
                    _domainMargin = MIN_DOMAIN_MARGIN + (totalWidth - reqWidth) / 2;
                }
            } else if (props.mode === 'plotly' && xAxisLabels.length > 1) {
                // Calculate the remaining width after rendering groups at their maximum allowable width
                const groupBandwidth = (0, _index.calcBandwidth)(totalWidth, _xAxisLabels.length, _xAxisInnerPadding);
                const barBandwidth = (0, _index.calcBandwidth)(groupBandwidth, _barLegends.length, X1_INNER_PADDING);
                const barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth, barBandwidth);
                const groupWidth = (0, _index.calcRequiredWidth)(barWidth, _barLegends.length, X1_INNER_PADDING);
                let reqWidth = (0, _index.calcRequiredWidth)(groupWidth, _xAxisLabels.length, _xAxisInnerPadding);
                const margin1 = (totalWidth - reqWidth) / 2;
                let margin2 = Number.POSITIVE_INFINITY;
                if (!props.hideTickOverlap) {
                    // Calculate the remaining width after accounting for the space required to render x-axis labels
                    const step = (0, _index.calculateLongestLabelWidth)(_xAxisLabels) + 20;
                    reqWidth = (_xAxisLabels.length - _xAxisInnerPadding) * step;
                    margin2 = (totalWidth - reqWidth) / 2;
                }
                _domainMargin = MIN_DOMAIN_MARGIN + Math.max(0, Math.min(margin1, margin2));
            }
        }
        return {
            ..._margins,
            left: _margins.left + _domainMargin,
            right: _margins.right + _domainMargin
        };
    };
    const _isChartEmpty = (_barData, _lineData)=>{
        return !(_barData && _barData.length > 0 && _barData.filter((item)=>item.series.length).length > 0 || _lineData && _lineData.length > 0 && _lineData.some((series)=>series.data.length > 0));
    };
    const _getChartTitle = ()=>{
        return (props.chartTitle ? `${props.chartTitle}. ` : '') + `Vertical bar chart with ${_barLegends.length} grouped bar series` + (_lineLegends.length > 0 ? ` and ${_lineLegends.length} line series. ` : '. ');
    };
    const _createLines = (_lineData, xScale, yScalePrimary, yScaleSecondary)=>{
        const lineBorders = [];
        const lines = [];
        const dots = [];
        const scaleLineX = (x)=>xScale(x) + xScale.bandwidth() / 2;
        _lineData.forEach((series, seriesIdx)=>{
            var _series_lineOptions;
            const lineBorderGroup = [];
            const lineGroup = [];
            const dotGroup = [];
            const lineBorderWidth = ((_series_lineOptions = series.lineOptions) === null || _series_lineOptions === void 0 ? void 0 : _series_lineOptions.lineBorderWidth) ? Number.parseFloat(series.lineOptions.lineBorderWidth.toString()) : 0;
            const yScale = series.useSecondaryYScale && yScaleSecondary ? yScaleSecondary : yScalePrimary;
            const shouldHighlight = _legendHighlighted(series.legend) || _noLegendHighlighted();
            series.data.forEach((point, pointIdx)=>{
                const x2 = scaleLineX(point.x);
                const y2 = yScale(point.y);
                if (pointIdx > 0) {
                    var _series_lineOptions, _series_lineOptions1, _series_lineOptions2;
                    const x1 = scaleLineX(series.data[pointIdx - 1].x);
                    const y1 = yScale(series.data[pointIdx - 1].y);
                    if (lineBorderWidth > 0) {
                        var _series_lineOptions3;
                        var _series_lineOptions_lineBorderColor;
                        lineBorderGroup.push(/*#__PURE__*/ _react.createElement("line", {
                            key: `lineBorder-${seriesIdx}-${pointIdx}`,
                            x1: x1,
                            y1: y1,
                            x2: x2,
                            y2: y2,
                            fill: "transparent",
                            stroke: (_series_lineOptions_lineBorderColor = (_series_lineOptions3 = series.lineOptions) === null || _series_lineOptions3 === void 0 ? void 0 : _series_lineOptions3.lineBorderColor) !== null && _series_lineOptions_lineBorderColor !== void 0 ? _series_lineOptions_lineBorderColor : _reacttheme.tokens.colorNeutralBackground1,
                            strokeWidth: 3 + lineBorderWidth * 2,
                            strokeLinecap: "round",
                            opacity: shouldHighlight ? 1 : 0.1
                        }));
                    }
                    var _series_lineOptions_strokeWidth, _series_lineOptions_strokeLinecap;
                    lineGroup.push(/*#__PURE__*/ _react.createElement("line", {
                        key: `line-${seriesIdx}-${pointIdx}`,
                        x1: x1,
                        y1: y1,
                        x2: x2,
                        y2: y2,
                        stroke: series.color,
                        strokeWidth: (_series_lineOptions_strokeWidth = (_series_lineOptions = series.lineOptions) === null || _series_lineOptions === void 0 ? void 0 : _series_lineOptions.strokeWidth) !== null && _series_lineOptions_strokeWidth !== void 0 ? _series_lineOptions_strokeWidth : 3,
                        strokeLinecap: (_series_lineOptions_strokeLinecap = (_series_lineOptions1 = series.lineOptions) === null || _series_lineOptions1 === void 0 ? void 0 : _series_lineOptions1.strokeLinecap) !== null && _series_lineOptions_strokeLinecap !== void 0 ? _series_lineOptions_strokeLinecap : 'round',
                        strokeDasharray: (_series_lineOptions2 = series.lineOptions) === null || _series_lineOptions2 === void 0 ? void 0 : _series_lineOptions2.strokeDasharray,
                        opacity: shouldHighlight ? 1 : 0.1,
                        onMouseOver: (e)=>_onLineHover(e, series, seriesIdx, pointIdx, scaleLineX),
                        onMouseLeave: _onBarLeave
                    }));
                }
                const dotId = _getDotId(seriesIdx, pointIdx);
                const isLinePointActive = activeLinePoint === point.x || activeLinePoint === dotId;
                dotGroup.push(/*#__PURE__*/ _react.createElement("circle", {
                    key: dotId,
                    id: dotId,
                    cx: x2,
                    cy: y2,
                    r: shouldHighlight && isLinePointActive ? 8 : 0.3,
                    fill: _reacttheme.tokens.colorNeutralBackground1,
                    stroke: series.color,
                    strokeWidth: 3,
                    opacity: shouldHighlight ? 1 : 0.1,
                    onMouseOver: (e)=>_onLineHover(e, series, seriesIdx, pointIdx, scaleLineX),
                    onMouseLeave: _onBarLeave,
                    "data-is-focusable": shouldHighlight,
                    onFocus: (e)=>_onLineFocus(e, series, seriesIdx, pointIdx),
                    onBlur: _onBarLeave,
                    role: "img",
                    "aria-label": getAriaLabel({
                        xAxisCalloutData: point.xAxisCalloutData,
                        legend: series.legend,
                        yAxisCalloutData: point.yAxisCalloutData,
                        data: point.y,
                        callOutAccessibilityData: point.callOutAccessibilityData
                    }, point.x)
                }));
            });
            lineBorders.push(/*#__PURE__*/ _react.createElement("g", {
                key: `lineBorderGroup-${seriesIdx}`
            }, lineBorderGroup));
            lines.push(/*#__PURE__*/ _react.createElement("g", {
                key: `lineGroup-${seriesIdx}`
            }, lineGroup));
            dots.push(/*#__PURE__*/ _react.createElement("g", {
                key: `dotGroup-${seriesIdx}`
            }, dotGroup));
        });
        return dots.length > 0 ? /*#__PURE__*/ _react.createElement("g", null, lineBorders.length > 0 ? /*#__PURE__*/ _react.createElement("g", null, lineBorders) : null, lines.length > 0 ? /*#__PURE__*/ _react.createElement("g", null, lines) : null, /*#__PURE__*/ _react.createElement("g", null, dots)) : null;
    };
    const _onLineHover = (event, series, seriesIdx, pointIdx, scaleLineX)=>{
        const pointerX = (0, _d3selection.pointer)(event, _rectRef.current)[0];
        let closestPointIdx = pointIdx;
        if (pointIdx > 0) {
            const currPointPos = scaleLineX(series.data[pointIdx].x);
            const prevPointPos = scaleLineX(series.data[pointIdx - 1].x);
            if (Math.abs(prevPointPos - pointerX) < Math.abs(currPointPos - pointerX)) {
                closestPointIdx = pointIdx - 1;
            }
        }
        event.persist();
        _showCalloutForLines(event.currentTarget, series, seriesIdx, closestPointIdx);
    };
    const _onLineFocus = (event, series, seriesIdx, pointIdx)=>{
        _showCalloutForLines(event.currentTarget, series, seriesIdx, pointIdx);
    };
    const _showCalloutForLines = (target, series, seriesIdx, pointIdx)=>{
        const point = series.data[pointIdx];
        const pointData = {
            ...point,
            legend: series.legend,
            color: series.color,
            key: series.legend,
            data: point.y,
            yAxisCalloutData: point.yAxisCalloutData
        };
        const groupData = _datasetForBars.find((singleSet)=>singleSet.xAxisPoint === point.x);
        _showCallout(target, pointData, groupData, _getDotId(seriesIdx, pointIdx));
    };
    const _showCallout = (target, pointData, groupData, _activeLinePoint = '')=>{
        setPopoverTarget(target);
        setPopoverOpen(_noLegendHighlighted() || _legendHighlighted(pointData.legend));
        setCalloutLegend(pointData.legend);
        setDataForHoverCard(pointData.data);
        setColor(pointData.color);
        var _pointData_xAxisCalloutData;
        setXCalloutValue((_pointData_xAxisCalloutData = pointData.xAxisCalloutData) !== null && _pointData_xAxisCalloutData !== void 0 ? _pointData_xAxisCalloutData : groupData.xAxisPoint);
        setYCalloutValue(pointData.yAxisCalloutData);
        setCallOutAccessibilityData(props.isCalloutForStack ? groupData.stackCallOutAccessibilityData : pointData.callOutAccessibilityData);
        setYValueHover(groupData.groupSeries.filter((item)=>_noLegendHighlighted() || _legendHighlighted(item.legend)));
        var _pointData_xAxisCalloutData1;
        setHoverXValue((_pointData_xAxisCalloutData1 = pointData.xAxisCalloutData) !== null && _pointData_xAxisCalloutData1 !== void 0 ? _pointData_xAxisCalloutData1 : groupData.xAxisPoint);
        setActiveLinePoint(props.isCalloutForStack ? groupData.xAxisPoint : _activeLinePoint);
    };
    const _getDotId = (seriesIdx, pointIdx)=>{
        return _uniqDotId + `-${seriesIdx}-${pointIdx}`;
    };
    return !_isChartEmpty(barData, lineData) ? /*#__PURE__*/ _react.createElement(_index1.CartesianChart, {
        ...props,
        chartTitle: _getChartTitle(),
        points: _datasetForBars,
        chartType: _index.ChartTypes.GroupedVerticalBarChart,
        getDomainNRangeValues: _getDomainNRangeValues,
        getMinMaxOfYAxis: _getMinMaxOfYAxis,
        createStringYAxis: _index.createStringYAxis,
        calloutProps: calloutProps,
        legendBars: legendBars,
        xAxisType: _xAxisType,
        createYAxis: _index.createNumericYAxis,
        datasetForXAxisDomain: _xAxisLabels,
        tickParams: tickParams,
        tickPadding: props.tickPadding || 5,
        maxOfYVal: _yMax,
        getmargins: _getMargins,
        getGraphData: _getGraphData,
        getAxisData: _getAxisData,
        onChartMouseLeave: _handleChartMouseLeave,
        getDomainMargins: _getDomainMargins,
        ..._xAxisType === _index.XAxisTypes.StringAxis && {
            xAxisInnerPadding: _xAxisInnerPadding,
            xAxisOuterPadding: _xAxisOuterPadding
        },
        barwidth: _barWidth,
        componentRef: cartesianChartRef,
        /* eslint-disable react/jsx-no-bind */ children: (childProps)=>{
            return /*#__PURE__*/ _react.createElement(_react.Fragment, null, /*#__PURE__*/ _react.createElement("rect", {
                ref: _rectRef,
                width: "100%",
                height: "100%",
                fill: "transparent",
                style: {
                    pointerEvents: 'none'
                }
            }), /*#__PURE__*/ _react.createElement("g", null, _groupedVerticalBarGraph), _createLines(lineData, childProps.xScale, childProps.yScalePrimary, childProps.yScaleSecondary));
        }
    }) : /*#__PURE__*/ _react.createElement("div", {
        id: _emptyChartId,
        role: 'alert',
        style: {
            opacity: '0'
        },
        "aria-label": 'Graph has no data to display'
    });
});
GroupedVerticalBarChart.displayName = 'GroupedVerticalBarChart';
