'use strict';
'use client';
import { c } from 'react/compiler-runtime';
import { jsx } from 'react/jsx-runtime';
import React from 'react';
import { Field } from '@base-ui/react/field';
import { FormProvider, Controller, useFormContext } from 'react-hook-form';
import { cn } from '../../../fd-toolbox/lib/utils.js';
import { Label } from '../label/Label.js';
import { createError } from '../../../fd-toolbox/errors/errors.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
const FormItemContext = React.createContext(void 0);
const Form = FormProvider;
const FormFieldContext = React.createContext(void 0);
function FormField({
  name,
  render,
  disabled,
  control,
  rules,
  defaultValue,
  shouldUnregister
}) {
  const contextValue = {
    name
  };
  return /* @__PURE__ */jsx(FormFieldContext.Provider, {
    value: contextValue,
    children: /* @__PURE__ */jsx(Controller, {
      name,
      render,
      disabled,
      control,
      rules,
      defaultValue,
      shouldUnregister
    })
  });
}
__name(FormField, "FormField");
function useFormField() {
  const $ = c(11);
  const fieldContext = React.useContext(FormFieldContext);
  const itemContext = React.useContext(FormItemContext);
  const {
    getFieldState,
    formState
  } = useFormContext();
  if (!fieldContext) {
    throw createError("useFormField should be used within <FormField>");
  }
  if (!itemContext) {
    throw createError("useFormField should be used within <FormItem>");
  }
  let t0;
  if ($[0] !== fieldContext.name || $[1] !== formState || $[2] !== getFieldState) {
    t0 = getFieldState(fieldContext.name, formState);
    $[0] = fieldContext.name;
    $[1] = formState;
    $[2] = getFieldState;
    $[3] = t0;
  } else {
    t0 = $[3];
  }
  const fieldState = t0;
  const t1 = `${itemContext.id}-form-item`;
  const t2 = `${itemContext.id}-form-item-description`;
  const t3 = `${itemContext.id}-form-item-message`;
  let t4;
  if ($[4] !== fieldContext.name || $[5] !== fieldState || $[6] !== itemContext.id || $[7] !== t1 || $[8] !== t2 || $[9] !== t3) {
    t4 = {
      id: itemContext.id,
      name: fieldContext.name,
      formItemId: t1,
      formDescriptionId: t2,
      formMessageId: t3,
      ...fieldState
    };
    $[4] = fieldContext.name;
    $[5] = fieldState;
    $[6] = itemContext.id;
    $[7] = t1;
    $[8] = t2;
    $[9] = t3;
    $[10] = t4;
  } else {
    t4 = $[10];
  }
  return t4;
}
__name(useFormField, "useFormField");
function FormItem({
  className,
  children,
  ref
}) {
  const id = React.useId();
  const contextValue = {
    id
  };
  return /* @__PURE__ */jsx(FormItemContext.Provider, {
    value: contextValue,
    children: /* @__PURE__ */jsx(Field.Root, {
      ref,
      className: cn("space-y-2", className),
      children
    })
  });
}
__name(FormItem, "FormItem");
FormItem.displayName = "FormItem";
function FormLabel({
  className,
  children,
  ref
}) {
  const {
    error,
    formItemId
  } = useFormField();
  return /* @__PURE__ */jsx(Label, {
    ref,
    className: cn(error && "text-destructive", className),
    htmlFor: formItemId,
    children
  });
}
__name(FormLabel, "FormLabel");
FormLabel.displayName = "FormLabel";
function FormControl({
  ref,
  children
}) {
  const {
    error,
    formItemId,
    formDescriptionId,
    formMessageId
  } = useFormField();
  return /* @__PURE__ */jsx(Field.Control, {
    id: formItemId,
    ref,
    "aria-describedby": !error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`,
    "aria-invalid": !!error,
    render: props => children && React.isValidElement(children) ? React.cloneElement(children, {
      ...props,
      ...children.props
    }) : /* @__PURE__ */jsx("input", {
      ...props
    })
  });
}
__name(FormControl, "FormControl");
FormControl.displayName = "FormControl";
function FormDescription({
  className,
  children,
  ref
}) {
  const {
    formDescriptionId
  } = useFormField();
  return /* @__PURE__ */jsx("p", {
    ref,
    id: formDescriptionId,
    className: cn("text-muted-foreground text-sm", className),
    children
  });
}
__name(FormDescription, "FormDescription");
FormDescription.displayName = "FormDescription";
function FormMessage({
  className,
  children,
  ref
}) {
  const {
    error,
    formMessageId
  } = useFormField();
  const body = error?.message ?? children;
  if (!body) {
    return void 0;
  }
  return /* @__PURE__ */jsx("p", {
    ref,
    id: formMessageId,
    className: cn("text-destructive text-sm font-medium", className),
    children: body
  });
}
__name(FormMessage, "FormMessage");
FormMessage.displayName = "FormMessage";

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