import { jsx as p, jsxs as h } from "react/jsx-runtime";
import { forwardRef as ao, useState as c, useEffect as io } from "react";
import po from "react-dom";
import { usePopper as co } from "react-popper";
import { noop as d } from "es-toolkit/function";
import { isNil as fo } from "es-toolkit/predicate";
import P from "../../utils/classNames.js";
import { getOrCreatePortalRoot as mo } from "../../utils/portalRoot.js";
import uo, { DEFAULT_EVENT_TYPES as go } from "../../hooks/useClickOutside.js";
import wo from "../menuItems/MenuItems.js";
import Co from "../menuItems/MenuItemList.js";
import bo from "./DropdownToggleButton.js";
import Bo from "./SplitCaretButton.js";
import Do from "./Caret.js";
const No = (r, s) => r && s ? "top-end" : !r && s ? "top-start" : r && !s ? "bottom-end" : "bottom-start", Oo = () => ao((r, s) => {
  const {
    id: g = Math.random().toString(36).slice(2, 16),
    "aria-label": R,
    items: T = [],
    bsSize: w = "md",
    bsStyle: C = "default",
    variant: k,
    disabled: b = !1,
    iconName: y,
    iconOnly: B = !1,
    title: E,
    splitButton: t = !1,
    customDropdown: D,
    open: e,
    dropup: L = !1,
    pullRight: f = !1,
    noCaret: x = !1,
    onOpen: I = d,
    onClose: j = d,
    onLabelButtonClick: A = d,
    usePortal: l = !1,
    popperConfig: U,
    toggleButtonType: _ = "button",
    toggleClassName: N = "",
    trackingAttributes: v,
    dropdownClassName: z,
    className: F = "",
    ...V
  } = r, [O, m] = c(e), [Y, q] = c(null), [S, G] = c(null), [H, J] = c(null), K = {
    placement: No(f, L),
    modifiers: []
  }, Q = t && f ? H : Y, { styles: W, attributes: X } = co(Q, S, U || K), n = fo(e), a = n ? O : e, Z = uo(
    (o) => {
      l && S?.contains(o.target) || i();
    },
    go,
    !!a
    // only listen to clicks outside when the dropdown is open
  ), $ = mo(), oo = !x && !t && !B;
  io(() => {
    n || m(e);
  }, [n, e]);
  const u = (o) => {
    (n ? O : e) ? i() : to(o);
  }, to = (o) => {
    n && m(!0), o && I(o);
  }, i = () => {
    n && m(!1), j();
  }, eo = () => {
    i(), A();
  }, no = (o) => {
    u(o);
  }, ro = (o) => {
    t ? eo() : u(o);
  }, so = P("dropdown", "btn-group", a && "open", F), lo = P(
    l && "dropdown-portal",
    t && f && "pull-right",
    z
  ), M = /* @__PURE__ */ p(
    Co,
    {
      className: lo,
      ref: G,
      style: W.popper,
      ...X.popper,
      children: D || /* @__PURE__ */ p(wo, { items: T, closeMenu: u })
    }
  );
  return /* @__PURE__ */ h("div", { ...V, className: so, ref: Z, children: [
    /* @__PURE__ */ h(
      bo,
      {
        id: g,
        "aria-label": R,
        splitButton: t,
        bsStyle: C,
        bsSize: w,
        variant: k,
        iconOnly: B,
        iconName: y,
        disabled: b,
        ref: q,
        onClick: ro,
        outerRef: s,
        toggleButtonType: _,
        className: N,
        trackingAttributes: v,
        children: [
          E,
          oo && /* @__PURE__ */ p(Do, {})
        ]
      }
    ),
    t && /* @__PURE__ */ p(
      Bo,
      {
        id: g,
        bsStyle: C,
        bsSize: w,
        disabled: b,
        className: N,
        onClick: no,
        ref: J
      }
    ),
    a && l && po.createPortal(M, $),
    a && !l && M
  ] });
}), _o = Oo();
export {
  _o as default
};
//# sourceMappingURL=ButtonDropdown.js.map
