/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.108.1
 * Released under the MIT license.
 */

import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { FocusableProvider } from '@react-aria/focus';
import { isValidElement, useRef, } from 'react';
import { useOverlayPosition, useTooltipTrigger, } from 'react-aria';
import { useTooltipTriggerState } from 'react-stately';
import { Block } from '../../Block.js';
import { ActiveZone } from '../../content/ActiveZone/ActiveZone.js';
import { DisplayTransition } from '../../helpers/DisplayTransition/DisplayTransition.js';
import { Portal } from '../../portal/index.js';
import { TooltipContext } from './context.js';
const DEFAULT_OFFSET = 8; // Offset needed to reach 4px/5px (med/large) distance between tooltip and trigger button
const DEFAULT_CROSS_OFFSET = 0;
// Type guards
function isTriggerFunction(value) {
    return typeof value === 'function';
}
function isReactElement(value) {
    return isValidElement(value);
}
/**
 * TooltipTrigger wraps around a trigger element and a Tooltip. It handles opening and closing
 * the Tooltip when the user hovers over or focuses the trigger, and positioning the Tooltip
 * relative to the trigger.
 */
export function TooltipTrigger(props) {
    let { children, activeWrap, targetRef: externalRef, crossOffset = DEFAULT_CROSS_OFFSET, isDisabled, isMaterial, isLight, offset = DEFAULT_OFFSET, trigger: triggerAction, delay = 250, isOpen, onOpenChange, defaultOpen, disableFocusableProvider, } = props;
    // Extract both children without using React.Children.toArray so we don't lose
    // function-as-child (render-prop) which React.Children filters out.
    const rawChildrenArray = Array.isArray(children)
        ? children
        : [children];
    const [trigger, tooltip] = rawChildrenArray;
    // Type guard the tooltip
    if (!isReactElement(tooltip)) {
        throw new Error('CubeUIKit: TooltipTrigger expects the second child to be a valid React element (Tooltip component).');
    }
    // Type guard the trigger
    if (!isTriggerFunction(trigger) &&
        !isReactElement(trigger) &&
        typeof trigger !== 'string') {
        throw new Error('CubeUIKit: TooltipTrigger expects the first child to be a function, React element, or string.');
    }
    // Show deprecation warning for activeWrap
    if (activeWrap && process.env.NODE_ENV === 'development') {
        console.warn('CubeUIKit: The `activeWrap` prop is deprecated. Use function-as-first-child pattern instead.');
    }
    let internalRef = useRef(null);
    let tooltipTriggerRef = externalRef ?? internalRef;
    let overlayRef = useRef(null);
    let state = useTooltipTriggerState({ delay, ...props, isDismissable: false });
    let { triggerProps, tooltipProps } = useTooltipTrigger({
        trigger: triggerAction,
        delay,
        isOpen,
        onOpenChange,
        isDismissable: false,
        defaultOpen,
    }, state, tooltipTriggerRef);
    let { overlayProps, arrowProps, placement, updatePosition } = useOverlayPosition({
        placement: props.placement || 'top',
        targetRef: tooltipTriggerRef,
        overlayRef,
        offset,
        crossOffset,
        isDismissable: false,
        isOpen: state.isOpen,
    });
    const tooltipContextValue = {
        state,
        placement: placement || props.placement || 'top',
        ref: overlayRef,
        overlayProps,
        arrowProps,
        minOffset: 'var(--gap)',
        minScale: '1',
        isMaterial,
        isLight,
        updatePosition,
        ...tooltipProps,
    };
    // Function trigger pattern (new)
    if (isTriggerFunction(trigger)) {
        return (_jsxs(_Fragment, { children: [trigger(triggerProps, tooltipTriggerRef), _jsx(DisplayTransition, { isShown: state.isOpen, children: ({ phase, isShown, ref: transitionRef }) => (_jsx(TooltipContext.Provider, { value: {
                            ...tooltipContextValue,
                            phase,
                            isShown,
                            ref: overlayRef,
                            transitionRef,
                        }, children: _jsx(Portal, { children: tooltip }) })) })] }));
    }
    // Legacy element/string trigger pattern
    if (!activeWrap && typeof trigger === 'string') {
        console.warn('CubeUIKit: Tooltips are only supported on elements that are both focusable and hoverable. To solve this issue you can: 1) Use active element as a trigger (`Button`, `Link`, etc); 2) Use function-as-first-child pattern to manually apply trigger props; 3) Use `ActiveZone` component to manually wrap the content.');
        return _jsx(Block, { children: trigger });
    }
    function onClick(e) {
        e?.currentTarget?.parentNode.click();
    }
    const triggerContent = (_jsxs(_Fragment, { children: [activeWrap ? (_jsx(ActiveZone, { onClick: onClick, children: trigger })) : (trigger), _jsx(DisplayTransition, { isShown: state.isOpen && !isDisabled, children: ({ phase, isShown, ref: transitionRef }) => (_jsx(TooltipContext.Provider, { value: {
                        ...tooltipContextValue,
                        phase,
                        isShown,
                        ref: overlayRef,
                        transitionRef,
                    }, children: _jsx(Portal, { children: tooltip }) })) })] }));
    if (disableFocusableProvider) {
        return triggerContent;
    }
    return (_jsx(FocusableProvider, { ...triggerProps, ref: tooltipTriggerRef, children: triggerContent }));
}

        