"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AutopilotChatHeaderActionMenu: ()=>AutopilotChatHeaderActionMenu
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const KeyboardArrowRight_namespaceObject = require("@mui/icons-material/KeyboardArrowRight");
var KeyboardArrowRight_default = /*#__PURE__*/ __webpack_require__.n(KeyboardArrowRight_namespaceObject);
const Menu_namespaceObject = require("@mui/material/Menu");
var Menu_default = /*#__PURE__*/ __webpack_require__.n(Menu_namespaceObject);
const MenuItem_namespaceObject = require("@mui/material/MenuItem");
var MenuItem_default = /*#__PURE__*/ __webpack_require__.n(MenuItem_namespaceObject);
const styles_namespaceObject = require("@mui/material/styles");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
var apollo_core_default = /*#__PURE__*/ __webpack_require__.n(apollo_core_namespaceObject);
const external_index_cjs_namespaceObject = require("../../../index.cjs");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const index_cjs_namespaceObject = require("../../../ap-typography/index.cjs");
const use_scheduled_callback_cjs_namespaceObject = require("../../hooks/use-scheduled-callback.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../providers/chat-state-provider.cjs");
const tooltip_cjs_namespaceObject = require("../common/tooltip.cjs");
const StyledMenuItem = (0, styles_namespaceObject.styled)(MenuItem_default())(()=>({
        display: 'flex',
        alignItems: 'center',
        padding: `${apollo_core_default().Spacing.SpacingXs} ${apollo_core_default().Spacing.SpacingS}`,
        gap: apollo_core_default().Spacing.SpacingMicro,
        cursor: 'pointer',
        fontSize: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
        color: 'var(--color-foreground)'
    }));
