'use client';
import { cva, cn, devWarn } from './chunk-VZLV27H2.js';
import * as React2 from 'react';
import { useFormContext, useFormState, FormProvider, Controller } from 'react-hook-form';
export { Controller, FormProvider, useController, useForm, useFormContext, useFormState, useWatch } from 'react-hook-form';
import { jsx } from 'react/jsx-runtime';

var Slot = React2.forwardRef(function Slot2(props, forwardedRef) {
  const { children, ...slotProps } = props;
  if (!React2.isValidElement(children)) {
    return null;
  }
  const childProps = children.props ?? {};
  const mergedProps = mergeProps(slotProps, childProps);
  let childRef = childProps.ref;
  if (childRef === void 0 && !("ref" in childProps)) {
    childRef = children.ref;
  }
  const mergedRef = composeRefs(forwardedRef, childRef);
  return React2.cloneElement(
    children,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    { ...mergedProps, ref: mergedRef }
  );
});
Slot.displayName = "Slot";
function mergeProps(slotProps, childProps) {
  const merged = { ...slotProps };
  for (const key of Object.keys(childProps)) {
    const slotValue = slotProps[key];
    const childValue = childProps[key];
    if (/^on[A-Z]/.test(key) && typeof slotValue === "function" && typeof childValue === "function") {
      merged[key] = (...args) => {
        const child = childValue;
        const slot = slotValue;
        child(...args);
        slot(...args);
      };
    } else if (key === "className") {
      merged[key] = [slotValue, childValue].filter(Boolean).join(" ");
    } else if (key === "style") {
      merged[key] = { ...slotValue, ...childValue };
    } else if (childValue !== void 0) {
      merged[key] = childValue;
    }
  }
  return merged;
}
function composeRefs(...refs) {
  return (node) => {
    for (const ref of refs) {
      if (typeof ref === "function") {
        ref(node);
      } else if (ref != null) {
        const objectRef = ref;
        objectRef.current = node;
      }
    }
  };
}
var Form = FormProvider;
var FormFieldContext = React2.createContext(
  void 0
);
var FormItemContext = React2.createContext(
  void 0
);
function FormField(props) {
  return /* @__PURE__ */ jsx(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsx(Controller, { ...props }) });
}
function useFormField() {
  const fieldContext = React2.useContext(FormFieldContext);
  const itemContext = React2.useContext(FormItemContext);
  const formContext = useFormContext();
  const { errors } = useFormState({
    control: formContext?.control,
    name: fieldContext?.name,
    exact: true
  });
  if (!fieldContext) {
    devWarn(
      "react-form/use-form-field-outside-form-field",
      "useFormField (via FormLabel/FormControl/FormDescription/FormMessage) must be used inside a <FormField>. The missing FormFieldContext makes this throw."
    );
    throw new Error("useFormField must be used within a <FormField>");
  }
  if (!itemContext) {
    devWarn(
      "react-form/use-form-field-outside-form-item",
      "useFormField (via FormLabel/FormControl/FormDescription/FormMessage) must be used inside a <FormItem>. The missing FormItemContext makes this throw."
    );
    throw new Error("useFormField must be used within a <FormItem>");
  }
  const { id } = itemContext;
  const error = getNestedError(errors, fieldContext.name);
  return {
    id,
    name: fieldContext.name,
    formItemId: `${id}-form-item`,
    formDescriptionId: `${id}-form-item-description`,
    formMessageId: `${id}-form-item-message`,
    error,
    invalid: Boolean(error)
  };
}
function getNestedError(errors, name) {
  if (!errors) return void 0;
  const segments = name.replace(/\[(\w+)\]/g, ".$1").split(".");
  let cursor = errors;
  for (const seg of segments) {
    if (cursor && typeof cursor === "object" && seg in cursor) {
      cursor = cursor[seg];
    } else {
      return void 0;
    }
  }
  return cursor;
}
var formItemVariants = cva({ base: "space-y-2" });
var FormItem = React2.forwardRef(
  ({ className, ...props }, ref) => {
    const id = React2.useId();
    return /* @__PURE__ */ jsx(FormItemContext.Provider, { value: { id }, children: /* @__PURE__ */ jsx("div", { ref, className: cn(formItemVariants(), className), ...props }) });
  }
);
FormItem.displayName = "FormItem";
var formLabelVariants = cva({
  base: "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",
  variants: {
    invalid: {
      true: "text-destructive",
      false: ""
    }
  },
  defaultVariants: {
    invalid: "false"
  }
});
var FormLabel = React2.forwardRef(
  ({ className, ...props }, ref) => {
    const { invalid, formItemId } = useFormField();
    return /* @__PURE__ */ jsx(
      "label",
      {
        ref,
        htmlFor: formItemId,
        "data-invalid": invalid ? "" : void 0,
        className: cn(formLabelVariants({ invalid: invalid ? "true" : "false" }), className),
        ...props
      }
    );
  }
);
FormLabel.displayName = "FormLabel";
var FormControl = React2.forwardRef(
  ({ ...props }, ref) => {
    const { invalid, formItemId, formDescriptionId, formMessageId } = useFormField();
    return /* @__PURE__ */ jsx(
      Slot,
      {
        ref,
        id: formItemId,
        "aria-describedby": invalid ? `${formDescriptionId} ${formMessageId}` : formDescriptionId,
        "aria-invalid": invalid || void 0,
        ...props
      }
    );
  }
);
FormControl.displayName = "FormControl";
var FormDescription = React2.forwardRef(({ className, ...props }, ref) => {
  const { formDescriptionId } = useFormField();
  return /* @__PURE__ */ jsx(
    "p",
    {
      ref,
      id: formDescriptionId,
      className: cn("text-sm text-muted-foreground", className),
      ...props
    }
  );
});
FormDescription.displayName = "FormDescription";
var FormMessage = React2.forwardRef(
  ({ className, children, ...props }, ref) => {
    const { error, formMessageId } = useFormField();
    const body = error?.message ? String(error.message) : children;
    if (!body) return null;
    return /* @__PURE__ */ jsx(
      "p",
      {
        ref,
        id: formMessageId,
        className: cn("text-sm font-medium text-destructive", className),
        ...props,
        children: body
      }
    );
  }
);
FormMessage.displayName = "FormMessage";

export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, Slot, formItemVariants, formLabelVariants, useFormField };
//# sourceMappingURL=form.js.map
//# sourceMappingURL=form.js.map