import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
import { forwardRef, createContext, useState, useCallback, useContext, useMemo } from 'react';
import { DropdownMenuTrigger, DropdownMenu as DropdownMenu$1, DropdownMenuContent, DropdownMenuSeparator, DropdownMenuLabel, DropdownMenuGroup, DropdownMenuItem, DropdownMenuSub, DropdownMenuSubTrigger, DropdownMenuSubContent } from '../primitives/DropdownMenu.js';
import { useTranslation } from 'react-i18next';
import { c as cn } from '../utils-DdHUxIdC.js';
import { r as renderIcon } from '../renderIcon-tlvMyboj.js';
import { Button } from '../primitives/Button.js';
import { ButtonGroup } from '../primitives/ButtonGroup.js';
import { C as ChevronDown } from '../chevron-down-BNi0ntys.js';
import '../chevron-right-DQnrO-ek.js';
import '../createLucideIcon-C8ycilSN.js';
import '../check-Cpkv29p1.js';
import '../index-DNzunGHb.js';
import '../index-BtkPdosV.js';
import '../index-CiyxEyB0.js';
import '../index-Cor698lu.js';
import '../index-DOzu5J1s.js';
import '../index-DhnfW8wQ.js';
import 'react-dom';
import '../index-OI_tWuLx.js';
import '../index-Dd1i1d2M.js';
import '../index-d0BBQf5t.js';
import '../index-BNPynZWM.js';
import '../index-CSUSJzOJ.js';
import '../index-B4_fVWDx.js';
import '../index-zW4GjM5L.js';
import '../index-C3hByjk3.js';
import '../floating-ui.react-dom-D8_f_WWh.js';
import '../index-BYZaLNq1.js';
import '../index-fV_U4ZJM.js';
import '../index-yFgkK_AM.js';
import '../index-DT56XkyA.js';
import '../button-D6uWDFCs.js';
import '../index-D7Zy7P05.js';
import '../index-CfriMyrd.js';
import '../separator-BbhgePmX.js';

const SIDE_MAP = {
    top: "top",
    bottom: "bottom",
    left: "left",
    right: "right",
    "top-start": "top",
    "top-end": "top",
    "bottom-start": "bottom",
    "bottom-end": "bottom",
    "left-start": "left",
    "left-end": "left",
    "right-start": "right",
    "right-end": "right",
    auto: "bottom",
};
const ALIGN_MAP = {
    top: "center",
    bottom: "center",
    left: "center",
    right: "center",
    "top-start": "start",
    "top-end": "end",
    "bottom-start": "start",
    "bottom-end": "end",
    "left-start": "start",
    "left-end": "end",
    "right-start": "start",
    "right-end": "end",
    auto: "end",
};
const BUTTON_STYLE_MAP = {
    primary: "default",
    secondary: "secondary",
    tertiary: "outline",
    text: "ghost",
    danger: "destructive",
    "danger-text": "ghost",
    link: "link",
};
const BUTTON_SIZE_MAP = {
    small: "sm",
    medium: "default",
    large: "lg",
};
const MENU_ITEM_STYLE_MAP = {
    default: "default",
    danger: "destructive",
};
const CHEVRON_SIZE_MAP = {
    small: "size-3.5",
    medium: "size-4",
    large: "size-4",
};

const SplitTrigger = forwardRef(({ label, icon, buttonStyle, buttonSize, buttonProps, disabled, isOpen, onClick, actionButtonRef, className, ...otherProps }, ref) => {
    const { t } = useTranslation();
    const variant = BUTTON_STYLE_MAP[buttonStyle];
    const size = BUTTON_SIZE_MAP[buttonSize];
    return (jsxs(ButtonGroup, { ref: ref, className: cn("neeto-ui-action-dropdown", className), ...otherProps, children: [jsxs(Button, { ref: actionButtonRef, type: "button", variant: variant, size: size, disabled: disabled, onClick: onClick, "data-testid": "action-dropdown-btn", ...buttonProps, children: [icon && renderIcon(icon, "size-4"), label && jsx("span", { children: label })] }), jsx(DropdownMenuTrigger, { asChild: true, disabled: disabled, children: jsx(Button, { type: "button", variant: variant, size: size, disabled: disabled, "aria-label": t("neetoatoms.dropdownMenu.openMenu", "Open dropdown menu"), className: "px-1.5", children: jsx(ChevronDown, { "aria-hidden": "true", className: cn(CHEVRON_SIZE_MAP[buttonSize], "shrink-0 transition-transform duration-200", isOpen && "rotate-180") }) }) })] }));
});
SplitTrigger.displayName = "Dropdown.SplitTrigger";

const TriggerButton = forwardRef(({ label, icon, buttonStyle, buttonSize, buttonProps, disabled, isOpen, onClick, className, ...otherProps }, ref) => (jsx(Button, { ref: ref, type: "button", variant: BUTTON_STYLE_MAP[buttonStyle], size: BUTTON_SIZE_MAP[buttonSize], disabled: disabled, className: cn(buttonStyle === "danger-text" &&
        "text-destructive hover:text-destructive", "gap-1.5", className), onClick: onClick, ...otherProps, ...buttonProps, children: jsxs("span", { className: "inline-flex items-center gap-1.5", children: [icon && renderIcon(icon, "size-4"), label && jsx("span", { children: label }), jsx(ChevronDown, { "aria-hidden": "true", className: cn(CHEVRON_SIZE_MAP[buttonSize], "shrink-0 transition-transform duration-200", isOpen && "rotate-180") })] }) })));
