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