import { jsx as n } from "@emotion/react/jsx-runtime";
import { useState as M, useMemo as w, useEffect as I, useCallback as p } from "react";
import { useModuleDictionary as x } from "@m4l/core";
import { MenuListStyled as P, HeaderRenderClickStyled as B } from "../../../../slots/DataGridSlot.js";
import { MenuDivider as F } from "../../../../../extended/mui/MenuDivider/MenuDivider.js";
import { DICTIONARY as b } from "../../../../dictionary.js";
import { DATAGRID_HEADER_RENDER_CLICK_KEY as z } from "../../../../constants.js";
import { useFilters as H } from "../../../../hooks/shared/useFilters/useFilters.js";
import { MenuItem as y } from "../../../../../extended/mui/MenuItem/MenuItem.js";
import { Popover as K } from "../../../../../extended/mui/Popover/Popover.js";
function X(v) {
  const {
    menuActions: s,
    size: i = "medium",
    externalOpen: a,
    onOpen: o,
    objItem: f = {},
    actionKey: m,
    className: k,
    arrowType: O = "right-top",
    paperProps: u = {},
    slots: N = {},
    ...S
  } = v, { getLabel: c } = x(), { activeFilters: _ } = H(), [h, d] = M(null), D = a ?? h, r = typeof a < "u" ? !!a : !!h, L = w(
    () => ({
      selected: r
    }),
    [r]
  );
  I(() => {
    r || d(null);
  }, [r]), I(() => {
    o && o(r);
  }, [r, o]);
  const R = p(() => {
    d(null), o && o(!1);
  }, [o]), C = p(
    (t, l) => {
      t.stopPropagation(), d(null), o && o(!1), l.onClick && l.onClick(t);
    },
    [o]
  ), T = p(() => {
    let t = [], l = [];
    return s && (typeof s == "function" ? t = s(f) : t = s), Array.isArray(t) && (l = t), l.length === 0 ? /* @__PURE__ */ n(
      y,
      {
        disabled: !0,
        role: "menu-no-actions",
        label: c(b.NO_ACTIONS_LABEL)
      }
    ) : /* @__PURE__ */ n(P, { ownerState: {}, children: l.map((e, g) => {
      const E = m ?? g;
      switch (e.type ?? "menuItem") {
        case "divider":
          return /* @__PURE__ */ n(
            F,
            {
              variant: "solid",
              size: i
            },
            `divider-${E}`
          );
        case "customNode":
          return e.customNode;
        case "menuItem":
          return /* @__PURE__ */ n(
            y,
            {
              size: i,
              ...e,
              label: e.label ?? c(b.NO_ACTIONS_LABEL),
              onClick: e.onClick ? (A) => C(A, e) : void 0,
              "aria-label": e.type === "menuItem" && e.dataTestId ? e.dataTestId : "header-render-click-menu-item"
            },
            `${z}${e.label ?? E}`
          );
        default:
          return null;
      }
    }) });
  }, [s, i, f, c, m, C]);
  return /* @__PURE__ */ n(B, { className: k, ownerState: { ownerState: L }, children: r && /* @__PURE__ */ n(
    K,
    {
      id: "Popover",
      open: r,
      anchorEl: D,
      onClose: R,
      arrowType: O,
      ...S,
      slots: { ...N },
      slotProps: {
        paper: {
          ...u,
          sx: {
            mt: _ ? 5.6 : 0.3,
            ...u?.sx
          }
        }
      },
      disableAutoFocus: !0,
      disableEnforceFocus: !0,
      anchorOrigin: { vertical: "bottom", horizontal: "left" },
      transformOrigin: { vertical: "top", horizontal: "left" },
      children: T()
    }
  ) });
}
export {
  X as HeaderRenderClick
};
