"use client";
import { Slot } from '@radix-ui/react-slot';
import * as React from 'react';
import { useFormContext, FormProvider, Controller } from 'react-hook-form';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import * as LabelPrimitive from '@radix-ui/react-label';
import { cva } from 'class-variance-authority';
import { jsx } from 'react/jsx-runtime';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
function cn(...inputs) {
  return twMerge(clsx(inputs));
}
__name(cn, "cn");
var labelVariants = cva(
  "text-sm leading-none font-medium peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
);
var Label = /* @__PURE__ */ __name(({
  className,
  ...props
}) => /* @__PURE__ */ jsx(LabelPrimitive.Root, { className: cn(labelVariants(), className), ...props }), "Label");
Label.displayName = LabelPrimitive.Root.displayName;
var Form = FormProvider;
var FormFieldContext = React.createContext(
  {}
);
var FormField = /* @__PURE__ */ __name(({
  ...props
}) => {
  return /* @__PURE__ */ jsx(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsx(Controller, { ...props }) });
}, "FormField");
var useFormField = /* @__PURE__ */ __name(() => {
  const fieldContext = React.useContext(FormFieldContext);
  const itemContext = React.useContext(FormItemContext);
  const { getFieldState, formState } = useFormContext();
  const fieldState = getFieldState(fieldContext.name, formState);
  if (!fieldContext) {
    throw new Error("useFormField should be used within <FormField>");
  }
  const { id } = itemContext;
  return {
    id,
    name: fieldContext.name,
    formItemId: `${id}-form-item`,
    formDescriptionId: `${id}-form-item-description`,
    formMessageId: `${id}-form-item-message`,
    ...fieldState
  };
}, "useFormField");
var FormItemContext = React.createContext(
  {}
);
var FormItem = /* @__PURE__ */ __name(({
  className,
  ...props
}) => {
  const id = React.useId();
  return /* @__PURE__ */ jsx(FormItemContext.Provider, { value: { id }, children: /* @__PURE__ */ jsx("div", { className: cn("relative", className), ...props }) });
}, "FormItem");
FormItem.displayName = "FormItem";
var FormLabel = /* @__PURE__ */ __name(({
  className,
  ...props
}) => {
  const { error, formItemId } = useFormField();
  return /* @__PURE__ */ jsx(
    Label,
    {
      className: cn(
        "text-text-secondary pointer-events-none absolute top-3 left-4 flex text-xs leading-tight font-medium transition-all select-none",
        // input
        "peer-placeholder-shown:text-text-tertiary z-[1] peer-placeholder-shown:text-base peer-placeholder-shown:leading-[2.3]",
        "peer-focus:text-text-secondary peer-disabled:peer-placeholder-shown:text-text-tertiary peer-disabled:text-text-tertiary peer-focus:text-xs peer-focus:leading-tight",
        // select
        "peer-data-[placeholder]/select:top-5 peer-data-[placeholder]/select:text-base",
        // adornment
        "peer-[.adornment]/adornment:text-xs",
        error && "text-red-500",
        className
      ),
      htmlFor: formItemId,
      ...props
    }
  );
}, "FormLabel");
FormLabel.displayName = "FormLabel";
var FormControl = /* @__PURE__ */ __name(({ ...props }) => {
  const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
  return /* @__PURE__ */ jsx(
    Slot,
    {
      "aria-describedby": error ? `${formDescriptionId} ${formMessageId}` : `${formDescriptionId}`,
      "aria-invalid": !!error,
      id: formItemId,
      ...props
    }
  );
}, "FormControl");
FormControl.displayName = "FormControl";
var FormDescription = /* @__PURE__ */ __name(({
  className,
  ...props
}) => {
  const { formDescriptionId } = useFormField();
  return /* @__PURE__ */ jsx(
    "p",
    {
      className: cn("text-text-secondary py-1 text-[0.75rem]", className),
      id: formDescriptionId,
      ...props
    }
  );
}, "FormDescription");
FormDescription.displayName = "FormDescription";
var FormMessage = /* @__PURE__ */ __name(({
  className,
  children,
  ...props
}) => {
  const { error, formMessageId } = useFormField();
  const body = error ? String(error?.message) : children;
  if (!body) {
    return null;
  }
  return /* @__PURE__ */ jsx(
    "p",
    {
      className: cn(
        "text-red text-[0.75rem] font-medium dark:text-red-900",
        className
      ),
      id: formMessageId,
      ...props,
      children: body
    }
  );
}, "FormMessage");
FormMessage.displayName = "FormMessage";

export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useFormField };
