"use client";

// src/components/ui/form.tsx
import * as React2 from "react";
import { Slot } from "radix-ui";
import {
  Controller,
  FormProvider,
  useFormContext
} from "react-hook-form";

// src/lib/utils.ts
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";
function cn(...inputs) {
  return twMerge(clsx(inputs));
}

// src/components/ui/label.tsx
import * as React from "react";
import { Label as LabelPrimitive } from "radix-ui";
import { jsx } from "react/jsx-runtime";
var Label = React.forwardRef(function Label2({ className, ...props }, ref) {
  return /* @__PURE__ */ jsx(
    LabelPrimitive.Root,
    {
      ref,
      "data-slot": "label",
      className: cn(
        "flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50",
        className
      ),
      ...props
    }
  );
});

// src/components/ui/form.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Form = FormProvider;
var FormFieldContext = React2.createContext(
  {}
);
function FormField({ ...props }) {
  const fieldContextValue = React2.useMemo(
    () => ({ name: props.name }),
    [props.name]
  );
  return /* @__PURE__ */ jsx2(FormFieldContext.Provider, { value: fieldContextValue, children: /* @__PURE__ */ jsx2(Controller, { ...props }) });
}
function useFormField() {
  const fieldContext = React2.useContext(FormFieldContext);
  const itemContext = React2.useContext(FormItemContext);
  const { getFieldState, formState } = useFormContext();
  const fieldState = getFieldState(fieldContext.name, formState);
  if (!fieldContext) throw new Error("useFormField must be used inside <FormField>");
  const { id } = itemContext;
  return {
    id,
    name: fieldContext.name,
    formItemId: `${id}-form-item`,
    formDescriptionId: `${id}-form-item-description`,
    formMessageId: `${id}-form-item-message`,
    ...fieldState
  };
}
var FormItemContext = React2.createContext({});
function FormItem({ className, ...props }) {
  const id = React2.useId();
  const itemContextValue = React2.useMemo(() => ({ id }), [id]);
  return /* @__PURE__ */ jsx2(FormItemContext.Provider, { value: itemContextValue, children: /* @__PURE__ */ jsx2("div", { "data-slot": "form-item", className: cn("flex flex-col gap-1.5", className), ...props }) });
}
function FormLabel({ className, ...props }) {
  const { error, formItemId } = useFormField();
  return /* @__PURE__ */ jsx2(
    Label,
    {
      "data-slot": "form-label",
      "data-error": !!error,
      className: cn("data-[error=true]:text-destructive", className),
      htmlFor: formItemId,
      ...props
    }
  );
}
function FormControl({ ...props }) {
  const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
  return /* @__PURE__ */ jsx2(
    Slot.Root,
    {
      "data-slot": "form-control",
      id: formItemId,
      "aria-describedby": !error ? formDescriptionId : `${formDescriptionId} ${formMessageId}`,
      "aria-invalid": !!error,
      ...props
    }
  );
}
function FormDescription({ className, ...props }) {
  const { formDescriptionId } = useFormField();
  return /* @__PURE__ */ jsx2(
    "p",
    {
      "data-slot": "form-description",
      id: formDescriptionId,
      className: cn("text-muted-foreground text-sm", className),
      ...props
    }
  );
}
function FormMessage({ className, children, ...props }) {
  const { error, formMessageId } = useFormField();
  const body = error ? String(error?.message ?? "") : children;
  if (!body) return null;
  return /* @__PURE__ */ jsx2(
    "p",
    {
      "data-slot": "form-message",
      id: formMessageId,
      className: cn("text-destructive text-sm", className),
      ...props,
      children: body
    }
  );
}
export {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
  useFormField
};
//# sourceMappingURL=form.js.map