/* @oracle/oraclejet-preact: undefined */
import { u as useContextMenu } from './useContextMenu-3c62d9eb.js';
import 'preact/jsx-runtime';
import './Menu-a5bf756f.js';
import './MenuItem-54015ca3.js';
import 'preact/hooks';
import 'preact';
import './logger-c92f309c.js';
import './LayerHost-e6402730.js';
import 'preact/compat';
import './TooltipContentStyles.styles.css';
import './hooks/UNSAFE_useTooltip/themes/redwood/TooltipContentBaseTheme.styles.css';
import './hooks/UNSAFE_useTooltip/themes/redwood/TooltipContentVariants.css.js';
import './tooltipUtils-768450a7.js';
import './IconStyle.styles.css';
import './SelectMenuGroupContext-b56cf5d4.js';
import './UNSAFE_Separator/themes/SeparatorStyles.css.js';
import './MenuSeparatorStyles.styles.css';
import './UNSAFE_Menu/themes/redwood/MenuSeparatorBaseTheme.styles.css';
import './UNSAFE_Menu/themes/redwood/MenuSeparatorVariants.css.js';
import { g as getInfo } from './eventsUtils-e5fc62d5.js';
import './accUtils-ce2d6588.js';
import { c as calculateOffset } from './layoutUtils-d7613d0c.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
 */
function getChartContextMenuContext(focusedItemInfo, gesture, target, series, groups, rootRef, xAxisTitle, yAxisTitle, getDataItem) {
    let seriesIndex;
    let groupIndex;
    if (gesture === 'keyboard') {
        //We find the element where the position of the menu is going to be based of
        groupIndex = focusedItemInfo.groupIndex;
        seriesIndex = focusedItemInfo.seriesIndex;
    }
    else {
        //We search for the key using the target of the event
        const info = getInfo(rootRef, target);
        seriesIndex = info ? info.seriesIndex : undefined;
        groupIndex = info ? info.groupIndex : undefined;
    }
    let context;
    if (seriesIndex !== undefined &&
        groupIndex !== undefined &&
        getDataItem?.(seriesIndex, groupIndex)) {
        context = {
            data: getDataItem(seriesIndex, groupIndex),
            groupIndex,
            seriesIndex,
            type: 'item'
        };
    }
    else if (seriesIndex !== undefined) {
        context = {
            data: series[seriesIndex],
            type: 'series'
        };
    }
    else if (groupIndex !== undefined) {
        context = {
            data: groups[groupIndex],
            type: 'xAxisTickLabel'
        };
    }
    else {
        const target = event?.target;
        const text = target?.textContent;
        if ((text && gesture !== 'keyboard' && text === xAxisTitle) || text === yAxisTitle) {
            context = {
                type: 'axisTitle',
                axis: text === xAxisTitle ? 'x' : 'y'
            };
        }
        else {
            context = {
                type: 'background'
            };
        }
    }
    return {
        context: context
    };
}

const useChartContextMenu = ({ isRtl, width, getDataItemPosition, getDataItem, focusedItemInfo, series, groups, rootRef, xAxisTitle, yAxisTitle, contextMenuConfig, onContextMenuDismissed }) => {
    const { contextMenuProps, contextMenuContent } = useContextMenu({
        onContextMenuHandler: ({ gesture, target }) => {
            const { context } = getChartContextMenuContext(focusedItemInfo, gesture, target, series, groups, rootRef, xAxisTitle, yAxisTitle, getDataItem);
            if (gesture === 'keyboard' && context.type === 'item') {
                const elementBounds = focusedItemInfo?.isCurrent
                    ? getDataItemPosition(focusedItemInfo.seriesIndex, focusedItemInfo.groupIndex)
                    : undefined;
                const offsetValue = calculateOffset(isRtl, width, elementBounds);
                return {
                    type: 'offset',
                    context,
                    offsetValue,
                    isRtl,
                    width
                };
            }
            return { type: 'pointer', context };
        },
        contextMenuOptions: {
            isDisabled: !contextMenuConfig //If there is not a context menu renderer we disable the hook
        },
        rootRef,
        contextMenuConfig,
        onContextMenuDismissed
    });
    return { contextMenuContent, contextMenuProps };
};

export { useChartContextMenu as u };
//# sourceMappingURL=useChartContextMenu-61fe46b8.js.map
