'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var primitives_DropdownMenu = require('../primitives/DropdownMenu.js');
var reactI18next = require('react-i18next');
var utils = require('../utils-BhM0B89p.js');
var primitives_Button = require('../primitives/Button.js');
var primitives_ButtonGroup = require('../primitives/ButtonGroup.js');
var Button = require('../Button-8VH9p9RB.js');
var chevronDown = require('../chevron-down-CPsWSS51.js');
var renderIcon = require('../renderIcon-BRrpZu9a.js');
require('../chevron-right-0jNdwX2Q.js');
require('../createLucideIcon-D0tRgV6l.js');
require('../check-BQgcDXys.js');
require('../index-COPkC3I5.js');
require('../index-D-iDn9RI.js');
require('../index-CGUGhyIp.js');
require('../index-CyrAgb4H.js');
require('../index-CCdG4z4E.js');
require('../index-DMbj7vXd.js');
require('react-dom');
require('../index-DbA93GP3.js');
require('../index-Ch55j1or.js');
require('../index-BY6wgiQV.js');
require('../index-Bvu9MiFi.js');
require('../index-lWVw05cs.js');
require('../index-CS_WSHYi.js');
require('../index-BwAq9ba8.js');
require('../index-C9ICrOhM.js');
require('../floating-ui.react-dom-B4Aw6O7R.js');
require('../index-Bf0WsHta.js');
require('../index-Df-Ffa3s.js');
require('../index-DcCSSgb7.js');
require('../index-DoVhdseB.js');
require('../button-CMl9rLXi.js');
require('../index-vioSzJOw.js');
require('../index-DuNgWCXZ.js');
require('../separator-DSL-aG1J.js');
require('react-router-dom');
require('../primitives/Spinner.js');
require('../loader-circle-Bw7zP2Gn.js');
require('../primitives/Tooltip.js');
require('../tooltip-BYCcUMZn.js');
require('../index-CB9xFokC.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 = React.forwardRef(
  ({
    label,
    icon,
    buttonProps,
    disabled,
    isOpen,
    onClick,
    actionButtonRef,
    className,
    ...otherProps
  }, ref) => {
    const { t } = reactI18next.useTranslation();
    const variant = buttonProps?.variant;
    const size = buttonProps?.size;
    return /* @__PURE__ */ jsxRuntime.jsxs(
      primitives_ButtonGroup.ButtonGroup,
      {
        ref,
        className: utils.cn("neeto-ui-action-dropdown", className),
        ...otherProps,
        children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            Button.Button,
            {
              ref: actionButtonRef,
              type: "button",
              label,
              icon,
              iconPosition: "left",
              disabled,
              ...buttonProps,
              onClick,
              "data-testid": "action-dropdown-btn"
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsx(primitives_DropdownMenu.DropdownMenuTrigger, { asChild: true, disabled, children: /* @__PURE__ */ jsxRuntime.jsx(
            primitives_Button.Button,
            {
              type: "button",
              variant,
              size,
              disabled,
              "aria-label": t(
                "neetoatoms.dropdownMenu.openMenu",
                "Open dropdown menu"
              ),
              className: "px-1.5",
              children: /* @__PURE__ */ jsxRuntime.jsx(
                chevronDown.ChevronDown,
                {
                  "aria-hidden": "true",
                  className: utils.cn(
                    "shrink-0 transition-transform duration-200",
                    isOpen && "rotate-180"
                  )
                }
              )
            }
          ) })
        ]
      }
    );
  }
);
SplitTrigger.displayName = "Dropdown.SplitTrigger";

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

const DropdownContext = React.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] = React.useState(false);
  const isControlled = isOpenProp !== void 0;
  const open = isControlled ? isOpenProp : internalOpen;
  const handleOpenChange = React.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__ */ jsxRuntime.jsx(
    SplitTrigger,
    {
      ...sharedTriggerProps,
      actionButtonRef: triggerRef,
      className
    }
  ) : /* @__PURE__ */ jsxRuntime.jsx(primitives_DropdownMenu.DropdownMenuTrigger, { asChild: true, disabled, className, children: customTarget ? typeof customTarget === "function" ? customTarget({ isOpen: open }) : customTarget : /* @__PURE__ */ jsxRuntime.jsx(TriggerButton, { ref: triggerRef, ...sharedTriggerProps }) });
  return /* @__PURE__ */ jsxRuntime.jsx(DropdownContext.Provider, { value: { closeOnSelect }, children: /* @__PURE__ */ jsxRuntime.jsxs(
    primitives_DropdownMenu.DropdownMenu,
    {
      open,
      onOpenChange: handleOpenChange,
      modal: closeOnOutsideClick,
      children: [
        triggerNode,
        /* @__PURE__ */ jsxRuntime.jsx(
          primitives_DropdownMenu.DropdownMenuContent,
          {
            side,
            align,
            onEscapeKeyDown: closeOnEsc ? void 0 : (e) => e.preventDefault(),
            onInteractOutside: closeOnOutsideClick ? void 0 : (e) => e.preventDefault(),
            ...dropdownProps,
            children
          }
        )
      ]
    }
  ) });
};

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

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

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

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

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

exports.DropdownMenu = DropdownMenu;
//# sourceMappingURL=DropdownMenu.js.map
