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

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

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