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

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

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