import { useCallback as p, useState as d } from "react";
import { useTableContext as F } from "./index.js";
import { useGetIntervalFilterByParam as g } from "../hooks/usePaginationSelector.js";
const P = () => {
  const [t, s] = F((n) => n.pagination), f = (n, o, r, l) => {
    const c = t == null ? void 0 : t.filt, e = { param: n, signal: o, value: l, type: r };
    if (c) {
      let i = [...c].filter((m) => m.param !== n);
      l && i.push(e);
      const u = i.length > 0 ? i : void 0;
      s((m) => {
        m.pagination.filt = u;
      });
      return;
    }
    l && s((i) => {
      i.pagination = {
        ...i.pagination,
        filt: [e]
      };
    });
  }, a = p(
    (n) => {
      var o, r;
      return ((r = (o = t == null ? void 0 : t.filt) == null ? void 0 : o.filter((l) => l.param === n)[0]) == null ? void 0 : r.value) ?? "";
    },
    [t == null ? void 0 : t.filt]
  );
  return {
    simpleSetFilter: f,
    getCurrentFiltValue: a
  };
}, y = () => {
  const [t, s] = F((n) => n.pagination);
  return {
    sortByParam: (n, o) => {
      const r = t == null ? void 0 : t.sort, l = r == null ? void 0 : r.findIndex((e) => e.selector === n);
      if (r && l !== void 0 && l >= 0) {
        let e = [...r];
        e[l].desc === o ? e.splice(l, 1) : e[l] = { selector: n, desc: o }, s((i) => {
          i.pagination.sort = e.length > 0 ? e : void 0;
        });
        return;
      }
      const c = { desc: o, selector: n };
      s((e) => {
        e.pagination = {
          ...e.pagination,
          sort: [...r ?? [], c]
        };
      });
    },
    currentSortByParam: (n) => {
      const o = t == null ? void 0 : t.sort, r = o == null ? void 0 : o.findIndex((l) => l.selector === n);
      if (o && r !== void 0 && r >= 0)
        return o[r];
    }
  };
}, I = ({ thead: t }) => {
  var c;
  const { currentValue: s, intervalSetFilter: f } = g(((c = t.filter) == null ? void 0 : c.param) || t.cellParam), [a, n] = d({ major: `${s.major ?? ""}`, minor: `${s.minor ?? ""}` }), o = (e) => {
    const { value: i, id: u } = e.target;
    n({ ...a, [u]: i });
  }, r = (e) => {
    e.preventDefault();
    let i = [];
    for (const u in a)
      i.push(l(u, a[u]));
    f(i);
  }, l = (e, i) => ({ ...t.filter, signal: e === "minor" ? "<=" : ">=", value: i });
  return { changeFiltValue: o, sendValue: r, state: a, currentValue: s };
};
export {
  I as useHandleIntervalFilter,
  P as useManagementFilter,
  y as useSortTable
};
