import { jsx as H } from "@emotion/react/jsx-runtime";
import { useState as N, useEffect as O, useMemo as I } from "react";
import { useModuleSkeleton as K, getPropertyByString as M } from "@m4l/core";
import { SELECT_COLUMN_KEY as _ } from "react-data-grid";
import { getDragHeaderRenderer as $ } from "../../helpers/getDragHeaderRenderer/getDragHeaderRenderer.js";
import { SkeletonFormatter as G } from "../../subcomponents/SkeletonFormatter/SkeletonFormatter.js";
import { filterColumnClassName as B, SelectColumn as P } from "../../subcomponents/SelectColumn/SelectColumn.js";
import { RenderGroupCell as J } from "../../subcomponents/RenderGroupCell/RenderGroupCell.js";
import { ActionsColumn as L } from "../../subcomponents/ActionsColumn/ActionsColumn.js";
import { RadioSelectColumn as T } from "../../subcomponents/RadioSelectColumn/RadioSelectColumn.js";
import { useDataGrid as j } from "../../../../hooks/shared/useDataGrid/useDataGrid.js";
import { useFilters as U } from "../../../../hooks/shared/useFilters/useFilters.js";
function V(n, l) {
  const s = n.find((d) => d.key === l);
  let r = "string";
  return s && (r = s.type || "string"), s?.customSort ? s.customSort : r === "number" ? (d, m) => {
    try {
      return Number(d[l]) - Number(m[l]);
    } catch {
      return -1;
    }
  } : (d, m) => {
    try {
      return d[l].toString().localeCompare(m[l].toString());
    } catch {
      return -1;
    }
  };
}
const b = (n, l) => l.findIndex((s) => s.key === n), Y = (n, l) => l.findIndex(
  (r) => n === r.key && r.hidden !== !0
) !== -1, q = (n, l, s, r, d, m) => {
  let a = n.filter((t) => {
    const i = b(t.key, r), f = i > -1 ? r[i].visible : !0;
    return !t.hidden && f;
  }).sort((t, i) => {
    let f = b(t.key, r);
    f = f === -1 ? r.length : f;
    let u = b(i.key, r);
    return u = u === -1 ? r.length : u, f - u;
  }).map((t) => {
    const i = b(t.key, r);
    return {
      ...t,
      frozen: i > -1 ? r[i].frozen : !1,
      width: typeof d.get(t.key) == "number" ? d.get(t.key) : t.width
    };
  }).sort((t, i) => (t.frozen === void 0 || t.frozen === !1) == (i.frozen === void 0 || i.frozen === !1) ? 0 : t.frozen ? -1 : 1);
  return m && (a = [L, ...a]), l && (a = [s === !1 ? T : P, ...a]), a;
}, Q = (n) => {
  switch (n) {
    case "string":
      return "left";
    case "date":
    case "boolean":
    case "custom":
      return "center";
    case "number":
      return "right";
  }
  return "left";
}, ae = (n, l, s) => {
  const {
    columnsConfig: r,
    columnsWidths: d,
    onChangeColumnsOrder: m,
    rowActionsGetter: a,
    checkedRows: t,
    onCheckedRowsChange: i,
    checkedRowsMultiple: f,
    sortColumns: u,
    setSortColumns: R
  } = j(), [S, E] = N(
    () => []
  ), z = K(), { activeFilters: F, filters: k } = U();
  O(() => {
    E(
      q(
        n,
        !!t && !!i,
        //Si están seteados se habilita el múltipe
        f,
        r,
        d,
        a
      )
    );
    const C = u.filter(
      (o) => Y(o.columnKey, n)
    );
    JSON.stringify(C) !== JSON.stringify(u) && R(C);
  }, [
    n,
    r,
    a,
    t,
    i,
    f,
    d,
    u,
    R
  ]);
  const A = I(() => {
    const C = $(
      (o, p) => m("table", o, p),
      {
        openPopover: s?.openPopover
      }
    );
    return S.map((o) => {
      const p = o.align !== void 0 ? `rdg-cell-align-${o.align}` : `rdg-cell-align-${Q(o.type)}`, e = {
        ...o,
        cellClass: p,
        headerCellClass: p
      };
      if (e.key === _ || e.isDraggable !== void 0 && e.isDraggable === !1)
        return e;
      z ? (e.renderHeaderCell = G, e.renderCell = G, e.renderEditCell = G) : (e.formatter && !e.renderCell && (e.renderCell = e.formatter), e.editor && !e.renderEditCell && (e.renderEditCell = e.editor)), e.renderHeaderCell && (e.withinHeaderRenderer = e.renderHeaderCell), (e?.withFilter === void 0 || e.withFilter === !0) && F && (e.headerCellClass = `${e.headerCellClass} ${B}`.trim()), e.renderHeaderCell = C;
      const g = r.find((y) => y.key === o.key)?.grouped === !0, x = n.find((y) => y.key === o.key), h = x?.renderGroupCell !== void 0;
      if (g) {
        const y = h ? x?.renderGroupCell : void 0;
        e.renderGroupCell = (c) => /* @__PURE__ */ H(
          J,
          {
            ...c,
            customRenderGroupCell: y
          }
        );
      }
      return e;
    });
  }, [
    z,
    S,
    F,
    m,
    s,
    r,
    n
  ]), D = I(() => {
    const C = l.filter((o) => {
      if (!k || k.size === 0)
        return !0;
      const p = k.entries();
      let e = !0;
      for (let w = 0; w < k.size; w++) {
        const g = p.next().value, x = g?.[0] ?? "", h = g?.[1] ?? {
          value: "",
          customFilter: void 0
        };
        let y;
        const c = M(o, x);
        let v;
        typeof c == "string" ? v = c : typeof c == "number" ? v = c.toString() : c != null ? v = String(c) : v = "";
        try {
          h.customFilter ? e = e && h.customFilter(o, h.value) : (y = v, e = e && y.includes(h.value));
        } catch {
          continue;
        }
        if (!e)
          break;
      }
      return e;
    });
    return u.length === 0 || C.sort((o, p) => {
      for (const e of u) {
        const g = V(S, e.columnKey)(o, p);
        if (g !== 0)
          return e.direction === "ASC" ? g : -g;
      }
      return 0;
    }), C;
  }, [l, u, k, S]);
  return { finalColumns: A, sortColumns: u, setSortColumns: R, finalRows: D };
};
export {
  ae as useSortColumnsRows
};
