import { 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 { CheckboxCheckOptions } from "../checkbox/checkbox-check.tsx";
import { useCheckboxCheck } from "../checkbox/checkbox-check.tsx";
import { SelectItemCheckedContext } from "./select-context.tsx";
import type { SelectStore } from "./select-store.ts";

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

/**
 * Returns props to create a `SelectItemCheck` component. This hook must be used
 * in a component that's wrapped with `SelectItem` or the `checked` prop must be
 * explicitly passed to the component.
 * @deprecated Use `useComboboxItemCheck` instead.
 * @see https://ariakit.com/components/select
 * @example
 * ```jsx
 * const props = useSelectItemCheck({ checked: true });
 * <Role {...props} />
 * ```
 */
export const useSelectItemCheck = createHook<TagName, SelectItemCheckOptions>(
  function useSelectItemCheck({ store, checked, ...props }) {
    const context = useContext(SelectItemCheckedContext);
    checked = checked ?? context;
    props = useCheckboxCheck({ ...props, checked });
    return props;
  },
);

/**
 * Renders a checkmark icon when the
 * [`checked`](https://ariakit.com/reference/select-item-check#checked) prop is
 * `true`. The icon can be overridden by providing a different one as children.
 *
 * When rendered inside a
 * [`SelectItem`](https://ariakit.com/reference/select-item) component, the
 * [`checked`](https://ariakit.com/reference/select-item-check#checked) prop is
 * automatically derived from the context.
 * @deprecated Use
 * [`ComboboxItemCheck`](https://ariakit.com/reference/combobox-item-check)
 * instead.
 * @see https://ariakit.com/components/select
 * @example
 * ```jsx {5,9}
 * <SelectProvider>
 *   <Select />
 *   <SelectPopover>
 *     <SelectItem value="Apple">
 *       <SelectItemCheck />
 *       Apple
 *     </SelectItem>
 *     <SelectItem value="Orange">
 *       <SelectItemCheck />
 *       Orange
 *     </SelectItem>
 *   </SelectPopover>
 * </SelectProvider>
 * ```
 */
export const SelectItemCheck = forwardRef(function SelectItemCheck(
  props: SelectItemCheckProps,
) {
  const htmlProps = useSelectItemCheck(props);
  return createElement(TagName, htmlProps);
});

/** @deprecated Use `ComboboxItemCheckOptions` instead. */
export interface SelectItemCheckOptions<
  T extends ElementType = TagName,
> extends CheckboxCheckOptions<T> {
  /**
   * Object returned by the
   * [`useSelectStore`](https://ariakit.com/reference/use-select-store) hook.
   * This prop doesn't affect the checkmark. It's accepted for consistency with
   * other Ariakit components and is not passed to the underlying element. The
   * checked state is derived solely from the
   * [`checked`](https://ariakit.com/reference/select-item-check#checked) prop
   * or, when it's not provided, from the closest
   * [`SelectItem`](https://ariakit.com/reference/select-item) component.
   */
  store?: SelectStore;
}

/** @deprecated Use `ComboboxItemCheckProps` instead. */
export type SelectItemCheckProps<T extends ElementType = TagName> = Props<
  T,
  SelectItemCheckOptions<T>
>;
