/* @oracle/oraclejet-preact: undefined */
'use strict';

var jsxRuntime = require('preact/jsx-runtime');
var BarGroup = require('./BarGroup-022d6009.js');
var useUser = require('./useUser-c85f9587.js');
var layoutUtils = require('./layoutUtils-0615d614.js');
var dataStyleUtils = require('./dataStyleUtils-e5ab11d0.js');
var Chart = require('./Chart-13ccd345.js');

/**
 * @license
 * Copyright (c) %FIRST_YEAR% %CURRENT_YEAR%, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 * @ignore
 */
/**
 * Get next item in unstacked bar chart.
 * @param info
 * @param viewportEndIndex
 * @param numSeries
 * @param isNavigable
 * @returns
 */
function getNextItem(info, viewportEndIndex, numSeries, isNavigable) {
    const nextItemInGroup = dataStyleUtils.getNextItemInGroup(info, numSeries, isNavigable);
    if (nextItemInGroup)
        return nextItemInGroup;
    if (info.groupIndex < viewportEndIndex) {
        let nextItem = { seriesIndex: 0, groupIndex: info.groupIndex + 1 };
        if (isNavigable(nextItem.seriesIndex, nextItem.groupIndex))
            return nextItem;
        nextItem = getNextItem(nextItem, viewportEndIndex, numSeries, isNavigable);
        if (nextItem)
            return nextItem;
    }
    return info;
}
/**
 * Get prev item in unstacked bar chart.
 * @param info
 * @param viewportStartIndex
 * @param numSeries
 * @param isNavigable
 * @returns
 */
function getPreviousItem(info, viewportStartIndex, numSeries, isNavigable) {
    const prevItemInGroup = dataStyleUtils.getPrevItemInGroup(info, isNavigable);
    if (prevItemInGroup)
        return prevItemInGroup;
    if (info.groupIndex > viewportStartIndex) {
        let prevItem = { groupIndex: info.groupIndex - 1, seriesIndex: numSeries - 1 };
        if (isNavigable(prevItem.seriesIndex, prevItem.groupIndex))
            return prevItem;
        prevItem = getPreviousItem(prevItem, viewportStartIndex, numSeries, isNavigable);
        if (prevItem)
            return prevItem;
    }
    return info;
}
function getBarChartNavUtil(getDataItem, numSeries, viewportStartIndex, viewportEndIndex, isStacked, isRtl) {
    const isItemNavigable = (seriesIndex, groupIndex) => {
        const barItem = getDataItem(seriesIndex, groupIndex);
        return Boolean(barItem);
    };
    const getNextChartItem = (itemInfo, arrow) => {
        if (!isStacked && (arrow === 'ArrowDown' || arrow === 'ArrowUp')) {
            return itemInfo;
        }
        let newInfo;
        // TODO: this is just to keep typescript happy.
        // figure out consistent typing while doing type cleanup.
        const info = {
            groupIndex: itemInfo.groupIndex,
            seriesIndex: itemInfo.seriesIndex
        };
        if (info.groupIndex < viewportStartIndex) {
            info.groupIndex = viewportStartIndex;
            return info;
        }
        else if (info.groupIndex > viewportEndIndex) {
            info.groupIndex = viewportEndIndex;
            return info;
        }
        if (isStacked) {
            if (arrow === 'ArrowLeft') {
                newInfo = isRtl
                    ? dataStyleUtils.getNextItemInSeries(info, viewportEndIndex, isItemNavigable)
                    : dataStyleUtils.getPrevItemInSeries(info, viewportStartIndex, isItemNavigable);
            }
            else if (arrow === 'ArrowRight') {
                newInfo = isRtl
                    ? dataStyleUtils.getPrevItemInSeries(info, viewportStartIndex, isItemNavigable)
                    : dataStyleUtils.getNextItemInSeries(info, viewportEndIndex, isItemNavigable);
            }
            else if (arrow === 'ArrowDown') {
                newInfo = dataStyleUtils.getPrevItemInGroup(info, isItemNavigable);
            }
            else {
                newInfo = dataStyleUtils.getNextItemInGroup(info, numSeries, isItemNavigable);
            }
        }
        else {
            if (arrow === 'ArrowRight') {
                newInfo = isRtl
                    ? getPreviousItem(info, viewportStartIndex, numSeries, isItemNavigable)
                    : getNextItem(info, viewportEndIndex, numSeries, isItemNavigable);
            }
            else if (arrow === 'ArrowLeft') {
                newInfo = isRtl
                    ? getNextItem(info, viewportEndIndex, numSeries, isItemNavigable)
                    : getPreviousItem(info, viewportStartIndex, numSeries, isItemNavigable);
            }
        }
        return newInfo || info;
    };
    return getNextChartItem;
}

