import type { JSX } from "solid-js";
import { createUniqueId, onCleanup, onMount, splitProps } from "solid-js";
import { useInternalAlertDialogContext } from "./alert-dialog-context";

/** Props for AlertDialog.Description. */
export interface AlertDialogDescriptionProps extends JSX.HTMLAttributes<HTMLParagraphElement> {
  children?: JSX.Element;
}

/** Description for the AlertDialog. Registers its ID for aria-describedby. */
export function AlertDialogDescription(props: AlertDialogDescriptionProps): JSX.Element {
  const [local, rest] = splitProps(props, ["children"]);
  const ctx = useInternalAlertDialogContext();
  const id = createUniqueId();
  onMount(() => ctx.setDescriptionId(id));
  onCleanup(() => ctx.setDescriptionId(undefined));
  return (
    <p id={id} {...rest}>
      {local.children}
    </p>
  );
}
