'use client';

import * as React from 'react';
import { useIsoLayoutEffect } from '@base-ui/utils/useIsoLayoutEffect';
import { useId } from '@base-ui/utils/useId';
import { useToastRootContext } from "../root/ToastRootContext.js";
import { useRenderElement } from "../../utils/useRenderElement.js";

/**
 * A title that labels the toast.
 * Renders an `<h2>` element.
 *
 * Documentation: [Base UI Toast](https://base-ui.com/react/components/toast)
 */
export const ToastTitle = /*#__PURE__*/React.forwardRef(function ToastTitle(componentProps, forwardedRef) {
  const {
    render,
    className,
    id: idProp,
    children: childrenProp,
    ...elementProps
  } = componentProps;
  const {
    toast
  } = useToastRootContext();
  const children = childrenProp ?? toast.title;
  const shouldRender = Boolean(children);
  const id = useId(idProp);
  const {
    setTitleId
  } = useToastRootContext();
  useIsoLayoutEffect(() => {
    if (!shouldRender) {
      return undefined;
    }
    setTitleId(id);
    return () => {
      setTitleId(undefined);
    };
  }, [shouldRender, id, setTitleId]);
  const state = React.useMemo(() => ({
    type: toast.type
  }), [toast.type]);
  const element = useRenderElement('h2', componentProps, {
    ref: forwardedRef,
    state,
    props: {
      ...elementProps,
      id,
      children
    }
  });
  if (!shouldRender) {
    return null;
  }
  return element;
});
if (process.env.NODE_ENV !== "production") ToastTitle.displayName = "ToastTitle";