TriggerButton.displayName = "Dropdown.TriggerButton";

const DropdownContext = createContext({ closeOnSelect: true });

const DropdownBase = ({ label, icon, buttonStyle = "primary", buttonSize = "medium", buttonProps, customTarget, disabled = false, children, position = "bottom-end", closeOnEsc = true, closeOnSelect = true, closeOnOutsideClick = true, isOpen: isOpenProp, onClose, onClick, className, triggerRef, dropdownProps, }) => {
    const [internalOpen, setInternalOpen] = useState(false);
    const isControlled = isOpenProp !== undefined;
    const open = isControlled ? isOpenProp : internalOpen;
    const handleOpenChange = useCallback((next) => {
        if (!isControlled)
            setInternalOpen(next);
        if (!next)
            onClose?.();
    }, [isControlled, onClose]);
    const side = SIDE_MAP[position] ?? "bottom";
    const align = ALIGN_MAP[position] ?? "end";
    // Split-button mode: onClick provided without a customTarget.
    const isSplit = !!onClick && !customTarget;
    const sharedTriggerProps = {
        label,
        icon,
        buttonStyle,
        buttonSize,
        buttonProps,
        disabled,
        isOpen: open,
        onClick,
    };
    const triggerNode = isSplit ? (jsx(SplitTrigger, { ...sharedTriggerProps, actionButtonRef: triggerRef, className: className })) : (jsx(DropdownMenuTrigger, { asChild: true, disabled: disabled, className: className, children: customTarget ? (typeof customTarget === "function" ? (customTarget({ isOpen: open })) : (customTarget)) : (jsx(TriggerButton, { ref: triggerRef, ...sharedTriggerProps })) }));
    return (jsx(DropdownContext.Provider, { value: { closeOnSelect }, children: jsxs(DropdownMenu$1, { open: open, onOpenChange: handleOpenChange, modal: closeOnOutsideClick, children: [triggerNode, jsx(DropdownMenuContent, { side: side, align: align, onEscapeKeyDown: closeOnEsc ? undefined : e => e.preventDefault(), onInteractOutside: closeOnOutsideClick ? undefined : e => e.preventDefault(), ...dropdownProps, children: children })] }) }));
};

const Divider = forwardRef((props, ref) => jsx(DropdownMenuSeparator, { ref: ref, ...props }));
Divider.displayName = "Dropdown.Divider";

const Label = forwardRef(({ children, className, ...otherProps }, ref) => (jsx(DropdownMenuLabel, { ref: ref, className: className, ...otherProps, children: children })));
Label.displayName = "Dropdown.Label";

const Menu = forwardRef(({ children, className, ...otherProps }, ref) => (jsx(DropdownMenuGroup, { ref: ref, className: cn("flex flex-col", className), ...otherProps, children: children })));
Menu.displayName = "Dropdown.Menu";

const MenuItemButton = forwardRef(({ children, isActive = false, isDisabled = false, style = "default", prefix, suffix, to, href, className, onClick, variant: _variant, disabled: _disabled, asChild: _asChild, onSelect: _onSelect, ...otherProps }, ref) => {
    const { closeOnSelect } = useContext(DropdownContext);
    const variant = MENU_ITEM_STYLE_MAP[style];
    const linkHref = to ?? href;
    const handleSelect = useCallback((e) => {
        if (!closeOnSelect)
            e.preventDefault();
        onClick?.(e);
    }, [closeOnSelect, onClick]);
    const itemContent = useMemo(() => (jsxs(Fragment, { children: [prefix && (jsx("span", { "data-slot": "menu-item-prefix", className: "shrink-0", children: prefix })), jsx("span", { className: "flex-1 truncate", children: children }), suffix && (jsx("span", { "data-slot": "menu-item-suffix", className: "ms-auto shrink-0 text-muted-foreground", children: suffix }))] })), [prefix, children, suffix]);
    return (jsx(DropdownMenuItem, { ref: ref, variant: variant, disabled: isDisabled, "data-active": isActive || undefined, className: cn(isActive && "bg-accent text-accent-foreground", className), asChild: !!linkHref, onSelect: handleSelect, ...otherProps, children: linkHref ? jsx("a", { href: linkHref, children: itemContent }) : itemContent }));
});
MenuItemButton.displayName = "Dropdown.MenuItem.Button";

const MenuItem = forwardRef(({ children, ...otherProps }, ref) => (jsx(DropdownMenuItem, { ref: ref, ...otherProps, children: children })));
MenuItem.displayName = "Dropdown.MenuItem";
MenuItem.Button = MenuItemButton;

const SubMenu = forwardRef(({ label, icon, children, className, triggerProps, contentProps }, ref) => (jsxs(DropdownMenuSub, { children: [jsxs(DropdownMenuSubTrigger, { ref: ref, className: className, ...triggerProps, children: [icon && jsx("span", { className: "shrink-0", children: renderIcon(icon, "size-4") }), label && jsx("span", { children: label })] }), jsx(DropdownMenuSubContent, { ...contentProps, children: children })] })));
SubMenu.displayName = "Dropdown.SubMenu";

const DropdownMenu = DropdownBase;
DropdownMenu.displayName = "DropdownMenu";
DropdownMenu.Menu = Menu;
DropdownMenu.MenuItem = MenuItem;
DropdownMenu.Divider = Divider;
DropdownMenu.Label = Label;
DropdownMenu.SubMenu = SubMenu;

export { DropdownMenu };
//# sourceMappingURL=DropdownMenu.js.map
