import J from "../../../classnames/index.mjs";
import K from "../../../rc-field-form/es/index.mjs";
import * as t from "react";
import { useMemo as P } from "react";
import "../config-provider/index.mjs";
import Q, { DisabledContextProvider as U } from "../config-provider/DisabledContext.mjs";
import X from "../config-provider/SizeContext.mjs";
import Y from "../config-provider/hooks/useSize.mjs";
import { FormProvider as Z, FormContext as ee } from "./context.mjs";
import re from "./hooks/useForm.mjs";
import oe from "./hooks/useFormWarning.mjs";
import te from "./style/index.mjs";
import le from "./validateMessagesContext.mjs";
import ie from "../config-provider/hooks/useCSSVarCls.mjs";
import { ConfigContext as se } from "../config-provider/context.mjs";
var ae = globalThis && globalThis.__rest || function(o, c) {
  var n = {};
  for (var r in o)
    Object.prototype.hasOwnProperty.call(o, r) && c.indexOf(r) < 0 && (n[r] = o[r]);
  if (o != null && typeof Object.getOwnPropertySymbols == "function")
    for (var l = 0, r = Object.getOwnPropertySymbols(o); l < r.length; l++)
      c.indexOf(r[l]) < 0 && Object.prototype.propertyIsEnumerable.call(o, r[l]) && (n[r[l]] = o[r[l]]);
  return n;
};
const ne = (o, c) => {
  const n = t.useContext(Q), {
    getPrefixCls: r,
    direction: l,
    form: e
  } = t.useContext(se), {
    prefixCls: _,
    className: q,
    rootClassName: R,
    size: j,
    disabled: w = n,
    form: V,
    colon: f,
    labelAlign: v,
    labelWrap: z,
    labelCol: x,
    wrapperCol: g,
    hideRequiredMark: y,
    layout: u = "horizontal",
    scrollToFirstError: E,
    requiredMark: p,
    onFinishFailed: C,
    name: m,
    style: I,
    feedbackIcons: N
  } = o, $ = ae(o, ["prefixCls", "className", "rootClassName", "size", "disabled", "form", "colon", "labelAlign", "labelWrap", "labelCol", "wrapperCol", "hideRequiredMark", "layout", "scrollToFirstError", "requiredMark", "onFinishFailed", "name", "style", "feedbackIcons"]), F = Y(j), D = t.useContext(le);
  process.env.NODE_ENV !== "production" && oe(o);
  const b = P(() => p !== void 0 ? p : y ? !1 : e && e.requiredMark !== void 0 ? e.requiredMark : !0, [y, p, e]), h = f ?? (e == null ? void 0 : e.colon), i = r("form", _), O = ie(i), [A, W, H] = te(i, O), L = J(i, `${i}-${u}`, {
    [`${i}-hide-required-mark`]: b === !1,
    [`${i}-rtl`]: l === "rtl",
    [`${i}-${F}`]: F
  }, H, O, W, e == null ? void 0 : e.className, q, R), [a] = re(V), {
    __INTERNAL__: k
  } = a;
  k.name = m;
  const B = P(() => ({
    name: m,
    labelAlign: v,
    labelCol: x,
    labelWrap: z,
    wrapperCol: g,
    vertical: u === "vertical",
    colon: h,
    requiredMark: b,
    itemRef: k.itemRef,
    form: a,
    feedbackIcons: N
  }), [m, v, x, g, u, h, b, a, N]);
  t.useImperativeHandle(c, () => a);
  const M = (s, d) => {
    if (s) {
      let S = {
        block: "nearest"
      };
      typeof s == "object" && (S = s), a.scrollToField(d, S);
    }
  }, G = (s) => {
    if (C == null || C(s), s.errorFields.length) {
      const d = s.errorFields[0].name;
      if (E !== void 0) {
        M(E, d);
        return;
      }
      e && e.scrollToFirstError !== void 0 && M(e.scrollToFirstError, d);
    }
  };
  return A(/* @__PURE__ */ t.createElement(U, {
    disabled: w
  }, /* @__PURE__ */ t.createElement(X.Provider, {
    value: F
  }, /* @__PURE__ */ t.createElement(Z, {
    // This is not list in API, we pass with spread
    validateMessages: D
  }, /* @__PURE__ */ t.createElement(ee.Provider, {
    value: B
  }, /* @__PURE__ */ t.createElement(K, Object.assign({
    id: m
  }, $, {
    name: m,
    onFinishFailed: G,
    form: a,
    style: Object.assign(Object.assign({}, e == null ? void 0 : e.style), I),
    className: L
  })))))));
}, T = /* @__PURE__ */ t.forwardRef(ne);
process.env.NODE_ENV !== "production" && (T.displayName = "Form");
const Ne = T;
export {
  Ne as default,
  re as useForm
};
