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-BJnb9o5c.js';
import { Button as Button$1 } from '../primitives/Button.js';
import { ButtonGroup } from '../primitives/ButtonGroup.js';
import { b as Button } from '../Button-DV9xCqlc.js';
import { C as ChevronDown } from '../chevron-down-BNi0ntys.js';
import { r as renderIcon } from '../renderIcon-C6twJSqH.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-DnD7-4_R.js';
import '../index-Dd1i1d2M.js';
import '../index-d0BBQf5t.js';
import '../index-BNPynZWM.js';
import '../index-CSUSJzOJ.js';
import '../index-Jr4soqx3.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-CrEMzWKw.js';
import '../index-Bo3AAQJp.js';
import '../index-CfriMyrd.js';
import '../separator-ByRsc-y-.js';
import 'react-router-dom';
import '../primitives/Spinner.js';
import '../loader-circle-DycHUAWN.js';
import '../primitives/Tooltip.js';
import '../tooltip-ChQ9xD1j.js';
import '../index-CSggBaQF.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 SplitTrigger = forwardRef(
  ({
    label,
    icon,
    buttonProps,
    disabled,
    isOpen,
    onClick,
    actionButtonRef,
    className,
    ...otherProps
  }, ref) => {
    const { t } = useTranslation();
    const variant = buttonProps?.variant;
    const size = buttonProps?.size;
    return /* @__PURE__ */ jsxs(
      ButtonGroup,
      {
        ref,
        className: cn("neeto-ui-action-dropdown", className),
        ...otherProps,
        children: [
          /* @__PURE__ */ jsx(
            Button,
            {
              ref: actionButtonRef,
              type: "button",
              label,
              icon,
              iconPosition: "left",
              disabled,
              ...buttonProps,
              onClick,
              "data-testid": "action-dropdown-btn"
            }
          ),
          /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, disabled, children: /* @__PURE__ */ jsx(
            Button$1,
            {
              type: "button",
              variant,
              size,
              disabled,
              "aria-label": t(
                "neetoatoms.dropdownMenu.openMenu",
                "Open dropdown menu"
              ),
              className: "px-1.5",
              children: /* @__PURE__ */ jsx(
                ChevronDown,
                {
                  "aria-hidden": "true",
                  className: cn(
                    "shrink-0 transition-transform duration-200",
                    isOpen && "rotate-180"
                  )
                }
              )
            }
          ) })
        ]
      }
    );
  }
);
SplitTrigger.displayName = "Dropdown.SplitTrigger";

const TriggerButton = forwardRef(
  ({
    label,
    icon,
    buttonProps,
    disabled,
    isOpen,
    onClick,
    className,
    ...otherProps
  }, ref) => /* @__PURE__ */ jsx(
    Button,
    {
      ref,
      type: "button",
      label,
      icon,
      iconPosition: "left",
      trailing: /* @__PURE__ */ jsx(
        ChevronDown,
        {
          "aria-hidden": "true",
          className: cn(
            "shrink-0 transition-transform duration-200",
            isOpen && "rotate-180"
          )
        }
      ),
      disabled,
      ...otherProps,
      ...buttonProps,
      onClick,
      className: cn(className, buttonProps?.className)
    }
  )
);
TriggerButton.displayName = "Dropdown.TriggerButton";

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

const DropdownBase = ({
  label,
  icon,
  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 !== void 0;
  const open = isControlled ? isOpenProp : internalOpen;
  const handleOpenChange = useCallback(
    (isOpen) => {
      if (!isControlled) setInternalOpen(isOpen);
      if (!isOpen) onClose?.();
    },
    [isControlled, onClose]
  );
  const side = SIDE_MAP[position] ?? "bottom";
  const align = ALIGN_MAP[position] ?? "end";
  const isSplit = !!onClick && !customTarget;
  const sharedTriggerProps = {
    label,
    icon,
    buttonProps,
    disabled,
    isOpen: open,
    onClick
  };
  const triggerNode = isSplit ? /* @__PURE__ */ jsx(
    SplitTrigger,
    {
      ...sharedTriggerProps,
      actionButtonRef: triggerRef,
      className
    }
  ) : /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, disabled, className, children: customTarget ? typeof customTarget === "function" ? customTarget({ isOpen: open }) : customTarget : /* @__PURE__ */ jsx(TriggerButton, { ref: triggerRef, ...sharedTriggerProps }) });
  return /* @__PURE__ */ jsx(DropdownContext.Provider, { value: { closeOnSelect }, children: /* @__PURE__ */ jsxs(
    DropdownMenu$1,
    {
      open,
      onOpenChange: handleOpenChange,
      modal: closeOnOutsideClick,
      children: [
        triggerNode,
        /* @__PURE__ */ jsx(
          DropdownMenuContent,
          {
            side,
            align,
            onEscapeKeyDown: closeOnEsc ? void 0 : (e) => e.preventDefault(),
            onInteractOutside: closeOnOutsideClick ? void 0 : (e) => e.preventDefault(),
            ...dropdownProps,
            children
          }
        )
      ]
    }
  ) });
};

const Divider = forwardRef((props, ref) => /* @__PURE__ */ jsx(DropdownMenuSeparator, { ref, ...props }));
Divider.displayName = "Dropdown.Divider";

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

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

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

const SubMenu = forwardRef(
  ({ label, icon, children, className, triggerProps, contentProps }, ref) => /* @__PURE__ */ jsxs(DropdownMenuSub, { children: [
    /* @__PURE__ */ jsxs(DropdownMenuSubTrigger, { ref, className, ...triggerProps, children: [
      icon && /* @__PURE__ */ jsx("span", { className: "shrink-0", children: renderIcon(icon, "size-4") }),
      label && /* @__PURE__ */ jsx("span", { children: label })
    ] }),
    /* @__PURE__ */ jsx(DropdownMenuSubContent, { ...contentProps, 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
