import {
  useId,
  useSafeLayoutEffect,
  createElement,
  createHook,
  forwardRef,
} from "@ariakit/react-utils";
import type { Props } from "@ariakit/react-utils";
import type { ElementType } from "react";
import { useContext } from "react";
import type { PopoverHeadingOptions } from "../popover/popover-heading.tsx";
import { usePopoverHeading } from "../popover/popover-heading.tsx";
import { SelectHeadingContext } from "./select-context.tsx";
import type { SelectStore } from "./select-store.ts";

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

/**
 * Returns props to create a `SelectHeading` component.
 * @deprecated Use `useComboboxHeading` instead.
 * @see https://ariakit.com/components/select
 * @example
 * ```jsx
 * const props = useSelectHeading();
 * <Role {...props}>Heading</Role>
 * ```
 */
export const useSelectHeading = createHook<TagName, SelectHeadingOptions>(
  function useSelectHeading(props) {
    const [, setHeadingId] = useContext(SelectHeadingContext) || [];
    const id = useId(props.id);

    useSafeLayoutEffect(() => {
      setHeadingId?.(id);
      return () => setHeadingId?.(undefined);
    }, [setHeadingId, id]);

    props = {
      ...props,
      id,
    };

    props = usePopoverHeading(props);

    return props;
  },
);

/**
 * Renders a heading element that serves as a label for
 * [`SelectPopover`](https://ariakit.com/reference/select-popover) and
 * [`SelectList`](https://ariakit.com/reference/select-list) components.
 *
 * When this component is rendered within
 * [`SelectPopover`](https://ariakit.com/reference/select-popover), all
 * [`SelectItem`](https://ariakit.com/reference/select-item) elements must be
 * rendered within a [`SelectList`](https://ariakit.com/reference/select-list)
 * instead of directly within the popover.
 * @deprecated Use
 * [`ComboboxHeading`](https://ariakit.com/reference/combobox-heading) instead.
 * @see https://ariakit.com/components/select
 * @example
 * ```jsx {4}
 * <SelectProvider>
 *   <Select />
 *   <SelectPopover>
 *     <SelectHeading>Fruits</SelectHeading>
 *     <SelectList>
 *       <SelectItem value="Apple" />
 *       <SelectItem value="Orange" />
 *     </SelectList>
 *   </SelectPopover>
 * </SelectProvider>
 * ```
 */
export const SelectHeading = forwardRef(function SelectHeading(
  props: SelectHeadingProps,
) {
  const htmlProps = useSelectHeading(props);
  return createElement(TagName, htmlProps);
});

/** @deprecated Use `ComboboxHeadingOptions` instead. */
export interface SelectHeadingOptions<
  T extends ElementType = TagName,
> extends PopoverHeadingOptions<T> {
  /**
   * Object returned by the
   * [`useSelectStore`](https://ariakit.com/reference/use-select-store) hook.
   *
   * **Note**: This prop has no effect on this component. The heading is linked
   * to the closest [`SelectList`](https://ariakit.com/reference/select-list)
   * or [`SelectPopover`](https://ariakit.com/reference/select-popover)
   * component through React context, so it must be rendered inside the list or
   * popover for the `aria-labelledby` prop to be set on that element.
   */
  store?: SelectStore;
}

/** @deprecated Use `ComboboxHeadingProps` instead. */
export type SelectHeadingProps<T extends ElementType = TagName> = Props<
  T,
  SelectHeadingOptions<T>
>;
