import { getPropertyByString as i } from "@m4l/core";
import { getFormatDate as l } from "../../../formatters/DateFormatter/DateFormatter.js";
import { ColumnDateFormatter as c } from "./formatter.js";
import { ColumnDateGroupFormatter as g } from "./ColumnDateGroupFormatter.js";
import { useFormatter as d } from "@m4l/graphics";
import { getValidDate as f } from "../../../../utils/getValidDate.js";
import { useState as D, useRef as y, useEffect as C, useMemo as V, useCallback as F } from "react";
import { deepEqual as S } from "fast-equals";
import { getColumnKey as s } from "../../helpers/shared/getColumnKey/getColumnKey.js";
const T = (e) => {
  const { dateFormatter: t } = d();
  return F((a, n) => {
    const u = s(e.fieldValue), o = i(a, u);
    let r = null;
    return typeof o == "string" ? r = new Date(o) : o instanceof Date && (r = o), !r || isNaN(r.getTime()) ? !1 : l(e.presentationType, r, t, e.format).includes(n);
  }, [e.presentationType, e.format, t]);
}, b = (e) => (t, m) => {
  const a = s(e.fieldValue), n = i(t, a), u = i(m, a), o = f(n), r = f(u);
  return !o || !r ? 0 : o.getTime() > r.getTime() ? 1 : o.getTime() < r.getTime() ? -1 : 0;
}, E = (e) => {
  const [t, m] = D(e), a = T(t), n = y({ ...e });
  return C(() => {
    S(n.current, e) || (n.current = e, m(e));
  }, [e]), V(() => ({
    formatter: c(t),
    renderGroupCell: g(t),
    customFilter: a,
    customSort: b(t)
  }), [t, a]);
};
export {
  E as useColumnDate,
  T as useCustomDateFilter
};
