"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.Dropdown = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const Dropdown = ({ children, trigger, placement = 'bottom', triggerType = 'click', size = 'md', isOpen: controlledIsOpen, onOpenChange, closeOnSelect = true, closeOnClickOutside = true, className, dropdownClassName, triggerClassName, disabled, offset = 8, arrow = false, portal = false, portalTarget, ariaLabel = 'Dropdown menu', }) => {
    const [uncontrolledIsOpen, setUncontrolledIsOpen] = (0, react_1.useState)(false);
    const dropdownRef = (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 handleClickOutside = (e) => {
        if (closeOnClickOutside &&
            dropdownRef.current &&
            !dropdownRef.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 dropdownClasses = [
        'bf-dropdown',
        `bf-dropdown--${size}`,
        `bf-dropdown--${placement}`,
        arrow ? 'bf-dropdown--arrow' : '',
        dropdownClassName || '',
    ].filter(Boolean).join(' ');
    const triggerClasses = [
        'bf-dropdown__trigger',
        disabled ? 'bf-dropdown__trigger--disabled' : '',
        triggerClassName || '',
    ].filter(Boolean).join(' ');
    const dropdownContent = ((0, jsx_runtime_1.jsxs)("div", { ref: dropdownRef, className: dropdownClasses, role: "menu", "aria-label": ariaLabel, "aria-hidden": !isOpen, tabIndex: -1, children: [arrow && (0, jsx_runtime_1.jsx)("div", { className: "bf-dropdown__arrow" }), (0, jsx_runtime_1.jsx)("div", { className: "bf-dropdown__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-dropdown-container', className].filter(Boolean).join(' '), children: [(0, jsx_runtime_1.jsx)("div", { ref: triggerRef, className: triggerClasses, onClick: handleTriggerClick, onMouseEnter: handleTriggerMouseEnter, onMouseLeave: handleTriggerMouseLeave, role: "button", tabIndex: disabled ? -1 : 0, "aria-haspopup": "menu", "aria-expanded": isOpen, "aria-disabled": disabled, children: trigger }), isOpen && (portal
                ? ReactDOM.createPortal(dropdownContent, portalTarget || document.body)
                : dropdownContent)] }));
};
exports.Dropdown = Dropdown;
const DropdownItem = ({ children, onClick, disabled, icon, shortcut, className, role = 'menuitem', ariaLabel, }) => {
    const itemClasses = [
        'bf-dropdown-item',
        disabled ? 'bf-dropdown-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-dropdown-item__icon", children: icon }), (0, jsx_runtime_1.jsx)("span", { className: "bf-dropdown-item__content", children: children }), shortcut && (0, jsx_runtime_1.jsx)("span", { className: "bf-dropdown-item__shortcut", children: shortcut })] }));
};
const DropdownGroup = ({ children, title, className, role = 'group', ariaLabel, }) => {
    const groupClasses = [
        'bf-dropdown-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-dropdown-group__title", role: "presentation", children: title })), (0, jsx_runtime_1.jsx)("div", { className: "bf-dropdown-group__content", children: children })] }));
};
const DropdownDivider = ({ className, role = 'separator', ariaLabel, }) => {
    const dividerClasses = [
        'bf-dropdown-divider',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsx)("div", { className: dividerClasses, role: role, "aria-label": ariaLabel }));
};
exports.Dropdown.Item = DropdownItem;
exports.Dropdown.Group = DropdownGroup;
exports.Dropdown.Divider = DropdownDivider;
exports.default = exports.Dropdown;
//# sourceMappingURL=Dropdown.js.map