import { BaseSelectProps } from '@mui/material'
import { Controller } from 'react-hook-form'
import CourseSelector from '../CourseSelector'

type FormCourseSelectorProps = {
  control: any
  name: string
  label: string
  required?: boolean
  multiple?: boolean
  allowAll?: boolean
  onChange?: (value: any) => void
  api?: string
  valueByUniqueId?: boolean
} & BaseSelectProps
export default function FormCourseSelector(props: FormCourseSelectorProps) {
  const {
    name,
    required = false,
    control,
    label,
    allowAll = true,
    multiple = false,
    onChange,
    value,
    api,
    valueByUniqueId = false,
  } = props

  return (
    <Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <CourseSelector
          label={label}
          name={name}
          onChange={onChange ?? field.onChange}
          value={value ?? field?.value}
          allowAll={allowAll}
          required={required}
          error={!!error}
          helperText={error?.message}
          valueByUniqueId={valueByUniqueId}
          {...props}
        />
      )}
    />
  )
}
