import type { JSX } from "solid-js";
import { createUniqueId, onCleanup, onMount, splitProps } from "solid-js";
import { useInternalDrawerContext } from "./drawer-context";

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

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