/* @oracle/oraclejet-preact: undefined */
import { jsx, jsxs, Fragment } from 'preact/jsx-runtime';
import { useRef, useEffect, useState } from 'preact/hooks';
import { S as SvgButton } from './Button-a57d82a7.js';
import { S as SvgMove } from './Move-cf6a8bb2.js';
import { styles } from './hooks/PRIVATE_useVisDragModeControls/themes/useVisDragModeControls.css.js';
import { u as useTranslationBundle } from './useTranslationBundle-09302c20.js';
import { S as SvgZoomAlt } from './ZoomAlt-df2dab3a.js';
import { B as ButtonSetSingle } from './ButtonSetSingle-8a4c0e0c.js';
import { B as ButtonSetItem } from './ButtonSetItem-eb2d43d4.js';
import { I as IconToggleButton } from './IconToggleButton-94cbea2b.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
 */
/**
 * Returns the dragMode during initial render or when dragMode props
 * change (i.e when user hasn't selected yet) based on available dragModes.
 */
function getInitialDragMode(dragMode, showPanButton, showZoomButton, showSelectButton) {
    return showPanButton && (dragMode === 'pan' || dragMode === 'user')
        ? 'pan'
        : showZoomButton && dragMode === 'zoom'
            ? 'zoom'
            : showSelectButton && dragMode === 'select'
                ? 'select'
                : undefined;
}

function VisDragModeControls({ position, divRef, dragMode, onDragModeChange, showZoomButton, showPanButton, showSelectButton }) {
    const { right, left, top, bottom } = position;
    const onButtonAction = ({ value }) => {
        onDragModeChange(value === dragMode ? undefined : value);
    };
    const showOnlyOne = (showPanButton && !showSelectButton && !showZoomButton) ||
        (!showPanButton && !showSelectButton && showZoomButton) ||
        (!showPanButton && showSelectButton && !showZoomButton);
    const translations = useTranslationBundle('@oracle/oraclejet-preact');
    const zoomAriaLabel = translations.vis_marqueeZoomTooltip();
    const panAriaLabel = translations.vis_panTooltip();
    const selectAriaLabel = translations.vis_marqueeSelectTooltip();
    return showZoomButton || showPanButton || showSelectButton ? (jsx("div", { ref: divRef, class: styles.background, "data-oj-private-object": "dragModeControls", style: {
            right,
            left,
            top,
            bottom
        }, children: !showOnlyOne ? (jsxs(ButtonSetSingle, { size: "sm", value: dragMode, onCommit: onButtonAction, display: "icons", variant: "outlined", children: [showPanButton && (jsx(ButtonSetItem, { value: "pan", label: panAriaLabel, startIcon: jsx(SvgMove, {}) })), showZoomButton && (jsx(ButtonSetItem, { value: "zoom", label: zoomAriaLabel, startIcon: jsx(SvgZoomAlt, {}) })), showSelectButton && (jsx(ButtonSetItem, { value: "select", label: selectAriaLabel, startIcon: jsx(SvgButton, {}) }))] })) : (jsxs(Fragment, { children: [showPanButton && (jsx(IconToggleButton, { accessibleLabel: panAriaLabel, isSelected: dragMode === 'pan', onToggle: () => {
                        onDragModeChange(dragMode === 'pan' ? undefined : 'pan');
                    }, variant: "outlined", size: "sm", children: jsx(SvgMove, {}) })), showZoomButton && (jsx(IconToggleButton, { accessibleLabel: zoomAriaLabel, isSelected: dragMode === 'zoom', onToggle: () => {
                        onDragModeChange(dragMode === 'zoom' ? undefined : 'zoom');
                    }, variant: "outlined", size: "sm", children: jsx(SvgZoomAlt, {}) })), showSelectButton && (jsx(IconToggleButton, { accessibleLabel: selectAriaLabel, isSelected: dragMode === 'select', onToggle: () => {
                        onDragModeChange(dragMode === 'select' ? undefined : 'select');
                    }, variant: "outlined", size: "sm", children: jsx(SvgButton, {}) }))] })) })) : null;
}

function useVisDragModeControls({ componentWidth, plotAreaSpace, isRtl, showPanButton, showZoomButton, showSelectButton, isDisabled = false, dragMode }) {
    const dragButtonDivRef = useRef(null);
    const originalDragModeRef = useRef(dragMode);
    const isInitialRenderRef = useRef(true);
    useEffect(() => {
        isInitialRenderRef.current = false;
    }, []);
    const [userDragMode, setUserDragMode] = useState();
    const initialSelection = getInitialDragMode(dragMode, showPanButton, showZoomButton, showSelectButton);
    const dragModeChanged = originalDragModeRef.current != dragMode;
    if (dragModeChanged || isInitialRenderRef.current) {
        originalDragModeRef.current = dragMode;
        setUserDragMode(initialSelection);
    }
    const toggleButtonContent = isDisabled ? undefined : (jsx(VisDragModeControls, { position: {
            top: `${plotAreaSpace.y}px`,
            right: isRtl ? undefined : `${componentWidth - plotAreaSpace.x - plotAreaSpace.width}px`,
            left: isRtl ? `${plotAreaSpace.x}px` : undefined
        }, divRef: dragButtonDivRef, showSelectButton: showSelectButton, showZoomButton: showZoomButton, showPanButton: showPanButton, dragMode: userDragMode, onDragModeChange: setUserDragMode }));
    return {
        dragButtonDivRef: dragButtonDivRef,
        toggleButtonContent: toggleButtonContent,
        userDragMode
    };
}

export { useVisDragModeControls as u };
//# sourceMappingURL=useVisDragModeControls-ed260d12.js.map
