import { jsx as t, jsxs as u } from "react/jsx-runtime";
import { useCallback as n, useState as O, useEffect as N } from "react";
import k from "../../types/FilterLogicalOperator.js";
import D from "../../types/FilterConditionOperator.js";
import { Dialog as S, DialogContent as B, DialogHeader as E, DialogTitle as $, DialogFooter as j } from "../../../../../dialog/DialogPrimitive.js";
import d from "../../../../../box/Box.js";
import V from "../advanced-filter-column-selector/AdvancedFilterColumnSelector.js";
import z from "../advanced-filter-operator-selector/AdvancedFilterOperatorSelector.js";
import J from "../advanced-filter-value-input/AdvancedFilterValueInput.js";
import L from "../advanced-filter-action-buttons/AdvancedFilterActionButtons.js";
import g from "../../../../../button/Button.js";
const X = ({ columns: i, value: s, onChange: m, isOpen: h, onClose: p }) => {
  const c = n(() => ({
    column: i[0],
    operator: D.Equals,
    value: "",
    logicalOperator: k.And
  }), [i]), [f, o] = O([c()]);
  N(() => {
    s && s.length > 0 ? o(JSON.parse(JSON.stringify(s))) : o([c()]);
  }, [s, c, h]);
  const F = n(() => {
    o((e) => {
      const r = [...e];
      return r.push(c()), r;
    });
  }, [c]), C = n((e) => {
    o((r) => {
      const l = [...r];
      return l.length > 1 && l.splice(e, 1), l;
    });
  }, []), x = n(
    (e, r) => {
      o((l) => {
        const a = [...l], v = i.find((b) => b.key === r);
        return v && (a[e].column = v, a[e].value = ""), a;
      });
    },
    [i]
  ), w = n((e, r) => {
    o((l) => {
      const a = [...l];
      return a[e].operator = r, a;
    });
  }, []), y = n((e, r) => {
    o((l) => {
      const a = [...l];
      return a[e].value = r, a;
    });
  }, []), A = n(() => {
    m == null || m(f), p();
  }, [f, m, p]);
  return /* @__PURE__ */ t(S, { open: h, onOpenChange: (e) => !e && p(), children: /* @__PURE__ */ u(B, { className: "max-w-[634px]", children: [
    /* @__PURE__ */ t(E, { className: "text-left", children: /* @__PURE__ */ t($, { children: "Erweiterte Filter" }) }),
    /* @__PURE__ */ u(d, { className: "flex flex-col gap-[var(--spacing-4)] max-h-[580px] overflow-y-auto overflow-x-visible -my-3 py-3 -mx-3 px-3", children: [
      f.map((e, r) => /* @__PURE__ */ u(
        d,
        {
          className: "flex items-center gap-[var(--spacing-4)]",
          children: [
            /* @__PURE__ */ t(
              V,
              {
                condition: e,
                conditionIndex: r,
                columns: i,
                onColumnChange: x
              }
            ),
            /* @__PURE__ */ t(z, { condition: e, conditionIndex: r, onOperatorChange: w }),
            /* @__PURE__ */ t(J, { condition: e, conditionIndex: r, onValueChange: y }),
            /* @__PURE__ */ t(
              L,
              {
                disabled: f.length === 1,
                conditionIndex: r,
                onRemoveCondition: C
              }
            )
          ]
        },
        `condition-${e.column.key}-${e.operator}-${e.value}-${r}`
      )),
      /* @__PURE__ */ t(d, { children: /* @__PURE__ */ t(g, { variant: "ghost", size: "sm", onClick: () => F(), icon: "Plus", label: "Weitere Bedingung hinzufügen" }) })
    ] }),
    /* @__PURE__ */ u(j, { className: "flex justify-end gap-[var(--spacing-4)]", children: [
      /* @__PURE__ */ t(g, { variant: "secondary", onClick: p, label: "Abbrechen" }),
      /* @__PURE__ */ t(g, { onClick: A, label: "Filtern" })
    ] })
  ] }) });
};
export {
  X as default
};
//# sourceMappingURL=AdvancedFilterDialog.js.map
