import { useEffect, useState } from 'react'
import { useConnectionInterfaceConfig, useGeneratedConnections } from '../ConnectionInterface'

export function useRequiredConnectionOptions(connectionInterfaceType?: string) {
  const [remote, setRemote] = useState<string[]>([])
  const [isLoading, setLoading] = useState(true)
  const connectionContext = useConnectionInterfaceConfig()
  const local = useGeneratedConnections(connectionInterfaceType ?? '')

  useEffect(() => {
    (async () => {
      const type: string = connectionInterfaceType ?? ''
      if (!type) {
        return setLoading(false)
      }
      try {
        setRemote(await connectionContext.getConnectionInterfaces(type))
      } catch (error) {
        // eslint-disable-next-line no-console
        console.error(error)
      }
      setLoading(false)
    })()
  }, [])
  
  return { remote, local, isLoading }
}
