import K from "../../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import G from "../../../../@ant-design/icons/es/icons/CaretDownOutlined.mjs";
import J from "../../../../@ant-design/icons/es/icons/CaretUpOutlined.mjs";
import E from "../../../../classnames/index.mjs";
import L from "../../../../rc-util/es/KeyCode.mjs";
import * as y from "react";
import Q from "../../tooltip/index.mjs";
import { getColumnKey as I, renderColumnTitle as V, safeColumnTitle as W, getColumnPos as H } from "../util.mjs";
const k = "ascend", $ = "descend";
function v(e) {
  return typeof e.sorter == "object" && typeof e.sorter.multiple == "number" ? e.sorter.multiple : !1;
}
function R(e) {
  return typeof e == "function" ? e : e && typeof e == "object" && e.compare ? e.compare : !1;
}
function X(e, r) {
  return r ? e[e.indexOf(r) + 1] : e[0];
}
function M(e, r, a) {
  let u = [];
  function O(t, l) {
    u.push({
      column: t,
      key: I(t, l),
      multiplePriority: v(t),
      sortOrder: t.sortOrder
    });
  }
  return (e || []).forEach((t, l) => {
    const c = H(l, a);
    t.children ? ("sortOrder" in t && O(t, c), u = [].concat(K(u), K(M(t.children, r, c)))) : t.sorter && ("sortOrder" in t ? O(t, c) : r && t.defaultSortOrder && u.push({
      column: t,
      key: I(t, c),
      multiplePriority: v(t),
      sortOrder: t.defaultSortOrder
    }));
  }), u;
}
function U(e, r, a, u, O, t, l, c) {
  return (r || []).map((o, m) => {
    const P = H(m, c);
    let s = o;
    if (s.sorter) {
      const b = s.sortDirections || O, S = s.showSorterTooltip === void 0 ? l : s.showSorterTooltip, n = I(s, P), f = a.find((h) => {
        let {
          key: i
        } = h;
        return i === n;
      }), d = f ? f.sortOrder : null, p = X(b, d);
      let j;
      if (o.sortIcon)
        j = o.sortIcon({
          sortOrder: d
        });
      else {
        const h = b.includes(k) && /* @__PURE__ */ y.createElement(J, {
          className: E(`${e}-column-sorter-up`, {
            active: d === k
          })
        }), i = b.includes($) && /* @__PURE__ */ y.createElement(G, {
          className: E(`${e}-column-sorter-down`, {
            active: d === $
          })
        });
        j = /* @__PURE__ */ y.createElement("span", {
          className: E(`${e}-column-sorter`, {
            [`${e}-column-sorter-full`]: !!(h && i)
          })
        }, /* @__PURE__ */ y.createElement("span", {
          className: `${e}-column-sorter-inner`,
          "aria-hidden": "true"
        }, h, i));
      }
      const {
        cancelSort: g,
        triggerAsc: _,
        triggerDesc: q
      } = t || {};
      let N = g;
      p === $ ? N = q : p === k && (N = _);
      const z = typeof S == "object" ? Object.assign({
        title: N
      }, S) : {
        title: N
      };
      s = Object.assign(Object.assign({}, s), {
        className: E(s.className, {
          [`${e}-column-sort`]: d
        }),
        title: (h) => {
          const i = /* @__PURE__ */ y.createElement("div", {
            className: `${e}-column-sorters`
          }, /* @__PURE__ */ y.createElement("span", {
            className: `${e}-column-title`
          }, V(o.title, h)), j);
          return S ? /* @__PURE__ */ y.createElement(Q, Object.assign({}, z), i) : i;
        },
        onHeaderCell: (h) => {
          const i = o.onHeaderCell && o.onHeaderCell(h) || {}, w = i.onClick, D = i.onKeyDown;
          i.onClick = (T) => {
            u({
              column: o,
              key: n,
              sortOrder: p,
              multiplePriority: v(o)
            }), w == null || w(T);
          }, i.onKeyDown = (T) => {
            T.keyCode === L.ENTER && (u({
              column: o,
              key: n,
              sortOrder: p,
              multiplePriority: v(o)
            }), D == null || D(T));
          };
          const C = W(o.title, {}), B = C == null ? void 0 : C.toString();
          return d ? i["aria-sort"] = d === "ascend" ? "ascending" : "descending" : i["aria-label"] = B || "", i.className = E(i.className, `${e}-column-has-sorters`), i.tabIndex = 0, o.ellipsis && (i.title = (C ?? "").toString()), i;
        }
      });
    }
    return "children" in s && (s = Object.assign(Object.assign({}, s), {
      children: U(e, s.children, a, u, O, t, l, P)
    })), s;
  });
}
function A(e) {
  const {
    column: r,
    sortOrder: a
  } = e;
  return {
    column: r,
    order: a,
    field: r.dataIndex,
    columnKey: r.key
  };
}
function F(e) {
  const r = e.filter((a) => {
    let {
      sortOrder: u
    } = a;
    return u;
  }).map(A);
  return r.length === 0 && e.length ? Object.assign(Object.assign({}, A(e[e.length - 1])), {
    column: void 0
  }) : r.length <= 1 ? r[0] || {} : r;
}
function Y(e, r, a) {
  const u = r.slice().sort((l, c) => c.multiplePriority - l.multiplePriority), O = e.slice(), t = u.filter((l) => {
    let {
      column: {
        sorter: c
      },
      sortOrder: o
    } = l;
    return R(c) && o;
  });
  return t.length ? O.sort((l, c) => {
    for (let o = 0; o < t.length; o += 1) {
      const m = t[o], {
        column: {
          sorter: P
        },
        sortOrder: s
      } = m, b = R(P);
      if (b && s) {
        const S = b(l, c, s);
        if (S !== 0)
          return s === k ? S : -S;
      }
    }
    return 0;
  }).map((l) => {
    const c = l[a];
    return c ? Object.assign(Object.assign({}, l), {
      [a]: Y(c, r, a)
    }) : l;
  }) : O;
}
function se(e) {
  let {
    prefixCls: r,
    mergedColumns: a,
    onSorterChange: u,
    sortDirections: O,
    tableLocale: t,
    showSorterTooltip: l
  } = e;
  const [c, o] = y.useState(M(a, !0)), m = y.useMemo(() => {
    let n = !0;
    const f = M(a, !1);
    if (!f.length)
      return c;
    const d = [];
    function p(g) {
      n ? d.push(g) : d.push(Object.assign(Object.assign({}, g), {
        sortOrder: null
      }));
    }
    let j = null;
    return f.forEach((g) => {
      j === null ? (p(g), g.sortOrder && (g.multiplePriority === !1 ? n = !1 : j = !0)) : (j && g.multiplePriority !== !1 || (n = !1), p(g));
    }), d;
  }, [a, c]), P = y.useMemo(() => {
    const n = m.map((f) => {
      let {
        column: d,
        sortOrder: p
      } = f;
      return {
        column: d,
        order: p
      };
    });
    return {
      sortColumns: n,
      // Legacy
      sortColumn: n[0] && n[0].column,
      sortOrder: n[0] && n[0].order
    };
  }, [m]);
  function s(n) {
    let f;
    n.multiplePriority === !1 || !m.length || m[0].multiplePriority === !1 ? f = [n] : f = [].concat(K(m.filter((d) => {
      let {
        key: p
      } = d;
      return p !== n.key;
    })), [n]), o(f), u(F(f), f);
  }
  return [(n) => U(r, n, m, s, O, t, l), m, P, () => F(m)];
}
export {
  se as default,
  Y as getSortData
};
