import { getPropertyByString as u } from "@m4l/core";
import { ColumnTagsFormatter as m } from "./formatter.js";
import { ColumnTagsGroupFormatter as i } from "./ColumnTagsGroupFormatter.js";
import { deepEqual as f } from "fast-equals";
import { useState as c, useRef as g, useEffect as y, useMemo as A } from "react";
import { getColumnKey as n } from "../../helpers/shared/getColumnKey/getColumnKey.js";
const C = (r) => {
  const { fieldValue: t } = r;
  return (a, e) => {
    const o = u(
      a,
      n(t),
      null
    );
    return o ? (Array.isArray(o) ? o : [o]).some(
      (s) => s.label.toLowerCase().includes(e.toLowerCase())
    ) : !1;
  };
}, b = (r) => (t, a) => {
  const e = u(
    t,
    n(r.fieldValue),
    null
  ), o = u(
    a,
    n(r.fieldValue),
    null
  ), l = e ? Array.isArray(e) ? e[0]?.label : e.label : "", s = o ? Array.isArray(o) ? o[0]?.label : o.label : "";
  return l > s ? 1 : l < s ? -1 : 0;
}, B = (r) => {
  const [t, a] = c(r), e = g({ ...r });
  return y(() => {
    f(e.current, r) || (e.current = r, a(r));
  }, [r]), A(
    () => ({
      formatter: m(t),
      renderGroupCell: i(t),
      customFilter: C(t),
      customSort: b(t)
    }),
    [t]
  );
};
export {
  B as useColumnTags
};
