var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { jsx as _jsx } from "react/jsx-runtime";
import { forwardRef, useEffect, useMemo, useRef, useState } from 'react';
import { Button, Icon, Tooltip } from '@patternfly/react-core';
import BarsIcon from '@patternfly/react-icons/dist/esm/icons/bars-icon';
const ChatbotHeaderMenuBase = (_a) => {
    var { className, onMenuToggle, tooltipProps, menuAriaLabel = 'Chat history menu', innerRef, tooltipContent = 'Chat history menu', isCompact } = _a, props = __rest(_a, ["className", "onMenuToggle", "tooltipProps", "menuAriaLabel", "innerRef", "tooltipContent", "isCompact"]);
    const [isDrawerAnimating, setIsDrawerAnimating] = useState(false);
    // I'd like to use a prop here later if this works
    const drawerState = props['aria-expanded'];
    const isDrawerOpen = drawerState === true;
    const prevDrawerStateRef = useRef(isDrawerOpen);
    const buttonRef = useRef(null);
    useEffect(() => {
        if (drawerState !== undefined) {
            const wasDrawerOpen = prevDrawerStateRef.current === true;
            const isDrawerClosing = wasDrawerOpen && !isDrawerOpen;
            setIsDrawerAnimating(true);
            const timeout = setTimeout(() => {
                setIsDrawerAnimating(false);
                if (isDrawerClosing) {
                    requestAnimationFrame(() => {
                        var _a;
                        (_a = buttonRef.current) === null || _a === void 0 ? void 0 : _a.focus();
                    });
                }
            }, 350);
            prevDrawerStateRef.current = isDrawerOpen;
            return () => clearTimeout(timeout);
        }
    }, [drawerState, isDrawerOpen]);
    const button = useMemo(() => (_jsx(Button, Object.assign({ className: `pf-chatbot__button--toggle-menu ${isCompact ? 'pf-m-compact' : ''}`, variant: "plain", onClick: onMenuToggle, "aria-label": menuAriaLabel, ref: innerRef !== null && innerRef !== void 0 ? innerRef : buttonRef, icon: _jsx(Icon, { size: isCompact ? 'lg' : 'xl', isInline: true, children: _jsx(BarsIcon, {}) }), size: isCompact ? 'sm' : undefined }, props))), 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [isCompact, menuAriaLabel, onMenuToggle, innerRef, buttonRef]);
    return (_jsx("div", { className: `pf-chatbot__menu ${className}`, children: isDrawerAnimating ? (button) : (_jsx(Tooltip, Object.assign({ content: tooltipContent, position: "bottom", 
            // prevents VO announcements of both aria label and tooltip
            aria: "none" }, tooltipProps, { children: button }))) }));
};
export const ChatbotHeaderMenu = forwardRef((props, ref) => (_jsx(ChatbotHeaderMenuBase, Object.assign({ innerRef: ref }, props))));
