"use client";
import { jsx as n } from "react/jsx-runtime";
import * as m from "react";
import { FormProvider as f, Controller as u, useFormContext as F, useFormState as x } from "react-hook-form";
import { Label as I } from "./label.js";
import { cn as a } from "../../../lib/utils.js";
const v = f, c = m.createContext(
  {}
), b = ({
  ...e
}) => /* @__PURE__ */ n(c.Provider, { value: { name: e.name }, children: /* @__PURE__ */ n(u, { ...e }) }), d = () => {
  const e = m.useContext(c), t = m.useContext(l), { getFieldState: r } = F(), o = x({ name: e.name }), s = r(e.name, o);
  if (!e)
    throw new Error("useFormField should be used within <FormField>");
  const { id: i } = t;
  return {
    id: i,
    name: e.name,
    formItemId: `${i}-form-item`,
    formDescriptionId: `${i}-form-item-description`,
    formMessageId: `${i}-form-item-message`,
    ...s
  };
}, l = m.createContext(
  {}
);
function S({ className: e, ...t }) {
  const r = m.useId();
  return /* @__PURE__ */ n(l.Provider, { value: { id: r }, children: /* @__PURE__ */ n(
    "div",
    {
      className: a("grid gap-2.5", e),
      "data-slot": "form-item",
      ...t
    }
  ) });
}
function $({
  className: e,
  ...t
}) {
  const { error: r, formItemId: o } = d();
  return /* @__PURE__ */ n(
    I,
    {
      className: a("data-[error=true]:text-destructive", e),
      "data-error": !!r,
      "data-slot": "form-label",
      htmlFor: o,
      ...t
    }
  );
}
function D({ children: e }) {
  const { error: t, formItemId: r, formDescriptionId: o, formMessageId: s } = d(), i = m.Children.only(e);
  return m.cloneElement(i, {
    id: r,
    "aria-describedby": t ? `${o} ${s}` : o,
    "aria-invalid": !!t,
    "data-slot": "form-control"
  });
}
function M({ className: e, ...t }) {
  const { formDescriptionId: r } = d();
  return /* @__PURE__ */ n(
    "p",
    {
      className: a("text-muted-foreground text-sm", e),
      "data-slot": "form-description",
      id: r,
      ...t
    }
  );
}
function N({ className: e, ...t }) {
  const { error: r, formMessageId: o } = d(), s = r ? String(r?.message ?? "") : t.children;
  return s ? /* @__PURE__ */ n(
    "p",
    {
      className: a("text-destructive text-sm", e),
      "data-slot": "form-message",
      id: o,
      ...t,
      children: s
    }
  ) : null;
}
export {
  v as Form,
  D as FormControl,
  M as FormDescription,
  b as FormField,
  S as FormItem,
  $ as FormLabel,
  N as FormMessage,
  d as useFormField
};
