import C from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import i from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import * as n from "react";
import { warningOnce as k } from "../../rc-util/es/warning.mjs";
import L from "./FieldContext.mjs";
import j from "./Field.mjs";
import { getNamePath as w, move as F } from "./utils/valueUtil.mjs";
import E from "./ListContext.mjs";
function G(u) {
  var N = u.name, A = u.initialValue, V = u.children, b = u.rules, M = u.validateTrigger, d = u.isListField, f = n.useContext(L), T = n.useContext(E), x = n.useRef({
    keys: [],
    id: 0
  }), t = x.current, s = n.useMemo(function() {
    var l = w(f.prefixName) || [];
    return [].concat(i(l), i(w(N)));
  }, [f.prefixName, N]), O = n.useMemo(function() {
    return C(C({}, f), {}, {
      prefixName: s
    });
  }, [f, s]), P = n.useMemo(function() {
    return {
      getKey: function(c) {
        var o = s.length, m = c[o];
        return [t.keys[m], c.slice(o + 1)];
      }
    };
  }, [s]);
  if (typeof V != "function")
    return k(!1, "Form.List only accepts function as children."), null;
  var R = function(c, o, m) {
    var v = m.source;
    return v === "internal" ? !1 : c !== o;
  };
  return /* @__PURE__ */ n.createElement(E.Provider, {
    value: P
  }, /* @__PURE__ */ n.createElement(L.Provider, {
    value: O
  }, /* @__PURE__ */ n.createElement(j, {
    name: [],
    shouldUpdate: R,
    rules: b,
    validateTrigger: M,
    initialValue: A,
    isList: !0,
    isListField: d ?? !!T
  }, function(l, c) {
    var o = l.value, m = o === void 0 ? [] : o, v = l.onChange, S = f.getFieldValue, y = function() {
      var r = S(s || []);
      return r || [];
    }, U = {
      add: function(r, e) {
        var a = y();
        e >= 0 && e <= a.length ? (t.keys = [].concat(i(t.keys.slice(0, e)), [t.id], i(t.keys.slice(e))), v([].concat(i(a.slice(0, e)), [r], i(a.slice(e))))) : (process.env.NODE_ENV !== "production" && (e < 0 || e > a.length) && k(!1, "The second parameter of the add function should be a valid positive number."), t.keys = [].concat(i(t.keys), [t.id]), v([].concat(i(a), [r]))), t.id += 1;
      },
      remove: function(r) {
        var e = y(), a = new Set(Array.isArray(r) ? r : [r]);
        a.size <= 0 || (t.keys = t.keys.filter(function(_, h) {
          return !a.has(h);
        }), v(e.filter(function(_, h) {
          return !a.has(h);
        })));
      },
      move: function(r, e) {
        if (r !== e) {
          var a = y();
          r < 0 || r >= a.length || e < 0 || e >= a.length || (t.keys = F(t.keys, r, e), v(F(a, r, e)));
        }
      }
    }, g = m || [];
    return Array.isArray(g) || (g = [], process.env.NODE_ENV !== "production" && k(!1, "Current value of '".concat(s.join(" > "), "' is not an array type."))), V(g.map(function(p, r) {
      var e = t.keys[r];
      return e === void 0 && (t.keys[r] = t.id, e = t.keys[r], t.id += 1), {
        name: r,
        key: e,
        isListField: !0
      };
    }), U, c);
  })));
}
export {
  G as default
};
