import { useMemo as m } from "react";
import { useTableContext as p } from "../context/index.js";
import "../../../context/index.js";
import "react/jsx-runtime";
const $ = (n) => {
  if (n) {
    if (n.length === 1) {
      const { param: t, value: e, signal: o } = n[0];
      return `["${t}", "${o}", "${e}"]`;
    }
    let a = [];
    for (const t of n)
      a.push(Object.assign(t));
    let r = [];
    const l = a.filter((t) => t.groupSignal === void 0).map((t) => `["${t.param}", "${t.signal}", "${t.value}"]`).toString();
    l && r.push(l.toString().replaceAll("],[", '],"and",['));
    const c = a.filter((t) => t.groupSignal && !t.isSearch).reduce(
      (t, e) => {
        let o = e.param;
        return (t[o] = t[o] || []).push(e), t;
      },
      {}
    );
    for (const t of Object.values(c)) {
      const e = t[0];
      if (t.length > 1) {
        const o = t.map((s) => `["${s.param}", "${s.signal}", "${s.value}"]`).toString().replaceAll("],[", `],"${e.groupSignal}",[`);
        r.push(o);
      }
      t.length === 1 && r.push(`["${e.param}","${e.signal}","${e.value}"]`);
    }
    const i = a.filter((t) => t.isSearch).map((t) => `["${t.param}","${t.signal}","${t.value}"]`);
    return i.length > 1 && r.length > 0 ? r.push(`[${i.toString().replaceAll("],[", '],"or",[')}]`) : i.length && r.push(`${i.toString().replaceAll("],[", '],"or",[')}`), `[${r.toString().replaceAll("],[", '],"and",[')}]`;
  }
  return "";
}, f = (n) => n && n.length > 0 ? `[${n.map((r) => `{"selector": "${r.selector}", "desc": ${r.desc}}`)}]` : "", A = () => {
  const [n] = p((t) => t.pagination), [a] = p((t) => t.url), [r, l] = p((t) => t.metaDataPagination), [g] = p((t) => t.dataConfig);
  return { queryParams: m(() => {
    if (n) {
      const { filt: t, limit: e, sort: o } = n, s = $(t), u = f(o);
      return a + "?" + new URLSearchParams({
        filt: s,
        limit: e ?? "20",
        page: (r == null ? void 0 : r.currentPage.toString()) || "1",
        sort: u
      }).toString();
    }
    return a + "?" + new URLSearchParams({ page: "1", limit: "20" }).toString();
  }, [n, r]), thead: g, changeFooterPaginate: (t) => {
    l((e) => {
      e.metaDataPagination = t;
    });
  } };
};
export {
  A as useTableInfoRest
};
