import { jsx as t, jsxs as x, Fragment as E } from "react/jsx-runtime";
import { useEffect as A, useMemo as N, Fragment as D } from "react";
import { a as I } from "../../../useQuery-n4ls1rLk.js";
import { a as k } from "../../../axios-f2q_J-wz.js";
import { cn as d } from "../../../utils/functions.js";
import { RenderEach as u } from "../../../utils/components/RenderEach.js";
import { RenderIf as S } from "../../../utils/components/RenderIf.js";
import { useTableContext as g } from "../context/index.js";
import { useTableInfoRest as F } from "../utils/useTableInfoRest.js";
import { TableTd as C } from "./Elements.js";
import { FiltersBox as O } from "./Filters.js";
import { TableLoading as j, TableErrorData as M, TableEmptyData as q } from "./TableStatesComponents.js";
import { WrapperTableTdComponents as L } from "./TdComponents/WrapperTableTdComponents.js";
import { TableTheadVariants as R, TableThItem as U } from "./variants.js";
import { A as $ } from "../../../index-T7wQHOux.js";
import { m as B } from "../../../motion-dVICNqWI.js";
const W = ({ thead: e, isLastTh: s }) => {
  const l = e.cellType == "longText";
  if (e != null && e.thComponent) {
    const { componentKey: o, ...n } = e.thComponent, i = Object.assign({}, { ...n, thead: e, cellparam: e.cellParam, componentKey: o });
    return /* @__PURE__ */ x(
      "th",
      {
        style: e.thStyles,
        className: d(
          "_text-start _whitespace-nowrap _p-0 _sticky _top-0",
          { "_w-96": l },
          e.isAction ? "_w-5 _min-w-[50px]" : "_min-w-[100px]",
          e.thClassName
        ),
        children: [
          " ",
          /* @__PURE__ */ t(L, { ...i }),
          " "
        ]
      }
    );
  }
  return /* @__PURE__ */ t(
    "th",
    {
      style: e.thStyles,
      className: d(
        "_text-start _whitespace-nowrap _p-0 _sticky _top-0",
        { "_w-96": l },
        e.isAction ? "_w-5 _min-w-[50px]" : "_min-w-[100px]",
        e.thClassName
      ),
      children: /* @__PURE__ */ x("div", { className: d(U({ variant: "govco" }), "_flex _justify-between _items-center"), children: [
        /* @__PURE__ */ t("p", { children: e.columnName }),
        /* @__PURE__ */ t(S, { condition: !!(e.filter || e.canSort), children: /* @__PURE__ */ t(O, { isLastTh: s, thead: e }) })
      ] })
    }
  );
}, ce = ({ tableConfig: e }) => /* @__PURE__ */ t("thead", { className: d(R({ variant: "govco" }), "_sticky _top-0 _z-[1] _border-none"), children: /* @__PURE__ */ t("tr", { className: "_sticky _top-0 _z-10 _bg-primary/10 _border _border-b-primary", children: /* @__PURE__ */ t(
  u,
  {
    of: e,
    render: (s, l) => /* @__PURE__ */ t(W, { isLastTh: e.length - 1 == l, thead: s }, s.cellParam)
  }
) }) }), me = ({ tableConfig: e, data: s, url: l, headers: o }) => {
  const [n] = g((m) => m.setterDataConfig), [i] = g((m) => m[(n == null ? void 0 : n.targetDataId) ?? "checkedData"]);
  return A(() => {
    i && (n == null || n.setterFn(i));
  }, [i]), l ? /* @__PURE__ */ t(z, { tableConfig: e, url: l, headers: o }) : /* @__PURE__ */ t("tbody", { className: "_relative", children: /* @__PURE__ */ t(K, { data: s, tableConfig: e }) });
}, z = ({ tableConfig: e, url: s, headers: l }) => {
  var T, a;
  const [o, n] = g((r) => r.metaDataPagination), [i] = g((r) => r.mutateDataConfig), { queryParams: m } = F(), { data: c, isError: b, isPending: h } = I({
    queryKey: ["tbodyWithUrl", s, `${o == null ? void 0 : o.currentPage}`, m],
    queryFn: async () => {
      const r = m, p = k.create({
        headers: {
          "Content-Type": "application/json",
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
          "Access-Control-Allow-Credentials": !0,
          "Access-Control-Allow-Headers": "*",
          ...l
        }
      }), { data: _ } = await p.get(r).then((f) => f);
      return _;
    },
    select: (r) => {
      if (!i)
        return r;
      if (i.targetIdProperty) {
        const p = r[i.targetIdProperty];
        return { items: p == null ? void 0 : p.map((f, P, y) => ({
          ...f,
          [i.newPropertyName]: `${y[P][i.targetItems[0]]}#${y[P][i.targetItems[1]]}`
        })), meta: r.meta };
      }
    }
  });
  return A(() => {
    c && n((r) => {
      r.metaDataPagination = c.meta;
    });
  }, [c]), /* @__PURE__ */ x($, { mode: "wait", initial: !1, children: [
    h ? /* @__PURE__ */ t(j, {}) : null,
    b ? /* @__PURE__ */ t(M, {}) : null,
    !c || (((T = c.items) == null ? void 0 : T.length) ?? 0) == 0 ? /* @__PURE__ */ t(q, {}) : null,
    !h && !b && c && (((a = c.items) == null ? void 0 : a.length) ?? 0) ? /* @__PURE__ */ t(
      B.tbody,
      {
        className: "_relative",
        animate: { opacity: 1 },
        exit: { opacity: 0 },
        initial: { opacity: 0 },
        transition: { duration: 0.4, delay: 0.4 },
        children: /* @__PURE__ */ t(
          u,
          {
            of: c.items,
            render: (r, p) => /* @__PURE__ */ t("tr", { className: "_border-b _border-slate-100", children: /* @__PURE__ */ t(u, { of: e, render: (_) => /* @__PURE__ */ t(C, { data: r, dataConfig: _ }, _.cellParam) }) }, p)
          }
        )
      },
      "dataTable"
    ) : null
  ] });
}, K = ({ tableConfig: e, data: s }) => /* @__PURE__ */ t(
  u,
  {
    of: s,
    render: (l, o) => /* @__PURE__ */ t("tr", { className: "_border-b _border-slate-100", children: /* @__PURE__ */ t(u, { of: e, render: (n) => /* @__PURE__ */ t(C, { data: l, dataConfig: n }, n.cellParam) }) }, o)
  }
), pe = () => {
  const [e, s] = g((a) => a.metaDataPagination), l = N(() => Array.from({ length: (e == null ? void 0 : e.totalPages) ?? 1 }, (a, r) => r + 1), [e == null ? void 0 : e.totalPages]), o = (e == null ? void 0 : e.currentPage) ?? 1, n = (e == null ? void 0 : e.totalPages) ?? 1, i = o == 1, m = o == n, c = (a) => {
    s((r) => {
      r.metaDataPagination && (r.metaDataPagination.currentPage = a);
    });
  }, b = () => {
    i || s((a) => {
      a.metaDataPagination && (a.metaDataPagination.currentPage = o - 1);
    });
  }, h = () => {
    m || s((a) => {
      a.metaDataPagination && (a.metaDataPagination.currentPage = o + 1);
    });
  }, T = N(() => {
    if (n < 2)
      return [1];
    const a = o > 3 ? [1, "..."] : [1, 2], r = n == 0 ? [] : [n], p = o < n - 2 ? ["...", n] : r, _ = Math.max(1, o - 1), f = Math.min(o + 1, n), P = l.slice(_ - 1, f), y = [...a, ...P, ...p];
    return y.filter((w, v) => y.indexOf(w) === v || w === "...");
  }, [e]);
  return /* @__PURE__ */ x("div", { className: "_flex _gap-1 _w-min _px-2 _py-1 _mt-2 _rounded-md", children: [
    /* @__PURE__ */ t("span", { onClick: b, className: d("_underline _cursor-pointer _text-primary _select-none", { "_text-gray-300": i }), children: "Anterior" }),
    /* @__PURE__ */ t("div", { className: "_flex _justify-center _items-center _gap-1 _w-56", children: /* @__PURE__ */ t(
      u,
      {
        of: T,
        render: (a, r) => /* @__PURE__ */ t(D, { children: a === "..." ? /* @__PURE__ */ t("span", { className: "_cursor-default", children: "..." }, r) : /* @__PURE__ */ t(
          "p",
          {
            className: d("_py-0.5 _px-2.5 _rounded-full _cursor-pointer hover:_bg-primary/80 hover:_text-white", {
              "_bg-primary _text-white": o == a
            }),
            onClick: () => {
              c(+a);
            },
            children: a
          },
          r
        ) }, r)
      }
    ) }),
    /* @__PURE__ */ t("span", { onClick: h, className: d("_underline _cursor-pointer _text-primary _select-none", { "_text-gray-300": m }), children: "Siguiente" })
  ] });
}, _e = () => {
  const [e] = g((s) => s.pagination);
  return /* @__PURE__ */ t(E, { children: /* @__PURE__ */ t("div", { className: "_w-full", children: JSON.stringify(e) }) });
};
export {
  pe as TablePagination,
  me as TableTbody,
  _e as TableTest,
  ce as TableThead
};
