import { jsx as Z } from "@emotion/react/jsx-runtime";
import { useCallback as $, useMemo as o } from "react";
import { useEnvironment as P, useModuleDictionary as Q } from "@m4l/core";
import { ActionsFilterOutlined as W } from "@m4l/icons";
import { DICTIONARY as p } from "../../../../dictionary.js";
import { pathIcons as b } from "../../../../icons.js";
import { Icon as X } from "@m4l/base";
import { useDataGrid as y } from "../../../../hooks/shared/useDataGrid/useDataGrid.js";
function at(t, A, U, _) {
  const {
    columnsConfig: i,
    onChangeColumnsConfig: g,
    setSortColumns: N,
    sortColumns: I,
    externalSortSettings: s,
    externalFilterSettings: u
  } = y(), { host_static_assets: d, environment_assets: l } = P(), { getLabel: a } = Q(), m = $(
    (e) => {
      _ ? _(e) : N(e);
    },
    [_, N]
  ), n = o(() => A.find((e) => e.key === t), [t, A]), k = o(() => t ? typeof n?.sortable == "boolean" ? n.sortable : U : !1, [t, n, U]), S = o(() => !t || !s ? !1 : s.sortsColumns.some(
    (e) => typeof e == "string" ? e === t : e.name === t
  ), [t, s]), c = o(() => {
    if (!(!t || !s?.sortsApplied))
      return s.sortsApplied.find(
        (e) => e.columnKey === t
      );
  }, [t, s?.sortsApplied]), v = o(() => c?.direction === "asc", [c]), F = o(() => c?.direction === "desc", [c]), D = o(
    () => !!(t && I.some(
      (e) => e.columnKey === t && e.direction === "ASC"
    )),
    [t, I]
  ), M = o(
    () => !!(t && I.some(
      (e) => e.columnKey === t && e.direction === "DESC"
    )),
    [t, I]
  ), G = $(() => {
    if (!t)
      return;
    const e = I.filter((r) => r.columnKey !== t);
    m(e);
  }, [t, I, m]), T = o(() => !!c, [c]), z = $(() => {
    if (!t)
      return;
    const e = i.map(
      (r) => r.key === t ? { ...r, frozen: !r.frozen } : r
    );
    g("table", e);
  }, [t, i, g]), B = $(() => {
    if (!t)
      return;
    const e = i.map(
      (r) => r.key === t ? { ...r, visible: !r.visible } : r
    );
    g("table", e);
  }, [t, i, g]), R = $(
    (e) => {
      !t || !n || n?.type === "custom" || s?.onChange({
        action: "add",
        columnKey: t,
        direction: e,
        label: n.name
      });
    },
    [t, n, s]
  ), x = o(() => c?.removable, [c]), V = $(() => {
    if (!t || !n)
      return null;
    u?.onChange({
      action: "add",
      columnKey: t
    });
  }, [t, n, u]), h = o(() => !t || !n ? !1 : n.groupable === !0, [t, n]), L = o(() => t ? i.find((r) => r.key === t)?.grouped === !0 : !1, [t, i]), H = o(() => A.some(
    (e) => e.groupable === !0 || e.grouped === !0
  ), [A]), Y = o(() => {
    if (!t || !n || n.type === "custom" || !u?.filterColumns.some(
      (C) => C.name === t
    ))
      return !0;
    const r = u?.filtersApplied.some(
      (C) => C.columnKey === t && C.isValid
    ), O = u?.filterColumns.find(
      (C) => C.name === t
    )?.multiple;
    return r && !O;
  }, [t, n, u]);
  return o(() => {
    if (!t)
      return [];
    const e = [
      {
        type: "menuItem",
        startIcon: `${d}/${l}/${b.sortAsc}`,
        label: a(p.ORDER_ASC_COLUMN),
        dataTestId: "sort-ascending",
        /**
         * Función que agrega un sort externo en modo ascendente
         */
        onClick: () => {
          s ? R("asc") : m([
            {
              columnKey: t,
              direction: "ASC"
            }
          ]);
        },
        disabled: !k || (s ? !S || v : D)
      },
      {
        type: "menuItem",
        startIcon: `${d}/${l}/${b.sortDesc}`,
        label: a(p.ORDER_DESC_COLUMN),
        dataTestId: "sort-descending",
        /**
         * Función que agrega un sort externo en modo descendente
         */
        onClick: () => {
          s ? R("desc") : m([
            {
              columnKey: t,
              direction: "DESC"
            }
          ]);
        },
        disabled: !k || (s ? !S || F : M)
      },
      {
        type: "menuItem",
        startIcon: `${d}/${l}/${b.removeSort}`,
        label: a(p.REMOVE_ORDER_COLUMN),
        dataTestId: "sort-remove",
        /**
         * Función que elimina el sort externo
         */
        onClick: () => {
          if (t)
            if (s && x) {
              s.onChange({
                action: "remove",
                columnKey: t
              });
              return;
            } else
              G();
        },
        disabled: s ? !T || !x : !(D || M)
      }
    ], r = u ? [{ type: "menuItem", startIcon: /* @__PURE__ */ Z(X, { icon: /* @__PURE__ */ Z(W, {}) }), label: a(p.ADD_FILTER_COLUMN), dataTestId: "filter-add", onClick: () => {
      V();
    }, disabled: Y }] : [], O = (f) => {
      if (!t)
        return;
      const w = i.filter((E) => E.grouped), q = f ? w.length : 0, J = i.map(
        (E) => E.key === t ? { ...E, grouped: f, groupOrder: f ? q : E.groupOrder } : E
      );
      g("table", J);
    }, C = H ? [{ type: "menuItem", startIcon: `${d}/${l}/${b.group}`, label: a(p.GROUP_BY_COLUMN), dataTestId: "group-add", onClick: () => {
      O(!0);
    }, disabled: !h || L }, {
      type: "menuItem",
      startIcon: `${d}/${l}/${b.removeGroup}`,
      label: a(p.REMOVE_GROUP_COLUMN),
      dataTestId: "group-remove",
      onClick: () => {
        O(!1);
      },
      /** Solo deshabilitado si no está agrupada O si es una agrupación estática (groupable=false)*/
      disabled: !L || !h
    }] : [], j = [
      {
        type: "menuItem",
        startIcon: i.find((f) => f.key === t)?.frozen ? `${d}/${l}/${b.unfreezeColumn}` : `${d}/${l}/${b.freezeColumn}`,
        label: i.find((f) => f.key === t)?.frozen ? a(p.UNFREEZE_COLUMN) : a(p.FREEZE_COLUMN),
        onClick: z,
        disabled: !i.find((f) => f.key === t)?.visible,
        dataTestId: "toggle-freeze"
      },
      {
        type: "menuItem",
        startIcon: `${d}/${l}/${b.hideColumn}`,
        label: a(p.HIDE_COLUMN),
        onClick: B,
        dataTestId: "hide-column"
      }
    ];
    return [...e, ...r, ...C, ...j];
  }, [
    t,
    k,
    S,
    v,
    F,
    T,
    R,
    z,
    B,
    i,
    g,
    s,
    u,
    H,
    h,
    L,
    d,
    l,
    a,
    Y,
    m,
    V,
    G,
    D,
    M,
    x
  ]);
}
export {
  at as useHeaderMenuActions
};
