"use client";
import { jsxs as a, jsx as l } from "react/jsx-runtime";
import { AppSelect as h } from "../../../app-select.js";
import { Badge as S } from "../../../badge.js";
import { Button as g } from "../../../button.js";
import { Icon as z } from "../../../icon.js";
import { Popover as C, PopoverTrigger as D, PopoverContent as L } from "../../../popover.js";
function B({
  table: c,
  sortedColumns: f = [],
  sortableColumns: d,
  i18n: o,
  maxSortedColumns: m = 3
}) {
  const r = c.getState().sorting || [], p = new Set(r.map((n) => n.id)), u = (n, t) => {
    const e = r[n], s = r.map(
      (i, N) => N === n ? { id: t, desc: e?.desc ?? !1 } : i
    );
    c.setSorting(s);
  }, x = (n, t) => {
    const e = r.map(
      (s, i) => i === n ? { ...s, desc: t === "desc" } : s
    );
    c.setSorting(e);
  }, v = () => {
    if (r.length >= m) return;
    const n = new Set(r.map((i) => i.id)), e = (d.find((i) => !n.has(i.column.id)) ?? d[0])?.column.id;
    if (!e) return;
    const s = [...r, { id: e, desc: !1 }];
    c.setSorting(s);
  }, w = (n) => {
    const t = r.filter((e, s) => s !== n);
    c.setSorting(t);
  };
  return /* @__PURE__ */ a(C, { children: [
    /* @__PURE__ */ l(D, { asChild: !0, children: /* @__PURE__ */ a(
      S,
      {
        variant: f.length > 0 ? "default" : "ghost",
        className: "hover:border-d-border cursor-pointer hover:border",
        children: [
          /* @__PURE__ */ l(z, { name: "ArrowUpDown" }),
          o.sorting.sort
        ]
      }
    ) }),
    /* @__PURE__ */ a(L, { className: "flex w-[400px] flex-col gap-2", align: "start", children: [
      /* @__PURE__ */ l("div", { className: "text-d-foreground text-md font-semibold", children: o.sorting.sorting }),
      f.length > 0 && /* @__PURE__ */ a("div", { className: "flex flex-col gap-2", children: [
        /* @__PURE__ */ a("div", { className: "flex w-full flex-row gap-1 pr-9 text-sm", children: [
          /* @__PURE__ */ l("div", { className: "flex flex-1", children: o.sorting.column }),
          /* @__PURE__ */ l("div", { className: "flex flex-1", children: o.sorting.order })
        ] }),
        f.map((n, t) => /* @__PURE__ */ a("div", { className: "flex w-full flex-row items-end gap-1", children: [
          /* @__PURE__ */ l(
            h,
            {
              size: "sm",
              wrpClassName: "flex flex-1",
              options: d.filter((e) => {
                const s = e.column.id;
                return s === n?.column.id || !p.has(s);
              }).map((e) => ({
                value: e.column.id,
                label: e.column.columnDef.meta?.headerLabel || (typeof e.column.columnDef.header == "string" ? e.column.columnDef.header : "") || e.column.id
              })),
              value: n?.column.id,
              onValueChange: (e) => {
                e != null && u(t, String(e));
              }
            }
          ),
          /* @__PURE__ */ l(
            h,
            {
              size: "sm",
              wrpClassName: "flex flex-1",
              options: [
                { value: "asc", label: o.sorting.ascending },
                { value: "desc", label: o.sorting.descending }
              ],
              value: r?.[t]?.desc ? "desc" : "asc",
              onValueChange: (e) => {
                e && x(t, e);
              }
            }
          ),
          /* @__PURE__ */ l(
            g,
            {
              variant: "ghost-destructive",
              size: "sm",
              onClick: () => w(t),
              iconLeft: "X"
            }
          )
        ] }, t))
      ] }),
      /* @__PURE__ */ a("div", { className: "flex w-full gap-2", children: [
        /* @__PURE__ */ l(
          g,
          {
            className: "flex flex-1",
            variant: "ghost-destructive",
            size: "sm",
            onClick: () => c.resetSorting(),
            disabled: r.length === 0 || d.length === 0,
            children: o.sorting.resetSorting
          }
        ),
        /* @__PURE__ */ l(
          g,
          {
            className: "flex flex-1",
            variant: "ghost",
            size: "sm",
            onClick: v,
            disabled: d.length === 0 || r.length >= m,
            children: o.sorting.addSort
          }
        )
      ] })
    ] })
  ] });
}
export {
  B as DataTableSortingComponent
};
