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

type FormQuotaSelectorProps = {
  control: any
  name: string
  label: string
  required?: boolean
  multiple?: boolean
  allowAll?: boolean
  onChange?: (value: any) => void
  filters?: { courseId: number | string }
  api?: string
} & BaseSelectProps
export default function FormQuotaSelector(props: FormQuotaSelectorProps) {
  const {
    name,
    required = false,
    control,
    label,
    allowAll = true,
    multiple = false,
    onChange,
    value,
    filters,
    api,
  } = props

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