import O from "../../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import * as E from "react";
import { useCallback as X, useMemo as P } from "react";
import ze from "../../../../@ant-design/icons/es/icons/DownOutlined.mjs";
import Fe from "../../../../classnames/index.mjs";
import "../../../../rc-table/es/Footer/index.mjs";
import "../../../../rc-table/es/Table.mjs";
import { INTERNAL_COL_DEFINE as pe } from "../../../../rc-table/es/utils/legacyUtil.mjs";
import "../../../../rc-table/es/VirtualTable/index.mjs";
import { arrDel as He, arrAdd as Xe } from "../../../../rc-tree/es/util.mjs";
import { conductCheck as he } from "../../../../rc-tree/es/utils/conductUtil.mjs";
import { convertDataToEntities as Be } from "../../../../rc-tree/es/utils/treeUtil.mjs";
import Ye from "../../../../rc-util/es/hooks/useMergedState.mjs";
import qe from "../../_util/hooks/useMultipleSelect.mjs";
import { devUseWarning as Je } from "../../_util/warning.mjs";
import Oe from "../../checkbox/index.mjs";
import Qe from "../../dropdown/index.mjs";
import Ze from "../../radio/index.mjs";
const x = {}, Le = "SELECT_ALL", _e = "SELECT_INVERT", De = "SELECT_NONE", Pe = [], Ae = (j, m) => {
  let C = [];
  return (m || []).forEach((b) => {
    C.push(b), b && typeof b == "object" && j in b && (C = [].concat(O(C), O(Ae(j, b[j]))));
  }), C;
}, gt = (j, m) => {
  const {
    preserveSelectedRowKeys: C,
    selectedRowKeys: b,
    defaultSelectedRowKeys: Ie,
    getCheckboxProps: oe,
    onChange: B,
    onSelect: le,
    onSelectAll: ce,
    onSelectInvert: se,
    onSelectNone: re,
    onSelectMultiple: Y,
    columnWidth: ve,
    type: L,
    selections: U,
    fixed: we,
    renderCell: Ce,
    hideSelectAll: ge,
    checkStrictly: A = !0
  } = m || {}, {
    prefixCls: V,
    data: q,
    pageData: J,
    getRecordByKey: g,
    getRowKey: u,
    expandType: Ee,
    childrenColumnName: Q,
    locale: ie,
    getPopupContainer: ye
  } = j, $ = Je("Table"), [Me, Z] = qe((l) => l), [G, ae] = Ye(b || Ie || Pe, {
    value: b
  }), W = E.useRef(/* @__PURE__ */ new Map()), Se = X((l) => {
    if (C) {
      const n = /* @__PURE__ */ new Map();
      l.forEach((t) => {
        let e = g(t);
        !e && W.current.has(t) && (e = W.current.get(t)), n.set(t, e);
      }), W.current = n;
    }
  }, [g, C]);
  E.useEffect(() => {
    Se(G);
  }, [G]);
  const {
    keyEntities: ee
  } = P(() => {
    if (A)
      return {
        keyEntities: null
      };
    let l = q;
    if (C) {
      const n = new Set(q.map((e, c) => u(e, c))), t = Array.from(W.current).reduce((e, c) => {
        let [d, p] = c;
        return n.has(d) ? e : e.concat(p);
      }, []);
      l = [].concat(O(l), O(t));
    }
    return Be(l, {
      externalGetKey: u,
      childrenPropName: Q
    });
  }, [q, u, A, Q, C]), N = P(() => Ae(Q, J), [Q, J]), y = P(() => {
    const l = /* @__PURE__ */ new Map();
    return N.forEach((n, t) => {
      const e = u(n, t), c = (oe ? oe(n) : null) || {};
      l.set(e, c), process.env.NODE_ENV !== "production" && $(!("checked" in c || "defaultChecked" in c), "usage", "Do not set `checked` or `defaultChecked` in `getCheckboxProps`. Please use `selectedRowKeys` instead.");
    }), l;
  }, [N, u, oe]), z = X((l) => {
    var n;
    return !!(!((n = y.get(u(l))) === null || n === void 0) && n.disabled);
  }, [y, u]), [I, ke] = P(() => {
    if (A)
      return [G || [], []];
    const {
      checkedKeys: l,
      halfCheckedKeys: n
    } = he(G, !0, ee, z);
    return [l || [], n];
  }, [G, A, ee, z]), _ = P(() => {
    const l = L === "radio" ? I.slice(0, 1) : I;
    return new Set(l);
  }, [I, L]), xe = P(() => L === "radio" ? /* @__PURE__ */ new Set() : new Set(ke), [ke, L]);
  E.useEffect(() => {
    m || ae(Pe);
  }, [!!m]);
  const K = X((l, n) => {
    let t, e;
    Se(l), C ? (t = l, e = l.map((c) => W.current.get(c))) : (t = [], e = [], l.forEach((c) => {
      const d = g(c);
      d !== void 0 && (t.push(c), e.push(d));
    })), ae(t), B == null || B(t, e, {
      type: n
    });
  }, [ae, g, B, C]), te = X((l, n, t, e) => {
    if (le) {
      const c = t.map((d) => g(d));
      le(g(l), n, c, e);
    }
    K(t, "single");
  }, [le, g, K]), de = P(() => !U || ge ? null : (U === !0 ? [Le, _e, De] : U).map((n) => n === Le ? {
    key: "all",
    text: ie.selectionAll,
    onSelect() {
      K(q.map((t, e) => u(t, e)).filter((t) => {
        const e = y.get(t);
        return !(e != null && e.disabled) || _.has(t);
      }), "all");
    }
  } : n === _e ? {
    key: "invert",
    text: ie.selectInvert,
    onSelect() {
      const t = new Set(_);
      J.forEach((c, d) => {
        const p = u(c, d), F = y.get(p);
        F != null && F.disabled || (t.has(p) ? t.delete(p) : t.add(p));
      });
      const e = Array.from(t);
      se && ($.deprecated(!1, "onSelectInvert", "onChange"), se(e)), K(e, "invert");
    }
  } : n === De ? {
    key: "none",
    text: ie.selectNone,
    onSelect() {
      re == null || re(), K(Array.from(_).filter((t) => {
        const e = y.get(t);
        return e == null ? void 0 : e.disabled;
      }), "none");
    }
  } : n).map((n) => Object.assign(Object.assign({}, n), {
    onSelect: function() {
      for (var t, e, c = arguments.length, d = new Array(c), p = 0; p < c; p++)
        d[p] = arguments[p];
      (e = n.onSelect) === null || e === void 0 || (t = e).call.apply(t, [n].concat(d)), Z(null);
    }
  })), [U, _, J, u, se, K]);
  return [X((l) => {
    var n;
    if (!m)
      return process.env.NODE_ENV !== "production" && $(!l.includes(x), "usage", "`rowSelection` is not config but `SELECTION_COLUMN` exists in the `columns`."), l.filter((o) => o !== x);
    let t = O(l);
    const e = new Set(_), c = N.map(u).filter((o) => !y.get(o).disabled), d = c.every((o) => e.has(o)), p = c.some((o) => e.has(o)), F = () => {
      const o = [];
      d ? c.forEach((s) => {
        e.delete(s), o.push(s);
      }) : c.forEach((s) => {
        e.has(s) || (e.add(s), o.push(s));
      });
      const r = Array.from(e);
      ce == null || ce(!d, r.map((s) => g(s)), o.map((s) => g(s))), K(r, "all"), Z(null);
    };
    let be, ue;
    if (L !== "radio") {
      let o;
      if (de) {
        const i = {
          getPopupContainer: ye,
          items: de.map((h, v) => {
            const {
              key: S,
              text: H,
              onSelect: D
            } = h;
            return {
              key: S ?? v,
              onClick: () => {
                D == null || D(c);
              },
              label: H
            };
          })
        };
        o = /* @__PURE__ */ E.createElement("div", {
          className: `${V}-selection-extra`
        }, /* @__PURE__ */ E.createElement(Qe, {
          menu: i,
          getPopupContainer: ye
        }, /* @__PURE__ */ E.createElement("span", null, /* @__PURE__ */ E.createElement(ze, null))));
      }
      const r = N.map((i, h) => {
        const v = u(i, h), S = y.get(v) || {};
        return Object.assign({
          checked: e.has(v)
        }, S);
      }).filter((i) => {
        let {
          disabled: h
        } = i;
        return h;
      }), s = !!r.length && r.length === N.length, f = s && r.every((i) => {
        let {
          checked: h
        } = i;
        return h;
      }), a = s && r.some((i) => {
        let {
          checked: h
        } = i;
        return h;
      });
      ue = /* @__PURE__ */ E.createElement(Oe, {
        checked: s ? f : !!N.length && d,
        indeterminate: s ? !f && a : !d && p,
        onChange: F,
        disabled: N.length === 0 || s,
        "aria-label": o ? "Custom selection" : "Select all",
        skipGroup: !0
      }), be = !ge && /* @__PURE__ */ E.createElement("div", {
        className: `${V}-selection`
      }, ue, o);
    }
    let fe;
    L === "radio" ? fe = (o, r, s) => {
      const f = u(r, s), a = e.has(f);
      return {
        node: /* @__PURE__ */ E.createElement(Ze, Object.assign({}, y.get(f), {
          checked: a,
          onClick: (i) => i.stopPropagation(),
          onChange: (i) => {
            e.has(f) || te(f, !0, [f], i.nativeEvent);
          }
        })),
        checked: a
      };
    } : fe = (o, r, s) => {
      var f;
      const a = u(r, s), i = e.has(a), h = xe.has(a), v = y.get(a);
      let S;
      return Ee === "nest" ? (S = h, process.env.NODE_ENV !== "production" && $(typeof (v == null ? void 0 : v.indeterminate) != "boolean", "usage", "set `indeterminate` using `rowSelection.getCheckboxProps` is not allowed with tree structured dataSource.")) : S = (f = v == null ? void 0 : v.indeterminate) !== null && f !== void 0 ? f : h, {
        node: /* @__PURE__ */ E.createElement(Oe, Object.assign({}, v, {
          indeterminate: S,
          checked: i,
          skipGroup: !0,
          onClick: (H) => H.stopPropagation(),
          onChange: (H) => {
            let {
              nativeEvent: D
            } = H;
            const {
              shiftKey: $e
            } = D, Ne = c.findIndex((k) => k === a), Ge = I.some((k) => c.includes(k));
            if ($e && A && Ge) {
              const k = Me(Ne, c, e), M = Array.from(e);
              Y == null || Y(!i, M.map((R) => g(R)), k.map((R) => g(R))), K(M, "multiple");
            } else {
              const k = I;
              if (A) {
                const M = i ? He(k, a) : Xe(k, a);
                te(a, !i, M, D);
              } else {
                const M = he([].concat(O(k), [a]), !0, ee, z), {
                  checkedKeys: R,
                  halfCheckedKeys: We
                } = M;
                let Ke = R;
                if (i) {
                  const Te = new Set(R);
                  Te.delete(a), Ke = he(Array.from(Te), {
                    checked: !1,
                    halfCheckedKeys: We
                  }, ee, z).checkedKeys;
                }
                te(a, !i, Ke, D);
              }
            }
            Z(i ? null : Ne);
          }
        })),
        checked: i
      };
    };
    const Re = (o, r, s) => {
      const {
        node: f,
        checked: a
      } = fe(o, r, s);
      return Ce ? Ce(a, r, s, f) : f;
    };
    if (!t.includes(x))
      if (t.findIndex((o) => {
        var r;
        return ((r = o[pe]) === null || r === void 0 ? void 0 : r.columnType) === "EXPAND_COLUMN";
      }) === 0) {
        const [o, ...r] = t;
        t = [o, x].concat(O(r));
      } else
        t = [x].concat(O(t));
    const me = t.indexOf(x);
    process.env.NODE_ENV !== "production" && $(t.filter((o) => o === x).length <= 1, "usage", "Multiple `SELECTION_COLUMN` exist in `columns`."), t = t.filter((o, r) => o !== x || r === me);
    const T = t[me - 1], ne = t[me + 1];
    let w = we;
    w === void 0 && ((ne == null ? void 0 : ne.fixed) !== void 0 ? w = ne.fixed : (T == null ? void 0 : T.fixed) !== void 0 && (w = T.fixed)), w && T && ((n = T[pe]) === null || n === void 0 ? void 0 : n.columnType) === "EXPAND_COLUMN" && T.fixed === void 0 && (T.fixed = w);
    const je = Fe(`${V}-selection-col`, {
      [`${V}-selection-col-with-dropdown`]: U && L === "checkbox"
    }), Ue = () => m != null && m.columnTitle ? typeof m.columnTitle == "function" ? m.columnTitle(ue) : m.columnTitle : be, Ve = {
      fixed: w,
      width: ve,
      className: `${V}-selection-column`,
      title: Ue(),
      render: Re,
      onCell: m.onCell,
      [pe]: {
        className: je
      }
    };
    return t.map((o) => o === x ? Ve : o);
  }, [u, N, m, I, _, xe, ve, de, Ee, y, Y, te, z]), _];
};
export {
  Le as SELECTION_ALL,
  x as SELECTION_COLUMN,
  _e as SELECTION_INVERT,
  De as SELECTION_NONE,
  gt as default
};
