import { createElement, createHook, forwardRef } from "@ariakit/react-utils";
import type { Props } from "@ariakit/react-utils";
import type { ElementType } from "react";
import type { GroupOptions } from "../group/group.tsx";
import { useGroup } from "../group/group.tsx";
import type { FormStore } from "./form-store.ts";

const TagName = "div" satisfies ElementType;
type TagName = typeof TagName;

/**
 * Returns props to create a `FormGroup` component.
 * @see https://ariakit.com/components/form
 * @example
 * ```jsx
 * const store = useFormStore();
 * const props = useFormGroup({ store });
 * <Form store={store}>
 *   <Role {...props}>
 *     <FormGroupLabel>Label</FormGroupLabel>
 *   </Role>
 * </Form>
 * ```
 */
export const useFormGroup = createHook<TagName, FormGroupOptions>(
  function useFormGroup({ store, ...props }) {
    props = useGroup(props);
    return props;
  },
);

/**
 * Renders a group element for form controls. The
 * [`FormGroupLabel`](https://ariakit.com/reference/form-group-label) component
 * can be used inside this component so the `aria-labelledby` prop is properly
 * set on the group element.
 * @see https://ariakit.com/components/form
 * @example
 * ```jsx {9-15}
 * const form = useFormStore({
 *   defaultValues: {
 *     username: "",
 *     email: "",
 *   },
 * });
 *
 * <Form store={form}>
 *   <FormGroup>
 *     <FormGroupLabel>Account</FormGroupLabel>
 *     <FormLabel name={form.names.username}>Username</FormLabel>
 *     <FormInput name={form.names.username} />
 *     <FormLabel name={form.names.email}>Email</FormLabel>
 *     <FormInput name={form.names.email} />
 *   </FormGroup>
 * </Form>
 * ```
 */
export const FormGroup = forwardRef(function FormGroup(props: FormGroupProps) {
  const htmlProps = useFormGroup(props);
  return createElement(TagName, htmlProps);
});

export interface FormGroupOptions<
  T extends ElementType = TagName,
> extends GroupOptions<T> {
  /**
   * Object returned by the
   * [`useFormStore`](https://ariakit.com/reference/use-form-store) hook. If not
   * provided, the closest [`Form`](https://ariakit.com/reference/form) or
   * [`FormProvider`](https://ariakit.com/reference/form-provider) components'
   * context will be used.
   */
  store?: FormStore;
}

export type FormGroupProps<T extends ElementType = TagName> = Props<
  T,
  FormGroupOptions<T>
>;
