"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__, {
    ApMenu: ()=>ApMenu
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const ArrowRight_namespaceObject = require("@mui/icons-material/ArrowRight");
var ArrowRight_default = /*#__PURE__*/ __webpack_require__.n(ArrowRight_namespaceObject);
const material_namespaceObject = require("@mui/material");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
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 external_constants_cjs_namespaceObject = require("./constants.cjs");
const anchorOriginDefault = {
    vertical: 'bottom',
    horizontal: 'left'
};
const transformOriginDefault = {
    vertical: 'top',
    horizontal: 'left'
};
function ApMenu(props) {
    const { menuItems, onClose, anchorEl, maxHeight, width = 246, dontUseInternalOnlyDebugMode, anchorOrigin = anchorOriginDefault, transformOrigin = transformOriginDefault, slotProps } = props;
    const [openMenus, setOpenMenus] = (0, external_react_namespaceObject.useState)(new Set());
    const [submenuAnchors, setSubmenuAnchors] = (0, external_react_namespaceObject.useState)({});
    const mergedSlotProps = (0, external_react_namespaceObject.useMemo)(()=>{
        const defaultPaperSx = {
            maxHeight,
            width
        };
        if (!slotProps) return {
            paper: {
                sx: defaultPaperSx
            }
        };
        if (!slotProps.paper) return {
            ...slotProps,
            paper: {
                sx: defaultPaperSx
            }
        };
        if ('function' == typeof slotProps.paper) {
            const paperFn = slotProps.paper;
            return {
                ...slotProps,
                paper: (ownerState)=>{
                    const userPaperProps = paperFn(ownerState);
                    return {
                        ...userPaperProps,
                        sx: [
                            defaultPaperSx,
                            userPaperProps?.sx
                        ].filter((sx)=>null != sx)
                    };
                }
            };
        }
        return {
            ...slotProps,
            paper: {
                ...slotProps.paper,
                sx: [
                    defaultPaperSx,
                    slotProps.paper?.sx
                ].filter((sx)=>null != sx)
            }
        };
    }, [
        slotProps,
        maxHeight,
        width
    ]);
    const handleMenuToggle = (0, external_react_namespaceObject.useCallback)((title, anchor)=>{
        setOpenMenus((prev)=>{
            const newOpenMenus = new Set(prev);
            if (newOpenMenus.has(title)) {
                newOpenMenus.delete(title);
                if (anchor) setSubmenuAnchors((prevAnchors)=>({
                        ...prevAnchors,
                        [title]: null
                    }));
            } else {
                newOpenMenus.add(title);
                if (anchor) setSubmenuAnchors((prevAnchors)=>({
                        ...prevAnchors,
                        [title]: anchor
                    }));
            }
            return newOpenMenus;
        });
    }, [
        setOpenMenus,
        setSubmenuAnchors
    ]);
    const renderSubMenu = (0, external_react_namespaceObject.useCallback)((item)=>{
        const anchor = submenuAnchors[item.title ?? ''];
        if (!item.subItems || !anchor) return null;
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Menu, {
            anchorEl: anchor,
            open: openMenus.has(item.title ?? ''),
            onClose: ()=>handleMenuToggle(item.title ?? ''),
            slotProps: {
                paper: {
                    style: {
                        maxHeight,
                        width,
                        marginLeft: width,
                        marginTop: -32
                    }
                }
            },
            children: item.subItems.map((subItem, index)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_react_default().Fragment, {
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(material_namespaceObject.MenuItem, {
                            onClick: subItem.subItems ? (event)=>{
                                event.stopPropagation();
                                handleMenuToggle(subItem.title ?? '', event.currentTarget);
                            } : subItem.onClick,
                            disabled: subItem.disabled,
                            sx: {
                                minHeight: subItem.subtitle ? '64px !important' : '32px !important'
                            },
                            children: [
                                subItem.startIcon,
                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.ListItemText, {
                                    primary: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeM,
                                        color: "var(--color-foreground)",
                                        style: subItem.subtitle ? {
                                            marginBottom: 4
                                        } : void 0,
                                        children: subItem.title
                                    }),
                                    secondary: subItem.subtitle ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                                        color: "var(--color-foreground-de-emp)",
                                        children: subItem.subtitle
                                    }) : void 0
                                }),
                                subItem.subItems && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ArrowRight_default(), {
                                    sx: {
                                        marginLeft: apollo_core_namespaceObject.Padding.PadS,
                                        width: 16
                                    }
                                })
                            ]
                        }),
                        subItem.subItems && renderSubMenu(subItem)
                    ]
                }, index))
        });
    }, [
        handleMenuToggle,
        maxHeight,
        openMenus,
        submenuAnchors,
        width
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Menu, {
        anchorEl: anchorEl,
        open: props.isOpen,
        onClose: onClose,
        slotProps: mergedSlotProps,
        anchorOrigin: anchorOrigin,
        transformOrigin: transformOrigin,
        children: (dontUseInternalOnlyDebugMode ? external_constants_cjs_namespaceObject.debugItems : menuItems).map((item, index)=>{
            if ('section' === item.variant) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                style: {
                    display: 'flex',
                    alignItems: 'center',
                    paddingLeft: apollo_core_namespaceObject.Padding.PadXxxl,
                    paddingRight: apollo_core_namespaceObject.Padding.PadL,
                    height: '32px'
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                    variant: apollo_core_namespaceObject.FontVariantToken.fontSizeMBold,
                    children: item.title
                })
            }, index);
            if ('separator' === item.variant) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Divider, {
                sx: {
                    marginLeft: apollo_core_namespaceObject.Padding.PadL,
                    marginRight: apollo_core_namespaceObject.Padding.PadL
                }
            }, index);
            const hasSubItems = item.subItems && item.subItems.length > 0;
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_react_default().Fragment, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(material_namespaceObject.MenuItem, {
                        tabIndex: 0,
                        sx: {
                            display: 'flex',
                            alignItems: 'center',
                            paddingLeft: apollo_core_namespaceObject.Padding.PadXxxl,
                            paddingRight: apollo_core_namespaceObject.Padding.PadL,
                            minHeight: item.subtitle ? '64px !important' : '32px !important',
                            height: '32px',
                            ...item.isSelected && {
                                backgroundColor: 'var(--color-background-selected) !important',
                                boxShadow: 'inset 4px 0px 0px var(--color-selection-indicator)'
                            }
                        },
                        onClick: (event)=>{
                            if (item.onClick) item.onClick();
                            if (item.subItems) handleMenuToggle(item.title ?? '', event.currentTarget);
                        },
                        disabled: item.disabled,
                        children: [
                            item.startIcon && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                style: {
                                    marginRight: apollo_core_namespaceObject.Padding.PadL,
                                    display: 'flex'
                                },
                                children: item.startIcon
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.ListItemText, {
                                primary: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                    variant: apollo_core_namespaceObject.FontVariantToken.fontSizeM,
                                    color: "var(--color-foreground)",
                                    style: item.subtitle ? {
                                        marginBottom: 4
                                    } : void 0,
                                    children: item.title
                                }),
                                secondary: item.subtitle ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                    variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                                    color: "var(--color-foreground-de-emp)",
                                    children: item.subtitle
                                }) : void 0
                            }),
                            item.endIcon,
                            hasSubItems && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ArrowRight_default(), {
                                sx: {
                                    marginLeft: apollo_core_namespaceObject.Padding.PadS,
                                    width: 16
                                }
                            })
                        ]
                    }),
                    hasSubItems && renderSubMenu(item)
                ]
            }, index);
        })
    });
}
exports.ApMenu = __webpack_exports__.ApMenu;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ApMenu"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