const NestedMenuContainer = (0, styles_namespaceObject.styled)('div')({
    display: 'flex',
    alignItems: 'center',
    gap: apollo_core_default().Spacing.SpacingMicro,
    width: '100%',
    justifyContent: 'space-between'
});
const MenuItemContent = (0, styles_namespaceObject.styled)('div')({
    display: 'flex',
    alignItems: 'center',
    gap: apollo_core_default().Spacing.SpacingMicro
});
const NestedMenuItem = /*#__PURE__*/ external_react_default().memo(/*#__PURE__*/ external_react_default().forwardRef(({ action, onActionClick, isFirst = false, portalContainer }, ref)=>{
    const [open, setOpen] = external_react_default().useState(false);
    const menuItemRef = external_react_default().useRef(null);
    const submenuRef = external_react_default().useRef(null);
    const closeTimeoutRef = external_react_default().useRef(null);
    const nestedPopoverActionRef = external_react_default().useRef(null);
    const updateNestedPosition = external_react_default().useCallback(()=>{
        if (nestedPopoverActionRef.current) nestedPopoverActionRef.current.updatePosition();
    }, []);
    const scheduleNestedPositionUpdate = (0, use_scheduled_callback_cjs_namespaceObject.useScheduledCallback)(updateNestedPosition);
    const handleNestedTransitionEnter = external_react_default().useCallback(()=>{
        scheduleNestedPositionUpdate();
    }, [
        scheduleNestedPositionUpdate
    ]);
    const handleNestedTransitionEntered = external_react_default().useCallback(()=>{
        scheduleNestedPositionUpdate();
    }, [
        scheduleNestedPositionUpdate
    ]);
    external_react_default().useImperativeHandle(ref, ()=>menuItemRef.current);
    external_react_default().useEffect(()=>()=>{
            if (closeTimeoutRef.current) clearTimeout(closeTimeoutRef.current);
        }, []);
    const handleMouseEnter = external_react_default().useCallback(()=>{
        if (closeTimeoutRef.current) {
            clearTimeout(closeTimeoutRef.current);
            closeTimeoutRef.current = null;
        }
        if (!action.disabled && action.children && action.children.length > 0) setOpen(true);
    }, [
        action.disabled,
        action.children
    ]);
    const handleMouseLeave = external_react_default().useCallback(()=>{
        closeTimeoutRef.current = setTimeout(()=>{
            setOpen(false);
        }, 150);
    }, []);
    const handleSubmenuMouseEnter = external_react_default().useCallback(()=>{
        if (closeTimeoutRef.current) {
            clearTimeout(closeTimeoutRef.current);
            closeTimeoutRef.current = null;
        }
    }, []);
    const handleSubmenuMouseLeave = external_react_default().useCallback(()=>{
        setOpen(false);
    }, []);
    const handleClick = external_react_default().useCallback((event)=>{
        event.stopPropagation();
        if (!action.disabled && !action.children) onActionClick(action);
    }, [
        onActionClick,
        action
    ]);
    const handleKeyDown = external_react_default().useCallback((event)=>{
        if (action.disabled) return;
        if (action.children && action.children.length > 0) {
            if ('ArrowRight' === event.key) {
                event.preventDefault();
                event.stopPropagation();
                setOpen(true);
                requestAnimationFrame(()=>{
                    const firstChild = submenuRef.current?.querySelector('[role="menuitem"]');
                    firstChild?.focus();
                });
            } else if ('Enter' === event.key || ' ' === event.key) {
                event.preventDefault();
                event.stopPropagation();
                setOpen(!open);
            }
        } else if ('Enter' === event.key || ' ' === event.key) {
            event.preventDefault();
            event.stopPropagation();
            onActionClick(action);
        }
    }, [
        action,
        open,
        onActionClick
    ]);
    const handleClose = external_react_default().useCallback(()=>{
        setOpen(false);
        menuItemRef.current?.focus();
    }, []);
    const handleChildClick = external_react_default().useCallback((childAction)=>{
        onActionClick(childAction);
        setOpen(false);
    }, [
        onActionClick
    ]);
    const handleSubmenuKeyDown = external_react_default().useCallback((event)=>{
        if ('ArrowLeft' === event.key) {
            event.preventDefault();
            event.stopPropagation();
            handleClose();
        } else if ('ArrowUp' === event.key || 'ArrowDown' === event.key) {
            event.preventDefault();
            event.stopPropagation();
            const menuItems = submenuRef.current?.querySelectorAll('[role="menuitem"]:not([disabled])');
            if (!menuItems || 0 === menuItems.length) return;
            const currentIndex = Array.from(menuItems).findIndex((item)=>item === document.activeElement);
            let nextIndex;
            nextIndex = 'ArrowDown' === event.key ? -1 === currentIndex ? 0 : (currentIndex + 1) % menuItems.length : -1 === currentIndex ? menuItems.length - 1 : (currentIndex - 1 + menuItems.length) % menuItems.length;
            menuItems[nextIndex]?.focus();
        } else if ('ArrowRight' === event.key) {
            event.preventDefault();
            event.stopPropagation();
        }
    }, [
        handleClose
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(StyledMenuItem, {
                ref: menuItemRef,
                onClick: handleClick,
                onKeyDown: handleKeyDown,
                onMouseEnter: handleMouseEnter,
                onMouseLeave: handleMouseLeave,
                disabled: action.disabled,
                tabIndex: isFirst ? 0 : -1,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(tooltip_cjs_namespaceObject.AutopilotChatTooltip, {
                    placement: "left",
                    disableInteractive: true,
                    title: action.description ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                        color: 'var(--color-foreground-inverse)',
                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                        children: action.description
                    }) : null,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(NestedMenuContainer, {
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(MenuItemContent, {
                                children: [
                                    action.icon && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApIcon, {
                                        variant: "outlined",
                                        name: action.icon,
                                        size: apollo_core_default().Icon.IconXs
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                                        children: action.name
                                    })
                                ]
                            }),
                            action.children && action.children.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(KeyboardArrowRight_default(), {
                                fontSize: "inherit"
                            })
                        ]
                    })
                })
            }),
            action.children && action.children.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Menu_default(), {
                hideBackdrop: true,
                style: {
                    pointerEvents: 'none'
                },
                open: open,
                anchorEl: menuItemRef.current,
                onClose: handleClose,
                container: portalContainer,
                anchorOrigin: {
                    vertical: 'top',
                    horizontal: 'left'
                },
                transformOrigin: {
                    vertical: 'top',
                    horizontal: 'right'
                },
                autoFocus: false,
                disableAutoFocus: true,
                disableEnforceFocus: true,
                action: nestedPopoverActionRef,
                MenuListProps: {
                    sx: {
                        padding: 0
                    },
                    disableListWrap: true
                },
                slotProps: {
                    paper: {
                        sx: {
                            marginRight: apollo_core_default().Spacing.SpacingXs
                        }
                    }
                },
                TransitionProps: {
                    onEnter: handleNestedTransitionEnter,
                    onEntered: handleNestedTransitionEntered
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    ref: submenuRef,
                    style: {
                        pointerEvents: 'auto'
                    },
                    onKeyDown: handleSubmenuKeyDown,
                    onMouseEnter: handleSubmenuMouseEnter,
                    onMouseLeave: handleSubmenuMouseLeave,
                    children: action.children.map((child)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(StyledMenuItem, {
                            onClick: ()=>handleChildClick(child),
                            onKeyDown: (e)=>{
                                if ('Enter' === e.key || ' ' === e.key) {
                                    e.preventDefault();
                                    e.stopPropagation();
                                    handleChildClick(child);
                                }
                            },
                            disabled: child.disabled,
                            tabIndex: 0,
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(tooltip_cjs_namespaceObject.AutopilotChatTooltip, {
                                placement: "left",
                                disableInteractive: true,
                                title: child.description ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                    color: 'var(--color-foreground-inverse)',
                                    variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                                    children: child.description
                                }) : null,
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(MenuItemContent, {
                                    children: [
                                        child.icon && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApIcon, {
                                            variant: "outlined",
                                            name: child.icon,
                                            size: apollo_core_default().Icon.IconXs
                                        }),
                                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                            variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                                            children: child.name
                                        })
                                    ]
                                })
                            })
                        }, child.id))
                })
            })
        ]
    });
}));
const AutopilotChatHeaderActionMenu = /*#__PURE__*/ external_react_default().memo(({ actions, onActionClick, anchorEl, open, onClose })=>{
    const { portalContainer } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const firstItemRef = external_react_default().useRef(null);
    const popoverActionRef = external_react_default().useRef(null);
    const updatePosition = external_react_default().useCallback(()=>{
        if (popoverActionRef.current) popoverActionRef.current.updatePosition();
    }, []);
    const schedulePositionUpdate = (0, use_scheduled_callback_cjs_namespaceObject.useScheduledCallback)(updatePosition);
    const handleTransitionEnter = external_react_default().useCallback(()=>{
        schedulePositionUpdate();
    }, [
        schedulePositionUpdate
    ]);
    const handleTransitionEntered = external_react_default().useCallback(()=>{
        if (firstItemRef.current) firstItemRef.current.focus();
        schedulePositionUpdate();
    }, [
        schedulePositionUpdate
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Menu_default(), {
        open: open,
        anchorEl: anchorEl,
        onClose: onClose,
        container: portalContainer,
        anchorOrigin: {
            vertical: 'bottom',
            horizontal: 'right'
        },
        transformOrigin: {
            vertical: 'top',
            horizontal: 'right'
        },
        variant: "menu",
        MenuListProps: {
            autoFocusItem: false
        },
        action: popoverActionRef,
        TransitionProps: {
            onEnter: handleTransitionEnter,
            onEntered: handleTransitionEntered
        },
        children: actions.map((action, index)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(NestedMenuItem, {
                action: action,
                onActionClick: onActionClick,
                ref: 0 === index ? firstItemRef : void 0,
                isFirst: 0 === index,
                portalContainer: portalContainer
            }, action.id))
    });
});
exports.AutopilotChatHeaderActionMenu = __webpack_exports__.AutopilotChatHeaderActionMenu;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatHeaderActionMenu"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
