import { createElement, createHook, forwardRef } from "@ariakit/react-utils";
import type { Props } from "@ariakit/react-utils";
import type { ElementType } from "react";
import type { DialogDescriptionOptions } from "../dialog/dialog-description.tsx";
import { useDialogDescription } from "../dialog/dialog-description.tsx";
import type { PopoverStore } from "./popover-store.ts";

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

/**
 * Returns props to create a `PopoverDescription` component. This hook must be
 * used in a component that's wrapped with `Popover` so the `aria-describedby`
 * prop is properly set on the popover element.
 * @see https://ariakit.com/components/popover
 * @example
 * ```jsx
 * // This component must be wrapped with Popover
 * const props = usePopoverDescription();
 * <Role {...props}>Description</Role>
 * ```
 */
export const usePopoverDescription = createHook<
  TagName,
  PopoverDescriptionOptions
>(function usePopoverDescription(props) {
  props = useDialogDescription(props);
  return props;
});

/**
 * Renders a description in a popover. This component must be wrapped with
 * [`Popover`](https://ariakit.com/reference/popover) so the `aria-describedby`
 * prop is properly set on the popover element.
 * @see https://ariakit.com/components/popover
 * @example
 * ```jsx {3}
 * <PopoverProvider>
 *   <Popover>
 *     <PopoverDescription>Description</PopoverDescription>
 *   </Popover>
 * </PopoverProvider>
 * ```
 */
export const PopoverDescription = forwardRef(function PopoverDescription(
  props: PopoverDescriptionProps,
) {
  const htmlProps = usePopoverDescription(props);
  return createElement(TagName, htmlProps);
});

export interface PopoverDescriptionOptions<
  T extends ElementType = TagName,
> extends DialogDescriptionOptions<T> {
  /**
   * Object returned by the
   * [`usePopoverStore`](https://ariakit.com/reference/use-popover-store) hook.
   *
   * **Note**: This prop has no effect on this component. The description is
   * linked to the closest [`Popover`](https://ariakit.com/reference/popover)
   * component through React context, so it must be rendered inside the popover
   * for the `aria-describedby` prop to be set on the popover element.
   */
  store?: PopoverStore;
}

export type PopoverDescriptionProps<T extends ElementType = TagName> = Props<
  T,
  PopoverDescriptionOptions<T>
>;
