import d from "../../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import * as a from "react";
import pe from "../../../../classnames/index.mjs";
import "../../../../rc-field-form/es/index.mjs";
import ge from "../../../../rc-util/es/hooks/useState.mjs";
import { supportRef as he } from "../../../../rc-util/es/ref.mjs";
import { isValidElement as ye, cloneElement as ve } from "../../_util/reactNode.mjs";
import { devUseWarning as Ce } from "../../_util/warning.mjs";
import "../../config-provider/index.mjs";
import Ee from "../../config-provider/hooks/useCSSVarCls.mjs";
import { FormContext as be, NoStyleItemContext as Ne } from "../context.mjs";
import Ie from "../hooks/useChildren.mjs";
import Se from "../hooks/useFormItemStatus.mjs";
import we from "../hooks/useFrameState.mjs";
import Oe from "../hooks/useItemRef.mjs";
import Ve from "../style/index.mjs";
import { toArray as D, getFieldId as Pe } from "../util.mjs";
import Fe from "./ItemHolder.mjs";
import _e from "./StatusProvider.mjs";
import { ConfigContext as xe } from "../../config-provider/context.mjs";
import je from "../../../../rc-field-form/es/FieldContext.mjs";
import Ae from "../../../../rc-field-form/es/ListContext.mjs";
import Re from "../../../../rc-field-form/es/Field.mjs";
const De = "__SPLIT__";
function qe(r, o) {
  const u = Object.keys(r), v = Object.keys(o);
  return u.length === v.length && u.every((l) => {
    const I = r[l], f = o[l];
    return I === f || typeof I == "function" || typeof f == "function";
  });
}
const Te = /* @__PURE__ */ a.memo((r) => {
  let {
    children: o
  } = r;
  return o;
}, (r, o) => qe(r.control, o.control) && r.update === o.update && r.childProps.length === o.childProps.length && r.childProps.every((u, v) => u === o.childProps[v]));
function Z() {
  return {
    errors: [],
    warnings: [],
    touched: !1,
    validating: !1,
    name: [],
    validated: !1
  };
}
function Le(r) {
  const {
    name: o,
    noStyle: u,
    className: v,
    dependencies: l,
    prefixCls: I,
    shouldUpdate: f,
    rules: q,
    children: te,
    required: T,
    label: L,
    messageVariables: k,
    trigger: K = "onChange",
    validateTrigger: U,
    hidden: re,
    help: F
  } = r, {
    getPrefixCls: oe
  } = a.useContext(xe), {
    name: ne
  } = a.useContext(be), s = Ie(te), S = typeof s == "function", M = a.useContext(Ne), {
    validateTrigger: se
  } = a.useContext(je), W = U !== void 0 ? U : se, h = o != null, w = oe("form", I), B = Ee(w), [$, ie, ae] = Ve(w, B), m = Ce("Form.Item");
  process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "production" && m(o !== null, "usage", "`null` is passed as `name` property");
  const _ = a.useContext(Ae), z = a.useRef(), [H, le] = we({}), [y, ce] = ge(() => Z()), de = (e) => {
    const i = _ == null ? void 0 : _.getKey(e.name);
    if (ce(e.destroy ? Z() : e, !0), u && F !== !1 && M) {
      let n = e.name;
      if (e.destroy)
        n = z.current || n;
      else if (i !== void 0) {
        const [c, p] = i;
        n = [c].concat(d(p)), z.current = n;
      }
      M(e, n);
    }
  }, me = (e, i) => {
    le((n) => {
      const c = Object.assign({}, n), b = [].concat(d(e.name.slice(0, -1)), d(i)).join(De);
      return e.destroy ? delete c[b] : c[b] = e, c;
    });
  }, [C, x] = a.useMemo(() => {
    const e = d(y.errors), i = d(y.warnings);
    return Object.values(H).forEach((n) => {
      e.push.apply(e, d(n.errors || [])), i.push.apply(i, d(n.warnings || []));
    }), [e, i];
  }, [H, y.errors, y.warnings]), ue = Oe();
  function Y(e, i, n) {
    return u && !re ? /* @__PURE__ */ a.createElement(_e, {
      prefixCls: w,
      hasFeedback: r.hasFeedback,
      validateStatus: r.validateStatus,
      meta: y,
      errors: C,
      warnings: x,
      noStyle: !0
    }, e) : /* @__PURE__ */ a.createElement(Fe, Object.assign({
      key: "row"
    }, r, {
      className: pe(v, ae, B, ie),
      prefixCls: w,
      fieldId: i,
      isRequired: n,
      errors: C,
      warnings: x,
      meta: y,
      onSubItemMetaChange: me
    }), e);
  }
  if (!h && !S && !l)
    return $(Y(s));
  let E = {};
  return typeof L == "string" ? E.label = L : o && (E.label = String(o)), k && (E = Object.assign(Object.assign({}, E), k)), $(/* @__PURE__ */ a.createElement(Re, Object.assign({}, r, {
    messageVariables: E,
    trigger: K,
    validateTrigger: W,
    onMetaChange: de
  }), (e, i, n) => {
    const c = D(o).length && i ? i.name : [], p = Pe(c, ne), b = T !== void 0 ? T : !!(q && q.some((t) => {
      if (t && typeof t == "object" && t.required && !t.warningOnly)
        return !0;
      if (typeof t == "function") {
        const V = t(n);
        return V && V.required && !V.warningOnly;
      }
      return !1;
    })), O = Object.assign({}, e);
    let N = null;
    if (process.env.NODE_ENV !== "production" && m(!(f && l), "usage", "`shouldUpdate` and `dependencies` shouldn't be used together. See https://u.ant.design/form-deps."), Array.isArray(s) && h)
      process.env.NODE_ENV !== "production" && m(!1, "usage", "A `Form.Item` with a `name` prop must have a single child element. For information on how to render more complex form items, see https://u.ant.design/complex-form-item."), N = s;
    else if (S && (!(f || l) || h))
      process.env.NODE_ENV !== "production" && m(!!(f || l), "usage", "A `Form.Item` with a render function must have either `shouldUpdate` or `dependencies`."), process.env.NODE_ENV !== "production" && m(!h, "usage", "A `Form.Item` with a render function cannot be a field, and thus cannot have a `name` prop.");
    else if (l && !S && !h)
      process.env.NODE_ENV !== "production" && m(!1, "usage", "Must set `name` or use a render function when `dependencies` is set.");
    else if (ye(s)) {
      process.env.NODE_ENV !== "production" && m(s.props.defaultValue === void 0, "usage", "`defaultValue` will not work on controlled Field. You should use `initialValues` of Form instead.");
      const t = Object.assign(Object.assign({}, s.props), O);
      if (t.id || (t.id = p), F || C.length > 0 || x.length > 0 || r.extra) {
        const g = [];
        (F || C.length > 0) && g.push(`${p}_help`), r.extra && g.push(`${p}_extra`), t["aria-describedby"] = g.join(" ");
      }
      C.length > 0 && (t["aria-invalid"] = "true"), b && (t["aria-required"] = "true"), he(s) && (t.ref = ue(c, s)), new Set([].concat(d(D(K)), d(D(W)))).forEach((g) => {
        t[g] = function() {
          for (var G, J, j, Q, A, X = arguments.length, R = new Array(X), P = 0; P < X; P++)
            R[P] = arguments[P];
          (j = O[g]) === null || j === void 0 || (G = j).call.apply(G, [O].concat(R)), (A = (Q = s.props)[g]) === null || A === void 0 || (J = A).call.apply(J, [Q].concat(R));
        };
      });
      const fe = [t["aria-required"], t["aria-invalid"], t["aria-describedby"]];
      N = /* @__PURE__ */ a.createElement(Te, {
        control: O,
        update: s,
        childProps: fe
      }, ve(s, t));
    } else
      S && (f || l) && !h ? N = s(n) : (process.env.NODE_ENV !== "production" && m(!c.length, "usage", "`name` is only used for validate React element. If you are using Form.Item as layout display, please remove `name` instead."), N = s);
    return Y(N, p, b);
  }));
}
const ee = Le;
ee.useStatus = Se;
const at = ee;
export {
  at as default
};
