import { jsx as e, 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";
import "../../../clsx-7d8NmkQq.js";
import "../../../context/index.js";
import "../../../dayjs.min-KYonM6tp.js";
import "../../../_commonjsHelpers-2ufMB8rV.js";
import "../../../hooks/useClickOutside.js";
import "../../Buttons/index.js";
import "../../../index.esm-BQxOvVZs.js";
import "../../Icons.js";
import "../../../context/generalContext/index.js";
import "../../Buttons/variants.js";
import "../../../index-xVF7D3FM.js";
import "../../../index-OHO5PlsB.js";
import "../../Inputs/variants.js";
import "../context/useCases.js";
import "../hooks/usePaginationSelector.js";
import "../hooks/useHandleInputWithTimer.js";
import "../../../LayoutGroupContext-3euHzeKi.js";
import "../../../SVGVisualElement-aqO4D4Z5.js";
import "../../../use-unmount-effect-Wy5iLAex.js";
const W = ({ thead: t, isLastTh: i }) => {
  const l = t.cellType == "longText";
  if (t != null && t.thComponent) {
    const { componentKey: a, ...o } = t.thComponent, s = Object.assign({}, { ...o, thead: t, cellparam: t.cellParam, componentKey: a });
    return /* @__PURE__ */ x(
      "th",
      {
        style: t.thStyles,
        className: d(
          "_text-start _whitespace-nowrap _p-0 _sticky _top-0",
          { "_w-96": l },
          t.isAction ? "_w-5 _min-w-[50px]" : "_min-w-[100px]",
          t.thClassName
        ),
        children: [
          " ",
          /* @__PURE__ */ e(L, { ...s }),
          " "
        ]
      }
    );
  }
  return /* @__PURE__ */ e(
    "th",
    {
      style: t.thStyles,
      className: d(
        "_text-start _whitespace-nowrap _p-0 _sticky _top-0",
        { "_w-96": l },
        t.isAction ? "_w-5 _min-w-[50px]" : "_min-w-[100px]",
        t.thClassName
      ),
      children: /* @__PURE__ */ x("div", { className: d(U({ variant: "govco" }), "_flex _justify-between _items-center"), children: [
        /* @__PURE__ */ e("p", { children: t.columnName }),
        /* @__PURE__ */ e(S, { condition: !!(t.filter || t.canSort), children: /* @__PURE__ */ e(O, { isLastTh: i, thead: t }) })
      ] })
    }
  );
}, Et = ({ tableConfig: t }) => /* @__PURE__ */ e("thead", { className: d(R({ variant: "govco" }), "_sticky _top-0 _z-[1] _border-none"), children: /* @__PURE__ */ e("tr", { className: "_sticky _top-0 _z-10 _bg-primary/10 _border _border-b-primary", children: /* @__PURE__ */ e(
  u,
  {
    of: t,
    render: (i, l) => /* @__PURE__ */ e(W, { isLastTh: t.length - 1 == l, thead: i }, i.cellParam)
  }
) }) }), Dt = ({ tableConfig: t, data: i, url: l, headers: a }) => {
  const [o] = g((m) => m.setterDataConfig), [s] = g((m) => m[(o == null ? void 0 : o.targetDataId) ?? "checkedData"]);
  return A(() => {
    s && (o == null || o.setterFn(s));
  }, [s]), l ? /* @__PURE__ */ e(z, { tableConfig: t, url: l, headers: a }) : /* @__PURE__ */ e("tbody", { className: "_relative", children: /* @__PURE__ */ e(K, { data: i, tableConfig: t }) });
}, z = ({ tableConfig: t, url: i, headers: l }) => {
  var T, n;
  const [a, o] = g((r) => r.metaDataPagination), [s] = g((r) => r.mutateDataConfig), { queryParams: m } = F(), { data: c, isError: b, isPending: h } = I({
    queryKey: ["tbodyWithUrl", i, `${a == null ? void 0 : a.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 (!s)
        return r;
      if (s.targetIdProperty) {
        const p = r[s.targetIdProperty];
        return { items: p == null ? void 0 : p.map((f, P, y) => ({
          ...f,
          [s.newPropertyName]: `${y[P][s.targetItems[0]]}#${y[P][s.targetItems[1]]}`
        })), meta: r.meta };
      }
    }
  });
  return A(() => {
    c && o((r) => {
      r.metaDataPagination = c.meta;
    });
  }, [c]), /* @__PURE__ */ x($, { mode: "wait", initial: !1, children: [
    h ? /* @__PURE__ */ e(j, {}) : null,
    b ? /* @__PURE__ */ e(M, {}) : null,
    !c || (((T = c.items) == null ? void 0 : T.length) ?? 0) == 0 ? /* @__PURE__ */ e(q, {}) : null,
    !h && !b && c && (((n = c.items) == null ? void 0 : n.length) ?? 0) ? /* @__PURE__ */ e(
      B.tbody,
      {
        className: "_relative",
        animate: { opacity: 1 },
        exit: { opacity: 0 },
        initial: { opacity: 0 },
        transition: { duration: 0.4, delay: 0.4 },
        children: /* @__PURE__ */ e(
          u,
          {
            of: c.items,
            render: (r, p) => /* @__PURE__ */ e("tr", { className: "_border-b _border-slate-100", children: /* @__PURE__ */ e(u, { of: t, render: (_) => /* @__PURE__ */ e(C, { data: r, dataConfig: _ }, _.cellParam) }) }, p)
          }
        )
      },
      "dataTable"
    ) : null
  ] });
}, K = ({ tableConfig: t, data: i }) => /* @__PURE__ */ e(
  u,
  {
    of: i,
    render: (l, a) => /* @__PURE__ */ e("tr", { className: "_border-b _border-slate-100", children: /* @__PURE__ */ e(u, { of: t, render: (o) => /* @__PURE__ */ e(C, { data: l, dataConfig: o }, o.cellParam) }) }, a)
  }
), It = () => {
  const [t, i] = g((n) => n.metaDataPagination), l = N(() => Array.from({ length: (t == null ? void 0 : t.totalPages) ?? 1 }, (n, r) => r + 1), [t == null ? void 0 : t.totalPages]), a = (t == null ? void 0 : t.currentPage) ?? 1, o = (t == null ? void 0 : t.totalPages) ?? 1, s = a == 1, m = a == o, c = (n) => {
    i((r) => {
      r.metaDataPagination && (r.metaDataPagination.currentPage = n);
    });
  }, b = () => {
    s || i((n) => {
      n.metaDataPagination && (n.metaDataPagination.currentPage = a - 1);
    });
  }, h = () => {
    m || i((n) => {
      n.metaDataPagination && (n.metaDataPagination.currentPage = a + 1);
    });
  }, T = N(() => {
    if (o < 2)
      return [1];
    const n = a > 3 ? [1, "..."] : [1, 2], r = o == 0 ? [] : [o], p = a < o - 2 ? ["...", o] : r, _ = Math.max(1, a - 1), f = Math.min(a + 1, o), P = l.slice(_ - 1, f), y = [...n, ...P, ...p];
    return y.filter((w, v) => y.indexOf(w) === v || w === "...");
  }, [t]);
  return /* @__PURE__ */ x("div", { className: "_flex _gap-1 _w-min _px-2 _py-1 _mt-2 _rounded-md", children: [
    /* @__PURE__ */ e("span", { onClick: b, className: d("_underline _cursor-pointer _text-primary _select-none", { "_text-gray-300": s }), children: "Anterior" }),
    /* @__PURE__ */ e("div", { className: "_flex _justify-center _items-center _gap-1 _w-56", children: /* @__PURE__ */ e(
      u,
      {
        of: T,
        render: (n, r) => /* @__PURE__ */ e(D, { children: n === "..." ? /* @__PURE__ */ e("span", { className: "_cursor-default", children: "..." }, r) : /* @__PURE__ */ e(
          "p",
          {
            className: d("_py-0.5 _px-2.5 _rounded-full _cursor-pointer hover:_bg-primary/80 hover:_text-white", {
              "_bg-primary _text-white": a == n
            }),
            onClick: () => {
              c(+n);
            },
            children: n
          },
          r
        ) }, r)
      }
    ) }),
    /* @__PURE__ */ e("span", { onClick: h, className: d("_underline _cursor-pointer _text-primary _select-none", { "_text-gray-300": m }), children: "Siguiente" })
  ] });
}, kt = () => {
  const [t] = g((i) => i.pagination);
  return /* @__PURE__ */ e(E, { children: /* @__PURE__ */ e("div", { className: "_w-full", children: JSON.stringify(t) }) });
};
export {
  It as TablePagination,
  Dt as TableTbody,
  kt as TableTest,
  Et as TableThead
};
