import { isFunction as v, cloneDeep as w } from "lodash-unified";
import { ref as F, computed as M, unref as l } from "vue";
function T(o, a) {
  return !a?.length || v(o) ? [] : a.map((i) => ({ [o]: i }));
}
const m = (o) => {
  const a = F(
    T(o.rowKey, o.defaultSelectedRowKeys)
  );
  function i(e, t) {
    a.value = t;
  }
  const u = (e) => v(o.rowKey) ? o.rowKey(e) : e[o.rowKey], s = M(() => {
    const e = /* @__PURE__ */ new Map();
    return l(a).forEach((t) => {
      e.set(u(t), t);
    }), e;
  });
  function h(e) {
    return l(s).has(u(e));
  }
  function g(e) {
    if (!f(e))
      return;
    const t = l(s);
    t.set(u(e), e), a.value = [...t.values()];
  }
  function x(e) {
    const t = l(s);
    for (const n of e)
      f(n) && t.set(u(n), n);
    a.value = [...t.values()];
  }
  function y(e) {
    if (v(o.rowKey))
      return;
    const t = l(s);
    for (const n of e) {
      const c = l(o.dataSource).find(
        (d) => u(d) === n
      ), r = { [o.rowKey]: n };
      f(c || r) && t.set(n, c || r);
    }
    a.value = [...t.values()];
  }
  function K(e) {
    if (!f(e))
      return;
    const t = l(s);
    t.delete(u(e)), a.value = [...t.values()];
  }
  function R(e) {
    const t = l(s);
    for (const n of e)
      f(n) && t.delete(u(n));
    a.value = [...t.values()];
  }
  function S(e, t) {
    const n = e;
    for (const c of t)
      n.set(u(c), c);
    return n;
  }
  function E(e, t, n) {
    const c = l(s);
    e ? S(c, [...n, ...t]) : l(o.dataSource).forEach((r) => {
      const d = u(r);
      t.some((B) => u(B) === d) ? c.set(d, r) : c.delete(d);
    }), a.value = [...c.values()];
  }
  function b(e, t) {
    const n = w(l(s));
    e ? S(n, t) : l(o.dataSource).forEach((c) => {
      f(c) && n.delete(u(c));
    }), a.value = [...n.values()];
  }
  function f(e) {
    const t = o.shown, n = o.disabled, c = t ? t(e) : !0, r = n ? n(e) : !1;
    return c && !r;
  }
  function A(e, t) {
    const n = w(l(s));
    l(o.dataSource).forEach((c) => {
      e.find((d) => u(d) === u(c)) ? n.set(u(c), c) : n.delete(u(c));
    }), S(n, t), a.value = [...n.values()];
  }
  function C() {
    const e = l(s);
    l(o.dataSource).forEach((t) => {
      f(t) && e.set(u(t), t);
    }), a.value = [...e.values()];
  }
  function D(e) {
    if (!f(e))
      return;
    const t = l(s), n = u(e);
    t.has(n) ? t.delete(n) : t.set(n, e), a.value = [...t.values()];
  }
  function I() {
    const e = l(s);
    l(o.dataSource).forEach((t) => {
      f(t) && e.delete(u(t));
    }), a.value = [...e.values()];
  }
  function k() {
    a.value = [];
  }
  return {
    select: g,
    unSelect: K,
    isSelected: h,
    selectAll: C,
    unSelectAll: I,
    clearAll: k,
    toggleSelect: D,
    selectedRows: a,
    selectMulti: x,
    unSelectMulti: R,
    rowSelection: M(() => ({
      ...o,
      onChange: i,
      selectedRowKeys: l(a).map((e) => u(e))
    })),
    selectChange: E,
    selectAllChange: b,
    selectRangeChange: A,
    selectMultiByKeys: y
  };
};
export {
  m as default
};
