import $ from "../../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import p from "../../../../@babel/runtime/helpers/esm/defineProperty.mjs";
import P from "../../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import _ from "../../../../@babel/runtime/helpers/esm/typeof.mjs";
import v from "../../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import U from "../../../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import ee from "../../../../rc-util/es/Children/toArray.mjs";
import { warningOnce as h } from "../../../../rc-util/es/warning.mjs";
import * as x from "react";
import { EXPAND_COLUMN as f } from "../../constant.mjs";
import { INTERNAL_COL_DEFINE as re } from "../../utils/legacyUtil.mjs";
import ne from "./useWidthColumns.mjs";
var ie = ["children"], te = ["fixed"];
function V(e) {
  return ee(e).filter(function(n) {
    return /* @__PURE__ */ x.isValidElement(n);
  }).map(function(n) {
    var r = n.key, t = n.props, i = t.children, a = U(t, ie), c = v({
      key: r
    }, a);
    return i && (c.children = V(i)), c;
  });
}
function W(e) {
  var n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "key";
  return e.filter(function(r) {
    return r && _(r) === "object";
  }).reduce(function(r, t, i) {
    var a = t.fixed, c = a === !0 ? "left" : a, g = "".concat(n, "-").concat(i), s = t.children;
    return s && s.length > 0 ? [].concat(P(r), P(W(s, g).map(function(w) {
      return v({
        fixed: c
      }, w);
    }))) : [].concat(P(r), [v(v({
      key: g
    }, t), {}, {
      fixed: c
    })]);
  }, []);
}
function oe(e) {
  for (var n = !0, r = 0; r < e.length; r += 1) {
    var t = e[r];
    if (n && t.fixed !== "left")
      n = !1;
    else if (!n && t.fixed === "left") {
      h(!1, "Index ".concat(r - 1, " of `columns` missing `fixed='left'` prop."));
      break;
    }
  }
  for (var i = !0, a = e.length - 1; a >= 0; a -= 1) {
    var c = e[a];
    if (i && c.fixed !== "right")
      i = !1;
    else if (!i && c.fixed === "right") {
      h(!1, "Index ".concat(a + 1, " of `columns` missing `fixed='right'` prop."));
      break;
    }
  }
}
function ae(e) {
  return e.map(function(n) {
    var r = n.fixed, t = U(n, te), i = r;
    return r === "left" ? i = "right" : r === "right" && (i = "left"), v({
      fixed: i
    }, t);
  });
}
function Ee(e, n) {
  var r = e.prefixCls, t = e.columns, i = e.children, a = e.expandable, c = e.expandedKeys, g = e.columnTitle, s = e.getRowKey, w = e.onTriggerExpand, D = e.expandIcon, I = e.rowExpandable, E = e.expandIconColumnIndex, m = e.direction, X = e.expandRowByClick, j = e.columnWidth, N = e.fixed, T = e.scrollWidth, B = e.clientWidth, u = x.useMemo(function() {
    return t || V(i);
  }, [t, i]), A = x.useMemo(function() {
    if (a) {
      var o, l = u.slice();
      if (process.env.NODE_ENV !== "production" && E >= 0 && h(!1, "`expandIconColumnIndex` is deprecated. Please use `Table.EXPAND_COLUMN` in `columns` instead."), !l.includes(f)) {
        var C = E || 0;
        C >= 0 && l.splice(C, 0, f);
      }
      process.env.NODE_ENV !== "production" && l.filter(function(d) {
        return d === f;
      }).length > 1 && h(!1, "There exist more than one `EXPAND_COLUMN` in `columns`.");
      var L = l.indexOf(f);
      l = l.filter(function(d, F) {
        return d !== f || F === L;
      });
      var R = u[L], b;
      (N === "left" || N) && !E ? b = "left" : (N === "right" || N) && E === u.length ? b = "right" : b = R ? R.fixed : null;
      var G = (o = {}, p(o, re, {
        className: "".concat(r, "-expand-icon-col"),
        columnType: "EXPAND_COLUMN"
      }), p(o, "title", g), p(o, "fixed", b), p(o, "className", "".concat(r, "-row-expand-icon-cell")), p(o, "width", j), p(o, "render", function(F, k, H) {
        var J = s(k, H), Q = c.has(J), Y = I ? I(k) : !0, K = D({
          prefixCls: r,
          expanded: Q,
          expandable: Y,
          record: k,
          onExpand: w
        });
        return X ? /* @__PURE__ */ x.createElement("span", {
          onClick: function(Z) {
            return Z.stopPropagation();
          }
        }, K) : K;
      }), o);
      return l.map(function(d) {
        return d === f ? G : d;
      });
    }
    return process.env.NODE_ENV !== "production" && u.includes(f) && h(!1, "`expandable` is not config but there exist `EXPAND_COLUMN` in `columns`."), u.filter(function(d) {
      return d !== f;
    });
  }, [a, u, s, c, D, m]), y = x.useMemo(function() {
    var o = A;
    return n && (o = n(o)), o.length || (o = [{
      render: function() {
        return null;
      }
    }]), o;
  }, [n, A, m]), O = x.useMemo(function() {
    return m === "rtl" ? ae(W(y)) : W(y);
  }, [y, m, T]);
  process.env.NODE_ENV !== "production" && oe(m === "rtl" ? O.slice().reverse() : O);
  var S = ne(O, T, B), M = $(S, 2), q = M[0], z = M[1];
  return [y, q, z];
}
export {
  V as convertChildrenToColumns,
  Ee as default
};
