"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatbotHeaderMenu = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_core_1 = require("@patternfly/react-core");
const bars_icon_1 = __importDefault(require("@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] = (0, react_1.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 = (0, react_1.useRef)(isDrawerOpen);
    const buttonRef = (0, react_1.useRef)(null);
    (0, react_1.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 = (0, react_1.useMemo)(() => ((0, jsx_runtime_1.jsx)(react_core_1.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: (0, jsx_runtime_1.jsx)(react_core_1.Icon, { size: isCompact ? 'lg' : 'xl', isInline: true, children: (0, jsx_runtime_1.jsx)(bars_icon_1.default, {}) }), size: isCompact ? 'sm' : undefined }, props))), 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [isCompact, menuAriaLabel, onMenuToggle, innerRef, buttonRef]);
    return ((0, jsx_runtime_1.jsx)("div", { className: `pf-chatbot__menu ${className}`, children: isDrawerAnimating ? (button) : ((0, jsx_runtime_1.jsx)(react_core_1.Tooltip, Object.assign({ content: tooltipContent, position: "bottom", 
            // prevents VO announcements of both aria label and tooltip
            aria: "none" }, tooltipProps, { children: button }))) }));
};
exports.ChatbotHeaderMenu = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(ChatbotHeaderMenuBase, Object.assign({ innerRef: ref }, props))));
