import { jsx as i, jsxs as E } from "react/jsx-runtime";
import L, { memo as M, useState as B, useRef as H, useLayoutEffect as V, useCallback as x, useEffect as F } from "react";
import { negate as U } from "es-toolkit/function";
import { delay as j, head as X } from "es-toolkit/compat";
import Y from "../../utils/classNames.js";
import q from "./ActionBarOverlay.js";
import R from "./ActionBarItemPopoverContent.js";
import _ from "./ActionBarItemIcon.js";
import z from "./ActionBarItemList.js";
import G from "./ActionBarItemListItem.js";
import J from "./ActionBarItemListSeparator.js";
import g from "../overlay/OverlayTrigger.js";
import K from "../dialog/Dialog.js";
const y = "mousedown", Q = "show", Z = "position-offscreen", $ = 250, oo = 200, eo = 580, to = () => (Math.random() + 1).toString(36).toUpperCase().substring(2), T = (o, e) => o.classList.contains(e), so = U(T), no = (o, e) => !e.contains(o), ro = () => {
  const [o, e] = B(!1);
  return F(() => {
    const a = X(document.getElementsByClassName("ApplicationHeader")), f = a ? a.offsetWidth < eo : !1;
    e(f);
  }, []), o;
}, O = (o) => o.type && o.type.displayName === R.displayName, P = (o) => o.type && o.type.displayName === _.displayName, io = (o) => {
  const {
    id: e = to(),
    className: a,
    mobileDialogBodyClassName: f = "",
    children: u,
    popoverWidth: k = $,
    hidePopoverOnClick: w = !0,
    ...h
  } = o, [t, s] = B(!1), n = H(null);
  V(() => {
    const r = (A) => {
      if (!n || !n.current || !t)
        return;
      const m = document.getElementById(e);
      if (!m || A.target.offsetParent === n.current)
        return;
      const W = no(A.target, m);
      if (T(m, Q) && so(m, Z) && W) {
        s(!1);
        return;
      }
      w && j(() => s(!1), oo);
    };
    return document.addEventListener(y, r), () => {
      document.removeEventListener(y, r);
    };
  }, [n, e, t]);
  const p = Y("ActionBarItem", a), b = ro(), I = x(() => s(!t), [s, t]), d = L.Children.toArray(u), l = d.find(O), C = d.find(P);
  if (!l)
    return /* @__PURE__ */ i("div", { className: p, children: u });
  const { useOffscreen: S = !1, title: N } = l.props, v = d.filter((r) => !O(r)).filter((r) => !P(r));
  if (b)
    return /* @__PURE__ */ E("div", { ...h, className: p, ref: n, children: [
      L.cloneElement(C, { onClick: I }),
      /* @__PURE__ */ i("div", { onClick: () => s(!1), children: /* @__PURE__ */ i(
        K,
        {
          show: t,
          onClose: () => s(!1),
          body: l,
          bodyClassName: f,
          title: N
        }
      ) }),
      v
    ] });
  const D = /* @__PURE__ */ i(
    q,
    {
      id: e,
      title: N,
      width: k,
      preRender: S,
      show: t,
      children: l
    },
    "child"
  );
  return /* @__PURE__ */ E("div", { ...h, className: p, ref: n, children: [
    /* @__PURE__ */ i(
      g,
      {
        onToggle: I,
        show: t || S,
        placement: g.BOTTOM_END,
        overlay: D,
        rootClose: !1,
        trigger: "click",
        popperConfig: {
          modifiers: [
            {
              name: "offset",
              options: {
                offset: [0, 5]
              }
            },
            {
              name: "arrow",
              options: {}
            }
          ]
        },
        children: C
      }
    ),
    v
  ] });
}, c = M(io);
c.Icon = _;
c.Popover = R;
c.List = z;
c.ListItem = G;
c.ListSeparator = J;
export {
  c as default
};
//# sourceMappingURL=ActionBarItem.js.map