function OverviewBarChart(props) {
    return (jsxRuntime.jsx(BarChart, { datatip: () => ({ content: '' }), zoomAndScroll: "off", plotArea: {
            yMajorTick: {
                isRendered: false
            },
            xMajorTick: {}
        }, 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
            }
        }, overview: "off" }));
}

/* bar chart start and end axis offset */
const AXIS_OFFSET = 0.5;
/**
 * A Bar chart displays information graphically using rectangular bars with heights or lengths proportional to the values that they represent,
 * making relationships among the data easier to understand.
 */
function BarChart({ width, height, selectedIds = [], hiddenIds = [], highlightedIds, hideAndShowBehavior = 'none', orientation = 'vertical', yAxis, series, groups, isStacked = false, getDataItem, selectionMode = 'none', drilling = 'off', dataItemGaps = 0.5, ...props }) {
    const { direction } = useUser.useUser();
    const isRtl = direction === 'rtl';
    const hiddenSet = new Set(hiddenIds);
    const isHoriz = orientation === 'horizontal';
    const timeAxisType = props.xAxis?.timeAxisType;
    const selectedSet = new Set(selectedIds);
    const highlightedSet = new Set(highlightedIds);
    const getGapRatio = (startIndex, endIndex) => {
        return BarGroup.getBarGapRatio(isStacked, startIndex, endIndex);
    };
    const getNextChartItemFunc = (startIndex, endIndex) => {
        return getBarChartNavUtil(getDataItem, series.length, startIndex, endIndex, isStacked, isRtl);
    };
    const getDataItemPos = (xScale, yScale, groupWidth, gapRatio) => {
        return BarGroup.getBarPosition(series, groups, getDataItem, xScale, yScale, hiddenSet, dataItemGaps, groupWidth, gapRatio, yAxis?.scale === 'log', isStacked, isRtl, isHoriz, timeAxisType);
    };
    const findNearest = (xScale, yScale, groupWidth, gapRatio) => {
        const getSeriesDataInfo = BarGroup.getSeriesOffsetAndWidth(groupWidth, gapRatio, series.length, dataItemGaps, isStacked, isRtl);
        return layoutUtils.findNearestDataPoint(series, groups, xScale, yScale, getDataItem, isStacked, getSeriesDataInfo, BarGroup.getBarCenterCoordinate, yAxis?.scale === 'log', dataItemGaps, hiddenSet, timeAxisType);
    };
    const getMarkerColorAndType = (seriesIndex, groupIndex) => {
        return {
            color: dataStyleUtils.getItemColor(seriesIndex, groupIndex, series, getDataItem),
            type: 'circle'
        };
    };
    return (jsxRuntime.jsx(Chart.Chart, { width: width, height: height, selectedIds: selectedSet, hiddenIds: hiddenIds, hideAndShowBehavior: hideAndShowBehavior, orientation: orientation, yAxis: yAxis, series: series, groups: groups, isStacked: isStacked, getDataItem: getDataItem, selectionMode: selectionMode, drilling: drilling, renderGridLinesInFront: false, findNearest: findNearest, getDataItemPos: getDataItemPos, getNavUtil: getNextChartItemFunc, getGapRatio: getGapRatio, getMarkerInfo: getMarkerColorAndType, isRtl: isRtl, offset: AXIS_OFFSET, defaultOverviewContentRenderer: () => {
            return jsxRuntime.jsx(OverviewBarChart, { series: series, groups: groups, getDataItem: getDataItem });
        }, ...props, children: ({ xStartIndex, xEndIndex, xScale, yScale, gapRatio, activeId, focusedItemInfo, hoveredItemInfo, groupWidth, isPointInsideMarquee, getTextDimensions, defaultFontSize, colors }) => {
            return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: groups.slice(xStartIndex, xEndIndex + 1).map((_, index) => (jsxRuntime.jsx(BarGroup.BarGroup, { yScale: yScale, xScale: xScale, groupIndex: index + xStartIndex, selectedIds: selectedSet, hiddenIds: hiddenSet, highlightedIds: highlightedIds ? highlightedSet : highlightedIds, isDrillEnabled: drilling === 'on', series: series, barGapRatio: gapRatio, dataItemGaps: dataItemGaps, isPointInsideMarquee: isPointInsideMarquee, isStacked: isStacked, groupWidth: groupWidth, group: groups[index], timeAxisType: props.xAxis?.timeAxisType, colors: colors, activeId: activeId, isLog: yAxis?.scale === 'log', orientation: orientation, getDataItem: getDataItem, isSelectionEnabled: selectionMode != 'none', isRtl: isRtl, focusedItemInfo: focusedItemInfo, hoveredItemInfo: hoveredItemInfo, getTextDimensions: getTextDimensions, defaultFontSize: defaultFontSize, getStackLabel: props.getStackLabel }))) }));
        } }));
}

exports.BarChart = BarChart;
//# sourceMappingURL=BarChart-66dbe5be.js.map
