"use client"
import { useController, useFormContext } from "react-hook-form"

import ReactAriaSelect from "@/components/TempDesignSystem/Select"

import type { SelectProps } from "./select"

export default function Select({
  className,
  items,
  label,
  disabled,
  name,
  isNestedInModal = false,
  registerOptions = {},
}: SelectProps) {
  const { control } = useFormContext()
  const { field } = useController({
    control,
    name,
    rules: registerOptions,
  })

  return (
    <ReactAriaSelect
      className={className}
      defaultSelectedKey={field.value}
      disabled={disabled || field.disabled}
      items={items}
      label={label}
      aria-label={label}
      name={field.name}
      onBlur={field.onBlur}
      onSelect={field.onChange}
      value={field.value}
      data-testid={name}
      isNestedInModal={isNestedInModal}
    />
  )
}
