import w from "../../../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import * as S from "react";
import { devUseWarning as D } from "../../../_util/warning.mjs";
import { getColumnKey as b, getColumnPos as j, renderColumnTitle as E } from "../../util.mjs";
import I, { flattenKeys as V } from "./FilterDropdown.mjs";
function M(s, t, d) {
  let n = [];
  return (s || []).forEach((e, c) => {
    var r;
    const a = j(c, d);
    if (e.filters || "filterDropdown" in e || "onFilter" in e)
      if ("filteredValue" in e) {
        let l = e.filteredValue;
        "filterDropdown" in e || (l = (r = l == null ? void 0 : l.map(String)) !== null && r !== void 0 ? r : l), n.push({
          column: e,
          key: b(e, a),
          filteredKeys: l,
          forceFiltered: e.filtered
        });
      } else
        n.push({
          column: e,
          key: b(e, a),
          filteredKeys: t && e.defaultFilteredValue ? e.defaultFilteredValue : void 0,
          forceFiltered: e.filtered
        });
    "children" in e && (n = [].concat(w(n), w(M(e.children, t, a))));
  }), n;
}
function v(s, t, d, n, e, c, r, a, l) {
  return d.map((i, u) => {
    const C = j(u, a), {
      filterMultiple: g = !0,
      filterMode: k,
      filterSearch: K
    } = i;
    let o = i;
    if (o.filters || o.filterDropdown) {
      const f = b(o, C), y = n.find((m) => {
        let {
          key: h
        } = m;
        return f === h;
      });
      o = Object.assign(Object.assign({}, o), {
        title: (m) => /* @__PURE__ */ S.createElement(I, {
          tablePrefixCls: s,
          prefixCls: `${s}-filter`,
          dropdownPrefixCls: t,
          column: o,
          columnKey: f,
          filterState: y,
          filterMultiple: g,
          filterMode: k,
          filterSearch: K,
          triggerFilter: c,
          locale: e,
          getPopupContainer: r,
          rootClassName: l
        }, E(i.title, m))
      });
    }
    return "children" in o && (o = Object.assign(Object.assign({}, o), {
      children: v(s, t, o.children, n, e, c, r, C, l)
    })), o;
  });
}
function O(s) {
  const t = {};
  return s.forEach((d) => {
    let {
      key: n,
      filteredKeys: e,
      column: c
    } = d;
    const r = n, {
      filters: a,
      filterDropdown: l
    } = c;
    if (l)
      t[r] = e || null;
    else if (Array.isArray(e)) {
      const i = V(a);
      t[r] = i.filter((u) => e.includes(String(u)));
    } else
      t[r] = null;
  }), t;
}
function T(s, t) {
  return t.reduce((d, n) => {
    const {
      column: {
        onFilter: e,
        filters: c
      },
      filteredKeys: r
    } = n;
    return e && r && r.length ? d.filter((a) => r.some((l) => {
      const i = V(c), u = i.findIndex((g) => String(g) === String(l)), C = u !== -1 ? i[u] : l;
      return e(C, a);
    })) : d;
  }, s);
}
const x = (s) => s.flatMap((t) => "children" in t ? [t].concat(w(x(t.children || []))) : [t]);
function R(s) {
  let {
    prefixCls: t,
    dropdownPrefixCls: d,
    mergedColumns: n,
    onFilterChange: e,
    getPopupContainer: c,
    locale: r,
    rootClassName: a
  } = s;
  const l = D("Table"), i = S.useMemo(() => x(n || []), [n]), [u, C] = S.useState(() => M(i, !0)), g = S.useMemo(() => {
    const f = M(i, !1);
    if (f.length === 0)
      return f;
    let y = !0, m = !0;
    if (f.forEach((h) => {
      let {
        filteredKeys: p
      } = h;
      p !== void 0 ? y = !1 : m = !1;
    }), y) {
      const h = (i || []).map((p, F) => b(p, j(F)));
      return u.filter((p) => {
        let {
          key: F
        } = p;
        return h.includes(F);
      }).map((p) => {
        const F = i[h.findIndex((A) => A === p.key)];
        return Object.assign(Object.assign({}, p), {
          column: Object.assign(Object.assign({}, p.column), F),
          forceFiltered: F.filtered
        });
      });
    }
    return process.env.NODE_ENV !== "production" && l(m, "usage", "Columns should all contain `filteredValue` or not contain `filteredValue`."), f;
  }, [i, u]), k = S.useMemo(() => O(g), [g]), K = (f) => {
    const y = g.filter((m) => {
      let {
        key: h
      } = m;
      return h !== f.key;
    });
    y.push(f), C(y), e(O(y), y);
  };
  return [(f) => v(t, d, f, g, r, K, c, void 0, a), g, k];
}
export {
  R as default,
  V as flattenKeys,
  T as getFilterData
};
