import { jsx as o, jsxs as j } from "react/jsx-runtime";
import u, { useContext as g } from "react";
import { c as L, b as P, d as y } from "../../../../FormPrimitives-CuaI5fdh.js";
import { Skeleton as N } from "../../../skeleton/SkeletonPrimitive.js";
import k from "../../hooks/useFieldContext.js";
import a from "../../../box/Box.js";
import q from "../../../error-message/ErrorMessage.js";
import { c as t } from "../../../../index-BtlgVD4m.js";
const M = ({ children: e, className: i, required: n, isLoading: x, skeletonClassName: b }) => {
  var c, f;
  const { field: d, fieldState: r, name: v, isLoading: C } = k(), F = x ?? C, { id: m } = g(L), { disabled: l } = g(P), I = m ? `${m}-form-item-message` : void 0;
  if (F)
    return /* @__PURE__ */ o(a, { "aria-busy": !0, className: t(i), children: /* @__PURE__ */ o(N, { className: t("h-[var(--spacing-10)] w-full rounded-[var(--border-radius-md)]", b) }) });
  if (!d) {
    const p = {
      ...n ? { required: !0 } : {},
      // Propagate form-level disabled so standalone inputs also respect the form state
      ...l ? { disabled: !0 } : {}
    }, E = Object.keys(p).length > 0;
    return /* @__PURE__ */ o(a, { className: t(i), children: E ? u.cloneElement(e, p) : e });
  }
  const s = e.props, h = {
    // Spread all RHF field props (value, onChange, onBlur, ref, name)
    ...d,
    // Only set id if not already provided by the user
    ...s.id === void 0 ? { id: v } : {},
    // Only inject isError when truly invalid — guard checks isError (not the old `error` prop)
    ...s.isError === void 0 && (r != null && r.error) ? { isError: !0 } : {},
    // Inject required if explicitly set on FieldControl
    ...n && s.required === void 0 ? { required: !0 } : {},
    // Propagate form-level disabled; child's own disabled prop takes precedence
    ...l && s.disabled === void 0 ? { disabled: !0 } : {}
  };
  return /* @__PURE__ */ j(a, { className: t("flex flex-col gap-[var(--spacing-2)]", i), children: [
    /* @__PURE__ */ o(y, { children: u.cloneElement(e, h) }),
    ((c = r == null ? void 0 : r.error) == null ? void 0 : c.message) && /* @__PURE__ */ o(q, { id: I, message: (f = r.error) == null ? void 0 : f.message })
  ] });
};
M.displayName = "FieldControl";
export {
  M as default
};
//# sourceMappingURL=FieldControl.js.map
