"use client";
import { jsx as t, jsxs as n } from "react/jsx-runtime";
import { flexRender as w } from "../../../../../node_modules/@tanstack/react-table/build/lib/index.js";
import S from "react";
import { cn as l } from "../../../../../lib/utils.js";
import { Button as i } from "../../../button.js";
import { Icon as m } from "../../../icon.js";
import { Popover as D, PopoverTrigger as y, PopoverContent as N } from "../../../popover.js";
import { TableHeader as I, TableRow as h, TableHead as C } from "../../../table.js";
import { getPinnedStyle as z } from "../data-table.service.js";
function F({
  table: r,
  headerRefs: g,
  pinnedOffsets: d,
  maxSortedColumns: c,
  i18n: s,
  searchbarGlobalFilter: f,
  isColumnFiltered: p,
  onAddFilter: v
}) {
  const [x, u] = S.useState({});
  return /* @__PURE__ */ t(I, { children: r.getHeaderGroups().map((a) => /* @__PURE__ */ t(h, { children: a.headers.map((e) => /* @__PURE__ */ t(
    C,
    {
      ref: (o) => {
        g.current[e.column.id] = o;
      },
      className: l(
        "bg-d-background sticky top-0 h-8 px-0",
        e.column.columnDef.meta?.pinned ? "z-[49]" : "z-10",
        e.column.columnDef.meta?.headerClassName
      ),
      style: z(
        e.column.id,
        d,
        e.column.columnDef.meta?.pinned,
        !0
      ),
      children: e.isPlaceholder ? null : /* @__PURE__ */ n(
        D,
        {
          open: !!x[e.id],
          onOpenChange: (o) => {
            u((b) => ({
              ...b,
              [e.id]: o
            }));
          },
          children: [
            /* @__PURE__ */ t(
              y,
              {
                asChild: !0,
                onClick: (o) => {
                  e.column.columnDef.meta?.sortable || e.column.columnDef.meta?.filterable || (o.preventDefault(), o.stopPropagation());
                },
                children: /* @__PURE__ */ n(
                  "div",
                  {
                    className: l(
                      "flex h-full items-center justify-between gap-2 px-2",
                      e.column.columnDef.meta?.sortable || e.column.columnDef.meta?.filterable ? "hover:bg-d-secondary-foreground/10 cursor-pointer" : "cursor-not-allowed"
                    ),
                    children: [
                      w(
                        e.column.columnDef.header,
                        e.getContext()
                      ),
                      /* @__PURE__ */ n("div", { className: "flex flex-row items-center gap-1", children: [
                        e.column.columnDef.meta?.searchable && /* @__PURE__ */ t(
                          m,
                          {
                            name: "Search",
                            className: l(
                              "text-d-foreground/30 max-h-3.5 max-w-3.5",
                              !!f && "text-d-primary"
                            )
                          }
                        ),
                        e.column.columnDef.meta?.filterable && /* @__PURE__ */ t(
                          m,
                          {
                            name: "Filter",
                            className: l(
                              "text-d-foreground/30 max-h-3.5 max-w-3.5",
                              p(e.column.id) && "text-d-primary"
                            )
                          }
                        ),
                        e.column.columnDef.meta?.sortable && /* @__PURE__ */ n("div", { className: "flex flex-col py-1", children: [
                          /* @__PURE__ */ t(
                            i,
                            {
                              iconLeft: "ChevronUp",
                              size: "icon",
                              className: l(
                                "hover:bg-d-foreground/10 max-h-4 max-w-6",
                                e.column.getIsSorted() === "asc" && "hover:text-d-destructive hover:bg-d-destructive/20"
                              ),
                              variant: e.column.getIsSorted() === "asc" ? "ghost-accent" : "ghost",
                              onClick: (o) => {
                                e.column.getIsSorted() === "asc" ? e.column.clearSorting() : e.column.toggleSorting(!1, !0), o.stopPropagation(), o.preventDefault();
                              },
                              disabled: r.getState().sorting.length >= c && !e.column.getIsSorted()
                            }
                          ),
                          /* @__PURE__ */ t(
                            i,
                            {
                              iconLeft: "ChevronDown",
                              size: "icon",
                              className: l(
                                "hover:bg-d-foreground/10 max-h-4 max-w-6",
                                e.column.getIsSorted() === "desc" && "hover:text-d-destructive hover:bg-d-destructive/20"
                              ),
                              variant: e.column.getIsSorted() === "desc" ? "ghost-accent" : "ghost",
                              onClick: (o) => {
                                e.column.getIsSorted() === "desc" ? e.column.clearSorting() : e.column.toggleSorting(!0, !0), o.stopPropagation(), o.preventDefault();
                              },
                              disabled: r.getState().sorting.length >= c && !e.column.getIsSorted()
                            }
                          )
                        ] })
                      ] })
                    ]
                  }
                )
              }
            ),
            /* @__PURE__ */ t(N, { className: "w-40 p-0", children: /* @__PURE__ */ n("div", { className: "flex flex-col text-sm", children: [
              e.column.columnDef.meta?.sortable && /* @__PURE__ */ n(
                "div",
                {
                  className: l(
                    "group hover:bg-d-secondary-foreground/5 relative flex cursor-pointer items-center justify-between rounded px-2 py-1.5",
                    e.column.getIsSorted() && "bg-d-secondary-foreground/5",
                    r.getState().sorting.length >= c && !e.column.getIsSorted() && "pointer-events-none cursor-not-allowed opacity-60"
                  ),
                  "aria-disabled": r.getState().sorting.length >= c && !e.column.getIsSorted(),
                  children: [
                    /* @__PURE__ */ t("span", { className: "text-md", children: s.sorting.sort }),
                    /* @__PURE__ */ t(m, { name: "ChevronRight", size: "xs" }),
                    /* @__PURE__ */ n("div", { className: "bg-d-background border-d-border absolute top-0 left-full ml-0 hidden w-48 flex-col rounded-md border p-1 shadow-md group-hover:flex", children: [
                      /* @__PURE__ */ t(
                        i,
                        {
                          className: "justify-start",
                          variant: "ghost",
                          size: "sm",
                          iconLeft: e.column.columnDef.meta?.type === "number" ? "ArrowDown01" : e.column.columnDef.meta?.type === "string" ? "ArrowDownAz" : "ArrowDownNarrowWide",
                          onClick: () => {
                            !e.column.getIsSorted() && r.getState().sorting.length >= c || e.column.toggleSorting(!1, !0);
                          },
                          children: s.sorting.ascending
                        }
                      ),
                      /* @__PURE__ */ t(
                        i,
                        {
                          className: "justify-start",
                          variant: "ghost",
                          size: "sm",
                          iconLeft: e.column.columnDef.meta?.type === "number" ? "ArrowUp01" : e.column.columnDef.meta?.type === "string" ? "ArrowUpAz" : "ArrowUpNarrowWide",
                          onClick: () => {
                            !e.column.getIsSorted() && r.getState().sorting.length >= c || e.column.toggleSorting(!0, !0);
                          },
                          children: s.sorting.descending
                        }
                      ),
                      e.column.getIsSorted() && /* @__PURE__ */ t(
                        i,
                        {
                          className: "text-d-destructive/80 hover:text-d-destructive justify-start",
                          variant: "ghost",
                          size: "sm",
                          iconLeft: "CircleX",
                          onClick: () => e.column.clearSorting(),
                          children: s.sorting.clearSort
                        }
                      )
                    ] })
                  ]
                }
              ),
              e.column.columnDef.meta?.filterable && /* @__PURE__ */ n(
                "div",
                {
                  className: "hover:bg-d-secondary-foreground/5 relative flex cursor-pointer items-center justify-between rounded px-2 py-1.5",
                  onClick: () => {
                    v(e), u((o) => ({
                      ...o,
                      [e.id]: !1
                    }));
                  },
                  children: [
                    /* @__PURE__ */ t("span", { className: "text-md", children: s.filters.filter }),
                    /* @__PURE__ */ t(m, { name: "Filter", size: "xs" })
                  ]
                }
              )
            ] }) })
          ]
        }
      )
    },
    e.id
  )) }, a.id)) });
}
export {
  F as DataTableHeaderComponent
};
