import { jsx } from "react/jsx-runtime";
import { Tooltip } from "@mui/material";
import react, { useCallback, useMemo, useRef } from "react";
import { useTruncationDetection } from "./useTruncationDetection.js";
const makeTooltipRenderInSameDOMTree = {
    PopperProps: {
        disablePortal: true,
        popperOptions: {
            strategy: 'fixed'
        }
    }
};
function ApTooltip({ content, placement = 'bottom', disabled = false, hide, isOpen, delay = false, children, formattedContent, dontUseInternalOnlyDebugForceOpen = false, smartTooltip = false, onTooltipOpen, onTooltipClose, closeOnInteraction = false, ...muiProps }) {
    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 [isTemporarilyClosed, setIsTemporarilyClosed] = react.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 handleInteractionStart = useCallback(()=>{
        if (closeOnInteraction) setIsTemporarilyClosed(true);
    }, [
        closeOnInteraction
    ]);
    const handleMouseLeave = useCallback((e)=>{
        if (closeOnInteraction) setIsTemporarilyClosed(false);
        const childProps = childElementRef.current.props;
        childProps.onMouseLeave?.(e);
    }, [
        closeOnInteraction
    ]);
    const handleInputCapture = useCallback((e)=>{
        handleInteractionStart();
        const childProps = childElementRef.current.props;
        childProps.onInputCapture?.(e);
    }, [
        handleInteractionStart
    ]);
    const handleMouseDown = useCallback((e)=>{
        handleInteractionStart();
        const childProps = childElementRef.current.props;
        childProps.onMouseDown?.(e);
    }, [
        handleInteractionStart
    ]);
    const handleMouseEnter = useCallback((e)=>{
        truncationDetection.check();
        const childProps = childElementRef.current.props;
        childProps.onMouseEnter?.(e);
    }, [
        truncationDetection.check
    ]);
    const closeOnInteractionHandlers = useMemo(()=>{
        if (!closeOnInteraction) return {};
        return {
            onMouseLeave: handleMouseLeave,
            onInputCapture: handleInputCapture,
            onMouseDown: handleMouseDown
        };
    }, [
        closeOnInteraction,
        handleMouseLeave,
        handleInputCapture,
        handleMouseDown
    ]);
    const childrenWithRefSmartTooltip = useMemo(()=>/*#__PURE__*/ react.cloneElement(childElement, {
            ref: setChildRef,
            onMouseEnter: handleMouseEnter,
            ...closeOnInteractionHandlers
        }), [
        childElement,
        setChildRef,
        handleMouseEnter,
        closeOnInteractionHandlers
    ]);
    const childrenWithRefRegular = useMemo(()=>/*#__PURE__*/ react.cloneElement(childElement, closeOnInteractionHandlers), [
        childElement,
        closeOnInteractionHandlers
    ]);
    const childrenWithRef = smartTooltip ? childrenWithRefSmartTooltip : childrenWithRefRegular;
    const debugPopperInstanceRef = react.useRef(null);
    react.useLayoutEffect(()=>{
        let forceUpdateInterval;
        if (dontUseInternalOnlyDebugForceOpen) forceUpdateInterval = window.setInterval(()=>{
            debugPopperInstanceRef.current?.forceUpdate();
        }, 100);
        return ()=>{
            window.clearInterval(forceUpdateInterval);
        };
    }, [
        dontUseInternalOnlyDebugForceOpen
    ]);
    const hasCustomContent = Boolean(formattedContent);
    const tooltipProps = hasCustomContent ? makeTooltipRenderInSameDOMTree : {};
    const isTooltipTitleHidden = useMemo(()=>{
        if (null != isOpen) return !isOpen;
        if (null != hide) return hide;
        if (closeOnInteraction && isTemporarilyClosed) return true;
        if (smartTooltip && !isTruncated) return true;
        return false;
    }, [
        isOpen,
        hide,
        closeOnInteraction,
        isTemporarilyClosed,
        smartTooltip,
        isTruncated
    ]);
    const handleTooltipOpen = useCallback((event)=>{
        if (smartTooltip) truncationDetection.check();
        onTooltipOpen?.(event);
    }, [
        smartTooltip,
        truncationDetection.check,
        onTooltipOpen
    ]);
    const tooltipTitle = isTooltipTitleHidden || disabled ? '' : formattedContent ?? content;
    const { content: _unusedContent, ...safeToSpreadMuiProps } = muiProps;
    return /*#__PURE__*/ jsx(Tooltip, {
        title: tooltipTitle,
        placement: placement,
        enterDelay: delay ? 700 : 0,
        enterNextDelay: delay ? 700 : 0,
        onOpen: handleTooltipOpen,
        onClose: onTooltipClose,
        ...safeToSpreadMuiProps,
        ...tooltipProps,
        ...dontUseInternalOnlyDebugForceOpen && {
            open: true,
            PopperProps: {
                ...'PopperProps' in tooltipProps ? tooltipProps.PopperProps : {},
                popperRef: debugPopperInstanceRef
            }
        },
        ...null != isOpen && {
            open: dontUseInternalOnlyDebugForceOpen ? true : isOpen
        },
        children: childrenWithRef
    });
}
export { ApTooltip };
