"use client";
import { jsxs as p, Fragment as E, jsx as o } from "react/jsx-runtime";
import { useState as M, useEffect as G } from "react";
import { Badge as S } from "../../../badge.js";
import { Button as P } from "../../../button.js";
import { Icon as D } from "../../../icon.js";
import { Input as R } from "../../../input.js";
import { Popover as _, PopoverTrigger as w, PopoverContent as C } from "../../../popover.js";
import { getIconFromColumnType as z } from "../data-table.service.js";
import { DataTableAdvancedFilterComponent as J } from "./data-table-advanced-filter.js";
import { DataTableStringFilter as K, DataTableNumberFilter as Q, DataTableBooleanFilter as U, DataTableDateFilter as V, DataTableDateTimeFilter as W, DataTableListSingleSelectFilter as X, DataTableListMultiSelectFilter as Y } from "./data-table-simple-filters.js";
function ae({
  advancedFilterBadge: m,
  setAdvancedFilterBadge: f,
  filterableColumns: h,
  sortedFilterBadges: I,
  setFilterBadges: d,
  filterSearch: j,
  setFilterSearch: k,
  filteredColumns: A,
  handleAddFilter: q,
  handleAddAdvancedFilter: F,
  pendingOpenFilterId: O,
  onPendingOpenFilterHandled: N,
  i18n: s
}) {
  const [x, T] = M(/* @__PURE__ */ new Set()), L = x.has("add");
  return G(() => {
    O && (T(/* @__PURE__ */ new Set([O])), N());
  }, [O, N]), G(() => {
    if (d((e) => {
      let a = !1;
      const i = e.map((r) => {
        if (r.columnType !== "list_single_select" && r.columnType !== "list_multi_select")
          return r;
        const n = (
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          h.find((t) => t.id === r.columnId)?.column.columnDef.meta?.listOptions || []
        ), g = new Set(n.map((t) => String(t.value)));
        if (r.columnType === "list_single_select") {
          const t = r.value;
          return t !== void 0 && !g.has(String(t)) ? (a = !0, { ...r, value: void 0, listOptions: n }) : r.listOptions !== n ? { ...r, listOptions: n } : r;
        } else {
          const t = Array.isArray(r.value) ? r.value : [], v = t.filter((u) => g.has(String(u)));
          return v.length !== t.length ? (a = !0, {
            ...r,
            value: v.length > 0 ? v : void 0,
            listOptions: n
          }) : r.listOptions !== n ? { ...r, listOptions: n } : r;
        }
      });
      return a ? i : e;
    }), m) {
      const e = (l) => {
        let n = !1;
        return { conditions: l.map((t) => {
          if (t.columnType !== "list_single_select" && t.columnType !== "list_multi_select")
            return t;
          const u = (
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            h.find(
              (c) => c.id === t.columnId
            )?.column.columnDef.meta?.listOptions || []
          ), b = new Set(u.map((c) => String(c.value)));
          if (t.columnType === "list_single_select") {
            const c = t.value;
            return c !== void 0 && !b.has(String(c)) ? (n = !0, {
              ...t,
              value: void 0,
              listOptions: u
            }) : t.listOptions !== u ? { ...t, listOptions: u } : t;
          } else {
            const c = Array.isArray(t.value) ? t.value : [], y = c.filter(($) => b.has(String($)));
            return y.length !== c.length ? (n = !0, {
              ...t,
              value: y.length > 0 ? y : void 0,
              listOptions: u
            }) : t.listOptions !== u ? { ...t, listOptions: u } : t;
          }
        }), changed: n };
      }, a = (l) => {
        let n = !1;
        const { conditions: g, changed: t } = e(l.conditions);
        t && (n = !0);
        const v = l.groups?.map((u) => {
          const { group: b, changed: c } = a(u);
          return c && (n = !0), b;
        });
        return {
          group: {
            ...l,
            conditions: g,
            groups: v
          },
          changed: n
        };
      }, { group: i, changed: r } = a(
        m.rootGroup
      );
      r && f({
        ...m,
        rootGroup: i
      });
    }
  }, [
    h,
    d,
    m,
    f
  ]), /* @__PURE__ */ p(E, { children: [
    m && /* @__PURE__ */ p(_, { children: [
      /* @__PURE__ */ o(w, { asChild: !0, children: /* @__PURE__ */ p(
        S,
        {
          className: "hover:border-d-border cursor-pointer hover:border",
          variant: "default",
          children: [
            /* @__PURE__ */ o(D, { name: "ListFilter" }),
            s.filters.advancedFilter
          ]
        }
      ) }),
      /* @__PURE__ */ o(C, { className: "w-[800px]", align: "start", children: /* @__PURE__ */ o(
        J,
        {
          columns: h,
          onFilterChange: (e) => {
            f(e || void 0);
          },
          initialFilter: m,
          i18n: s
        }
      ) })
    ] }),
    I.map((e) => {
      const a = x.has(e.id);
      return /* @__PURE__ */ p(
        _,
        {
          open: a,
          onOpenChange: (i) => {
            T((r) => {
              const l = new Set(r);
              return i ? l.add(e.id) : l.delete(e.id), l;
            });
          },
          children: [
            /* @__PURE__ */ o(w, { asChild: !0, children: /* @__PURE__ */ p(
              S,
              {
                variant: (e.value === void 0 || e.value === "") && !(e.operator === "eq_null" || e.operator === "n_eq_null" || e.operator === "checked" || e.operator === "unchecked") ? "ghost" : "default",
                className: `hover:border-d-border flex cursor-pointer items-center gap-1 hover:border ${a && "border-d-foreground border"}`,
                children: [
                  /* @__PURE__ */ o(D, { name: "Filter" }),
                  e.columnLabel || s.filters.value
                ]
              }
            ) }),
            /* @__PURE__ */ p(C, { align: "start", children: [
              e.columnType === "string" && /* @__PURE__ */ o(
                K,
                {
                  filter: e,
                  setFilterBadges: d,
                  i18n: s
                }
              ),
              e.columnType === "number" && /* @__PURE__ */ o(
                Q,
                {
                  filter: e,
                  setFilterBadges: d,
                  i18n: s
                }
              ),
              e.columnType === "boolean" && /* @__PURE__ */ o(
                U,
                {
                  filter: e,
                  setFilterBadges: d,
                  i18n: s
                }
              ),
              e.columnType === "date" && /* @__PURE__ */ o(
                V,
                {
                  filter: e,
                  setFilterBadges: d,
                  i18n: s
                }
              ),
              e.columnType === "datetime" && /* @__PURE__ */ o(
                W,
                {
                  filter: e,
                  setFilterBadges: d,
                  i18n: s
                }
              ),
              e.columnType === "list_single_select" && (() => {
                const r = (
                  // eslint-disable-next-line @typescript-eslint/no-explicit-any
                  h.find(
                    (l) => l.id === e.columnId
                  )?.column.columnDef.meta?.listOptions || e.listOptions || []
                );
                return /* @__PURE__ */ o(
                  X,
                  {
                    filter: { ...e, listOptions: r },
                    setFilterBadges: d,
                    i18n: s
                  }
                );
              })(),
              e.columnType === "list_multi_select" && (() => {
                const r = (
                  // eslint-disable-next-line @typescript-eslint/no-explicit-any
                  h.find(
                    (l) => l.id === e.columnId
                  )?.column.columnDef.meta?.listOptions || e.listOptions || []
                );
                return /* @__PURE__ */ o(
                  Y,
                  {
                    filter: { ...e, listOptions: r },
                    setFilterBadges: d,
                    i18n: s
                  }
                );
              })(),
              e.columnType === "other" && /* @__PURE__ */ o("div", { children: s.filters.filterNotSupported })
            ] })
          ]
        },
        e.id
      );
    }),
    h?.length > 0 && /* @__PURE__ */ p(
      _,
      {
        open: L,
        onOpenChange: (e) => {
          T((a) => {
            const i = new Set(a);
            return e ? i.add("add") : i.delete("add"), i;
          });
        },
        children: [
          /* @__PURE__ */ o(w, { asChild: !0, children: /* @__PURE__ */ p(
            S,
            {
              variant: "ghost",
              className: `hover:border-d-border flex cursor-pointer items-center gap-1 hover:border ${L && "border-d-foreground border"}`,
              children: [
                /* @__PURE__ */ o(D, { name: "FunnelPlus" }),
                s.filters.addFilter
              ]
            }
          ) }),
          /* @__PURE__ */ p(C, { className: "p-2", align: "start", children: [
            /* @__PURE__ */ o(
              R,
              {
                className: "mb-1",
                placeholder: s.filters.searchFilters,
                iconLeft: "Search",
                onChange: (e) => k(e.target.value),
                value: j
              }
            ),
            /* @__PURE__ */ o("div", { className: "max-h-[400px] overflow-y-auto", children: A.map((e) => /* @__PURE__ */ o("div", { children: /* @__PURE__ */ o(
              P,
              {
                variant: "ghost",
                className: "w-full justify-start",
                iconLeft: z(
                  e.column.columnDef.meta?.type
                ),
                onClick: () => {
                  q(e);
                },
                children: e.column.columnDef.meta?.headerLabel || (typeof e.column.columnDef.header == "string" ? e.column.columnDef.header : "") || e.column.id
              }
            ) }, e.id)) }),
            /* @__PURE__ */ o("div", { className: "border-d-border mb-1 w-full border-b" }),
            /* @__PURE__ */ o(
              P,
              {
                variant: "ghost",
                className: "w-full justify-start",
                iconLeft: "ListFilterPlus",
                disabled: !!m,
                onClick: () => F(),
                children: s.filters.advancedFilter
              }
            )
          ] })
        ]
      }
    )
  ] });
}
export {
  ae as DataTableFilters
};
