"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Menu = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const Menu = ({ children, trigger, placement = 'bottom', triggerType = 'click', size = 'md', isOpen: controlledIsOpen, onOpenChange, closeOnSelect = true, closeOnClickOutside = true, className, menuClassName, triggerClassName, disabled, offset = 8, arrow = false, portal = false, portalTarget, ariaLabel = 'Menu', }) => {
    const [uncontrolledIsOpen, setUncontrolledIsOpen] = (0, react_1.useState)(false);
    const menuRef = (0, react_1.useRef)(null);
    const triggerRef = (0, react_1.useRef)(null);
    const isControlled = controlledIsOpen !== undefined;
    const isOpen = isControlled ? controlledIsOpen : uncontrolledIsOpen;
    const handleOpenChange = (newIsOpen) => {
        if (!isControlled) {
            setUncontrolledIsOpen(newIsOpen);
        }
        onOpenChange?.(newIsOpen);
    };
    const handleTriggerClick = () => {
        if (disabled)
            return;
        if (triggerType === 'click') {
            handleOpenChange(!isOpen);
        }
    };
    const handleTriggerMouseEnter = () => {
        if (disabled)
            return;
        if (triggerType === 'hover') {
            handleOpenChange(true);
        }
    };
    const handleTriggerMouseLeave = () => {
        if (disabled)
            return;
        if (triggerType === 'hover') {
            handleOpenChange(false);
        }
    };
    const handleTriggerContextMenu = (e) => {
        if (disabled)
            return;
        if (triggerType === 'contextMenu') {
            e.preventDefault();
            handleOpenChange(!isOpen);
        }
    };
    const handleClickOutside = (e) => {
        if (closeOnClickOutside &&
            menuRef.current &&
            !menuRef.current.contains(e.target) &&
            triggerRef.current &&
            !triggerRef.current.contains(e.target)) {
            handleOpenChange(false);
        }
    };
    (0, react_1.useEffect)(() => {
        if (isOpen && closeOnClickOutside) {
            document.addEventListener('mousedown', handleClickOutside);
        }
        return () => {
            document.removeEventListener('mousedown', handleClickOutside);
        };
    }, [isOpen, closeOnClickOutside]);
    const menuClasses = [
        'bf-menu',
        `bf-menu--${size}`,
        `bf-menu--${placement}`,
        arrow ? 'bf-menu--arrow' : '',
        menuClassName || '',
    ].filter(Boolean).join(' ');
    const triggerClasses = [
        'bf-menu__trigger',
        disabled ? 'bf-menu__trigger--disabled' : '',
        triggerClassName || '',
    ].filter(Boolean).join(' ');
    const menuContent = ((0, jsx_runtime_1.jsxs)("div", { ref: menuRef, className: menuClasses, role: "menu", "aria-label": ariaLabel, "aria-hidden": !isOpen, tabIndex: -1, children: [arrow && (0, jsx_runtime_1.jsx)("div", { className: "bf-menu__arrow" }), (0, jsx_runtime_1.jsx)("div", { className: "bf-menu__content", children: react_1.default.Children.map(children, (child) => {
                    if (!react_1.default.isValidElement(child))
                        return null;
                    // Clone child and add closeOnSelect handler
                    return react_1.default.cloneElement(child, {
                        onClick: () => {
                            child.props.onClick?.();
                            if (closeOnSelect) {
                                handleOpenChange(false);
                            }
                        },
                    });
                }) })] }));
    return ((0, jsx_runtime_1.jsxs)("div", { className: ['bf-menu-container', className].filter(Boolean).join(' '), children: [(0, jsx_runtime_1.jsx)("div", { ref: triggerRef, className: triggerClasses, onClick: handleTriggerClick, onMouseEnter: handleTriggerMouseEnter, onMouseLeave: handleTriggerMouseLeave, onContextMenu: handleTriggerContextMenu, role: "button", tabIndex: disabled ? -1 : 0, "aria-haspopup": "menu", "aria-expanded": isOpen, "aria-disabled": disabled, children: trigger }), isOpen && (portal
                ? ReactDOM.createPortal(menuContent, portalTarget || document.body)
                : menuContent)] }));
};
exports.Menu = Menu;
const MenuItem = ({ children, onClick, disabled, icon, shortcut, className, role = 'menuitem', ariaLabel, }) => {
    const itemClasses = [
        'bf-menu-item',
        disabled ? 'bf-menu-item--disabled' : '',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsxs)("div", { className: itemClasses, role: role, onClick: disabled ? undefined : onClick, "aria-disabled": disabled, "aria-label": ariaLabel, tabIndex: disabled ? -1 : 0, children: [icon && (0, jsx_runtime_1.jsx)("span", { className: "bf-menu-item__icon", children: icon }), (0, jsx_runtime_1.jsx)("span", { className: "bf-menu-item__content", children: children }), shortcut && (0, jsx_runtime_1.jsx)("span", { className: "bf-menu-item__shortcut", children: shortcut })] }));
};
const MenuGroup = ({ children, title, className, role = 'group', ariaLabel, }) => {
    const groupClasses = [
        'bf-menu-group',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsxs)("div", { className: groupClasses, role: role, "aria-label": ariaLabel, children: [title && ((0, jsx_runtime_1.jsx)("div", { className: "bf-menu-group__title", role: "presentation", children: title })), (0, jsx_runtime_1.jsx)("div", { className: "bf-menu-group__content", children: children })] }));
};
const MenuDivider = ({ className, role = 'separator', ariaLabel, }) => {
    const dividerClasses = [
        'bf-menu-divider',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsx)("div", { className: dividerClasses, role: role, "aria-label": ariaLabel }));
};
exports.Menu.Item = MenuItem;
exports.Menu.Group = MenuGroup;
exports.Menu.Divider = MenuDivider;
exports.default = exports.Menu;
//# sourceMappingURL=Menu.js.map