import type { JSX } from "solid-js";
import { splitProps } from "solid-js";
import { useInternalSelectContext } from "./select-context";

export interface SelectItemProps extends JSX.HTMLAttributes<HTMLDivElement> {
  /** The value this item represents. */
  value: string;
  /** Whether this item is disabled. */
  disabled?: boolean;
  children?: JSX.Element;
}

/** A single selectable option within a Select dropdown. */
export function SelectItem(props: SelectItemProps): JSX.Element {
  const [local, rest] = splitProps(props, ["value", "disabled", "children"]);
  const ctx = useInternalSelectContext();
  const itemProps = () => ctx.navigation.getItemProps(local.value);

  return (
    <div
      aria-disabled={local.disabled || undefined}
      data-disabled={local.disabled || undefined}
      {...itemProps()}
      {...rest}
    >
      {local.children}
    </div>
  );
}
