import U from "../../../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import * as t from "react";
import he from "../../../../../@ant-design/icons/es/icons/FilterFilled.mjs";
import H from "../../../../../classnames/index.mjs";
import J from "../../../../../rc-util/es/isEqual.mjs";
import ye from "../../../_util/hooks/useSyncState.mjs";
import { devUseWarning as Ce } from "../../../_util/warning.mjs";
import Q from "../../../button/button.mjs";
import Z from "../../../checkbox/index.mjs";
import { ConfigContext as ve } from "../../../config-provider/context.mjs";
import Ee from "../../../dropdown/index.mjs";
import X from "../../../empty/index.mjs";
import be from "../../../menu/index.mjs";
import { OverrideProvider as we } from "../../../menu/OverrideContext.mjs";
import Se from "../../../radio/index.mjs";
import De from "../../../tree/index.mjs";
import Y from "./FilterSearch.mjs";
import Ke from "./FilterWrapper.mjs";
function x(s) {
  let l = [];
  return (s || []).forEach((c) => {
    let {
      value: f,
      children: a
    } = c;
    l.push(f), a && (l = [].concat(U(l), U(x(a))));
  }), l;
}
function ke(s) {
  return s.some((l) => {
    let {
      children: c
    } = l;
    return c;
  });
}
function ee(s, l) {
  return typeof l == "string" || typeof l == "number" ? l == null ? void 0 : l.toString().toLowerCase().includes(s.trim().toLowerCase()) : !1;
}
function te(s) {
  let {
    filters: l,
    prefixCls: c,
    filteredKeys: f,
    filterMultiple: a,
    searchValue: n,
    filterSearch: p
  } = s;
  return l.map((u, m) => {
    const b = String(u.value);
    if (u.children)
      return {
        key: b || m,
        label: u.text,
        popupClassName: `${c}-dropdown-submenu`,
        children: te({
          filters: u.children,
          prefixCls: c,
          filteredKeys: f,
          filterMultiple: a,
          searchValue: n,
          filterSearch: p
        })
      };
    const g = a ? Z : Se, i = {
      key: u.value !== void 0 ? b : m,
      label: /* @__PURE__ */ t.createElement(t.Fragment, null, /* @__PURE__ */ t.createElement(g, {
        checked: f.includes(b)
      }), /* @__PURE__ */ t.createElement("span", null, u.text))
    };
    return n.trim() ? typeof p == "function" ? p(n, u) ? i : null : ee(n, u.text) ? i : null : i;
  });
}
function I(s) {
  return s || [];
}
function qe(s) {
  var l, c;
  const {
    tablePrefixCls: f,
    prefixCls: a,
    column: n,
    dropdownPrefixCls: p,
    columnKey: u,
    filterMultiple: m,
    filterMode: b = "menu",
    filterSearch: g = !1,
    filterState: i,
    triggerFilter: ne,
    locale: h,
    children: re,
    getPopupContainer: M,
    rootClassName: le
  } = s, {
    filterDropdownOpen: O,
    onFilterDropdownOpenChange: P,
    filterResetToDefaultFilteredValue: R,
    defaultFilteredValue: T,
    // Deprecated
    filterDropdownVisible: _,
    onFilterDropdownVisibleChange: K
  } = n, [k, oe] = t.useState(!1), L = !!(i && (!((l = i.filteredKeys) === null || l === void 0) && l.length || i.forceFiltered)), w = (e) => {
    oe(e), P == null || P(e), K == null || K(e);
  };
  if (process.env.NODE_ENV !== "production") {
    const e = Ce("Table");
    [["filterDropdownVisible", "filterDropdownOpen", _], ["onFilterDropdownVisibleChange", "onFilterDropdownOpenChange", K]].forEach((r) => {
      let [o, d, N] = r;
      e.deprecated(N == null, o, d);
    });
  }
  const A = (c = O ?? _) !== null && c !== void 0 ? c : k, S = i == null ? void 0 : i.filteredKeys, [D, y] = ye(I(S)), C = (e) => {
    let {
      selectedKeys: r
    } = e;
    y(r);
  }, ie = (e, r) => {
    let {
      node: o,
      checked: d
    } = r;
    C(m ? {
      selectedKeys: e
    } : {
      selectedKeys: d && o.key ? [o.key] : []
    });
  };
  t.useEffect(() => {
    k && C({
      selectedKeys: I(S)
    });
  }, [S]);
  const [se, ae] = t.useState([]), ce = (e) => {
    ae(e);
  }, [v, $] = t.useState(""), j = (e) => {
    const {
      value: r
    } = e.target;
    $(r);
  };
  t.useEffect(() => {
    k || $("");
  }, [k]);
  const V = (e) => {
    const r = e && e.length ? e : null;
    if (r === null && (!i || !i.filteredKeys) || J(r, i == null ? void 0 : i.filteredKeys, !0))
      return null;
    ne({
      column: n,
      key: u,
      filteredKeys: r
    });
  }, z = () => {
    w(!1), V(D());
  }, W = function() {
    let {
      confirm: e,
      closeDropdown: r
    } = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {
      confirm: !1,
      closeDropdown: !1
    };
    e && V([]), r && w(!1), $(""), y(R ? (T || []).map((o) => String(o)) : []);
  }, fe = function() {
    let {
      closeDropdown: e
    } = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {
      closeDropdown: !0
    };
    e && w(!1), V(D());
  }, ue = (e, r) => {
    r.source === "trigger" && (e && S !== void 0 && y(I(S)), w(e), !e && !n.filterDropdown && z());
  }, de = H({
    [`${p}-menu-without-submenu`]: !ke(n.filters || [])
  }), me = (e) => {
    if (e.target.checked) {
      const r = x(n == null ? void 0 : n.filters).map((o) => String(o));
      y(r);
    } else
      y([]);
  }, q = (e) => {
    let {
      filters: r
    } = e;
    return (r || []).map((o, d) => {
      const N = String(o.value), G = {
        title: o.text,
        key: o.value !== void 0 ? N : String(d)
      };
      return o.children && (G.children = q({
        filters: o.children
      })), G;
    });
  }, B = (e) => {
    var r;
    return Object.assign(Object.assign({}, e), {
      text: e.title,
      value: e.key,
      children: ((r = e.children) === null || r === void 0 ? void 0 : r.map((o) => B(o))) || []
    });
  };
  let E;
  if (typeof n.filterDropdown == "function")
    E = n.filterDropdown({
      prefixCls: `${p}-custom`,
      setSelectedKeys: (e) => C({
        selectedKeys: e
      }),
      selectedKeys: D(),
      confirm: fe,
      clearFilters: W,
      filters: n.filters,
      visible: A,
      close: () => {
        w(!1);
      }
    });
  else if (n.filterDropdown)
    E = n.filterDropdown;
  else {
    const e = D() || [], r = () => (n.filters || []).length === 0 ? /* @__PURE__ */ t.createElement(X, {
      image: X.PRESENTED_IMAGE_SIMPLE,
      description: h.filterEmptyText,
      imageStyle: {
        height: 24
      },
      style: {
        margin: 0,
        padding: "16px 0"
      }
    }) : b === "tree" ? /* @__PURE__ */ t.createElement(t.Fragment, null, /* @__PURE__ */ t.createElement(Y, {
      filterSearch: g,
      value: v,
      onChange: j,
      tablePrefixCls: f,
      locale: h
    }), /* @__PURE__ */ t.createElement("div", {
      className: `${f}-filter-dropdown-tree`
    }, m ? /* @__PURE__ */ t.createElement(Z, {
      checked: e.length === x(n.filters).length,
      indeterminate: e.length > 0 && e.length < x(n.filters).length,
      className: `${f}-filter-dropdown-checkall`,
      onChange: me
    }, h.filterCheckall) : null, /* @__PURE__ */ t.createElement(De, {
      checkable: !0,
      selectable: !1,
      blockNode: !0,
      multiple: m,
      checkStrictly: !m,
      className: `${p}-menu`,
      onCheck: ie,
      checkedKeys: e,
      selectedKeys: e,
      showIcon: !1,
      treeData: q({
        filters: n.filters
      }),
      autoExpandParent: !0,
      defaultExpandAll: !0,
      filterTreeNode: v.trim() ? (d) => typeof g == "function" ? g(v, B(d)) : ee(v, d.title) : void 0
    }))) : /* @__PURE__ */ t.createElement(t.Fragment, null, /* @__PURE__ */ t.createElement(Y, {
      filterSearch: g,
      value: v,
      onChange: j,
      tablePrefixCls: f,
      locale: h
    }), /* @__PURE__ */ t.createElement(be, {
      selectable: !0,
      multiple: m,
      prefixCls: `${p}-menu`,
      className: de,
      onSelect: C,
      onDeselect: C,
      selectedKeys: e,
      getPopupContainer: M,
      openKeys: se,
      onOpenChange: ce,
      items: te({
        filters: n.filters || [],
        filterSearch: g,
        prefixCls: a,
        filteredKeys: D(),
        filterMultiple: m,
        searchValue: v
      })
    })), o = () => R ? J((T || []).map((d) => String(d)), e, !0) : e.length === 0;
    E = /* @__PURE__ */ t.createElement(t.Fragment, null, r(), /* @__PURE__ */ t.createElement("div", {
      className: `${a}-dropdown-btns`
    }, /* @__PURE__ */ t.createElement(Q, {
      type: "link",
      size: "small",
      disabled: o(),
      onClick: () => W()
    }, h.filterReset), /* @__PURE__ */ t.createElement(Q, {
      type: "primary",
      size: "small",
      onClick: z
    }, h.filterConfirm)));
  }
  n.filterDropdown && (E = /* @__PURE__ */ t.createElement(we, {
    selectable: void 0
  }, E));
  const pe = () => /* @__PURE__ */ t.createElement(Ke, {
    className: `${a}-dropdown`
  }, E);
  let F;
  typeof n.filterIcon == "function" ? F = n.filterIcon(L) : n.filterIcon ? F = n.filterIcon : F = /* @__PURE__ */ t.createElement(he, null);
  const {
    direction: ge
  } = t.useContext(ve);
  return /* @__PURE__ */ t.createElement("div", {
    className: `${a}-column`
  }, /* @__PURE__ */ t.createElement("span", {
    className: `${f}-column-title`
  }, re), /* @__PURE__ */ t.createElement(Ee, {
    dropdownRender: pe,
    trigger: ["click"],
    open: A,
    onOpenChange: ue,
    getPopupContainer: M,
    placement: ge === "rtl" ? "bottomLeft" : "bottomRight",
    rootClassName: le
  }, /* @__PURE__ */ t.createElement("span", {
    role: "button",
    tabIndex: -1,
    className: H(`${a}-trigger`, {
      active: L
    }),
    onClick: (e) => {
      e.stopPropagation();
    }
  }, F)));
}
export {
  qe as default,
  x as flattenKeys
};
