import { jsx, jsxs } from "react/jsx-runtime";
import { cn } from "@uipath/apollo-wind";
import { Tooltip, TooltipContent, TooltipPortal, TooltipProvider, TooltipTrigger } from "@uipath/apollo-wind/components/ui/tooltip";
import react, { createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
import { useTruncationDetection } from "../../material/components/ap-tooltip/useTruncationDetection.js";
const HasTooltipProviderContext = /*#__PURE__*/ createContext(false);
function CanvasTooltipProviderMarker({ children }) {
    return /*#__PURE__*/ jsx(HasTooltipProviderContext.Provider, {
        value: true,
        children: children
    });
}
function CanvasTooltip({ content, placement = 'top', smartTooltip = false, delay = false, isOpen, hide, sideOffset, contentClassName, disableHoverableContent, disableSkipDelay, children }) {
    const childElement = useMemo(()=>/*#__PURE__*/ react.isValidElement(children) ? children : /*#__PURE__*/ jsx("span", {
            children: children
        }), [
        children
    ]);
    const childElementRef = useRef(childElement);
    childElementRef.current = childElement;
    const childRef = useRef(null);
    const truncationDetection = useTruncationDetection(childRef);
    const isTruncated = smartTooltip ? truncationDetection.isTruncated : true;
    const [hoverOpen, setHoverOpen] = useState(false);
    const setChildRef = useCallback((node)=>{
        childRef.current = node;
        const childRefProp = childElementRef.current.props.ref;
        if ('function' == typeof childRefProp) childRefProp(node);
        else if (childRefProp && 'object' == typeof childRefProp) childRefProp.current = node;
    }, []);
    const handleMouseEnter = useCallback((e)=>{
        truncationDetection.check();
        const childProps = childElementRef.current.props;
        childProps.onMouseEnter?.(e);
    }, [
        truncationDetection.check
    ]);
    const triggerWithSmartTooltipHandlers = useMemo(()=>/*#__PURE__*/ react.cloneElement(childElement, {
            ref: setChildRef,
            onMouseEnter: handleMouseEnter
        }), [
        childElement,
        setChildRef,
        handleMouseEnter
    ]);
    const triggerWithHandlers = smartTooltip ? triggerWithSmartTooltipHandlers : childElement;
    const handleOpenChange = useCallback((nextOpen)=>{
        if (nextOpen && smartTooltip) truncationDetection.check();
        setHoverOpen(nextOpen);
    }, [
        smartTooltip,
        truncationDetection.check
    ]);
    const isEmptyContent = null == content || false === content || 'string' == typeof content && '' === content.trim();
    const isTooltipHidden = useMemo(()=>{
        if (null != isOpen) return !isOpen;
        if (null != hide) return hide;
        if (smartTooltip && !isTruncated) return true;
        return false;
    }, [
        isOpen,
        hide,
        smartTooltip,
        isTruncated
    ]);
    const effectiveOpen = isEmptyContent || isTooltipHidden ? false : isOpen ?? hoverOpen;
    const hasProvider = useContext(HasTooltipProviderContext);
    const tooltip = /*#__PURE__*/ jsxs(Tooltip, {
        open: effectiveOpen,
        onOpenChange: handleOpenChange,
        delayDuration: delay ? 700 : 200,
        disableHoverableContent: disableHoverableContent,
        children: [
            /*#__PURE__*/ jsx(TooltipTrigger, {
                asChild: true,
                children: triggerWithHandlers
            }),
            /*#__PURE__*/ jsx(TooltipPortal, {
                children: /*#__PURE__*/ jsx(TooltipContent, {
                    side: placement,
                    sideOffset: sideOffset,
                    className: cn('z-1200 max-w-xs wrap-break-word', contentClassName),
                    children: content
                })
            })
        ]
    });
    if (hasProvider && !disableSkipDelay) return tooltip;
    return /*#__PURE__*/ jsx(TooltipProvider, {
        delayDuration: 200,
        skipDelayDuration: disableSkipDelay ? 0 : 100,
        children: tooltip
    });
}
export { CanvasTooltip, CanvasTooltipProviderMarker };
