import { jsx as e, jsxs as A } from "@emotion/react/jsx-runtime";
import w, { useState as X, useMemo as x, useEffect as Z, useCallback as y } from "react";
import { useModuleDictionary as ee } from "@m4l/core";
import { MENU_ACTIONS_ as oe, MENU_ACTIONS_CLASSES as E } from "./constants.js";
import { getMenuActionsDictionary as P, DICTIONARY as _ } from "./dictionary.js";
import { MenuDivider as M } from "../extended/mui/MenuDivider/MenuDivider.js";
import te from "clsx";
import { MenuMoreVerticalOutlined as re } from "@m4l/icons";
import { MenuListStyled as ne, MenuLoaderStyled as le, RootStyled as ie, IconButtonStyled as se, HeaderMenuActionsStyled as ae, FooterMenuActionsStyled as ce } from "./slots/MenuActionsSlots.js";
import { useComponentSize as de } from "@m4l/graphics";
import { MenuItem as O } from "../extended/mui/MenuItem/MenuItem.js";
import { CircularProgress as ue } from "../extended/mui/CircularProgress/CircularProgress.js";
import { Popover as me } from "../extended/mui/Popover/Popover.js";
function pe(T) {
  const {
    menuActions: a,
    endListElement: u,
    menuActionSx: fe,
    iconButtonClassName: z = "",
    objItem: h = {},
    urlIcon: m,
    icon: i,
    toolTip: L = "",
    badgeContent: R,
    arrowType: B = "right-top",
    externalOpen: D = null,
    externalClose: Ce,
    size: r = "medium",
    disabled: $ = !1,
    paperProps: j = {},
    color: b = "primary",
    actionKey: g,
    header: c,
    footer: d,
    className: F,
    onOpen: o,
    slots: H = {},
    variant: U = "text",
    ...v
  } = T, { currentSize: I } = de(r), { getLabel: p } = ee(), [N, f] = X(null), n = !!(D ?? N), C = x(
    () => ({
      iconSize: I,
      selected: n,
      paletteColor: b
    }),
    [I, n, b]
  );
  Z(() => {
    o && o(n);
  }, [n, o]);
  const V = () => {
    f(null), o && o(!1);
  }, K = x(() => i ? typeof i == "string" || w.isValidElement(i) ? i : void 0 : m || /* @__PURE__ */ e(re, {}), [m, i]), k = y(
    (t, s) => {
      t.stopPropagation(), f(null), o && o(!1), s.onClick && s.onClick(t);
    },
    [o]
  ), Y = y(
    (t) => {
      t.stopPropagation(), f(t.currentTarget), o && o(!0);
    },
    [o]
  ), q = y(() => {
    let t = [], s = [];
    if (a && (typeof a == "function" ? t = a(h) : t = a), Array.isArray(t) && (s = t), s.length === 0)
      return /* @__PURE__ */ e(
        O,
        {
          disabled: !0,
          role: "menu-no-actions",
          label: p(
            P(_.no_actions_label)
          )
        }
      );
    const G = () => /* @__PURE__ */ e(ae, { children: c }), J = () => /* @__PURE__ */ e(ce, { children: d });
    return /* @__PURE__ */ A(ne, { ownerState: {}, children: [
      c && /* @__PURE__ */ e(G, {}),
      c && /* @__PURE__ */ e(M, { variant: "solid", size: r }),
      s.map((l, Q) => {
        const S = g ?? Q;
        switch (l.type ?? "menuItem") {
          case "divider":
            return /* @__PURE__ */ e(
              M,
              {
                variant: "solid",
                size: r
              },
              `divider-${S}`
            );
          case "loader":
            return /* @__PURE__ */ e(le, { size: r, children: /* @__PURE__ */ e(ue, { size: r }) }, `loader-${S}`);
          case "customNode":
            return l.customNode;
          case "menuItem":
            return /* @__PURE__ */ e(
              O,
              {
                size: r,
                ...l,
                label: l.label ?? p(
                  P(_.no_actions_label)
                ),
                onClick: l.onClick ? (W) => k(W, l) : void 0
              },
              `${oe}${l.label ?? S}`
            );
          default:
            return null;
        }
      }),
      d && /* @__PURE__ */ e(M, { variant: "solid", size: r }),
      d && /* @__PURE__ */ e(J, {}),
      u && u
    ] });
  }, [
    a,
    c,
    r,
    d,
    u,
    h,
    p,
    g,
    k
  ]);
  return /* @__PURE__ */ A(
    ie,
    {
      className: te(
        F,
        E.root,
        C.selected && E.selected
      ),
      ownerState: { ownerState: C },
      children: [
        /* @__PURE__ */ e(
          se,
          {
            ownerState: { ...C },
            tooltip: L,
            size: r,
            icon: K,
            onClick: Y,
            className: z,
            selected: n,
            disabled: $,
            badgeProps: { badgeContent: R },
            "aria-label": v?.["aria-label"] ?? "menu-actions",
            variant: U
          }
        ),
        n && /* @__PURE__ */ e(
          me,
          {
            id: "Popover",
            open: n,
            anchorEl: N,
            onClose: V,
            arrowType: B,
            ...v,
            slots: { ...H },
            slotProps: { paper: { ...j } },
            children: q()
          }
        )
      ]
    }
  );
}
const Pe = w.memo(pe);
export {
  Pe as MemonizedMenuActions,
  pe as MenuActions,
  pe as default
};
